FCFS算法实验报告_第1页
FCFS算法实验报告_第2页
FCFS算法实验报告_第3页
FCFS算法实验报告_第4页
FCFS算法实验报告_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、char pnameN; int runtime; int arrivetime;char state; struct pcb *next;char pnameN; int runtime; int arrivetime;char state; struct pcb *next;PCB;PCB head_input;PCB head_run;四:FCFS算法源码及注释:#define N 20#include #include #include using namespace std; typedef struct pcb char pnameN;int runtime;int arrivet

2、ime;char state; struct pcb *next; PCB;PCB head_input;PCB head_run;PCB * pcb_input;static char R=r,C=c; unsigned long current; void inputprocess(); int readydata(); int readyprocess(); int runprocess();ofstream fout(result.txt);一:实验题目: 设计一个先来先服务调度算法二:源程序名:fcfs.cpp,执行程序名:fcfs.exe 三:使用的数据结构及符号说明如下typed

3、ef struct pcb/定义进程控制块 pcb 的数据结构/进程名/进程运行时间/进程到来时间/进程状态/指向下一进程的指针/定义初始放入进程的队列的头指针/定义运行程序的头指针/输入输出流头文件/文件头文件/定义进程控制块 pcb 的数据结构/进程名/进程运行时间/进程到来时间/进程状态/指向下一进程的指针/定义初始放入进程的队列的头指针/定义执行中进程的头指针/定义运行程序的头指针/定义当前时间变量/定义进程进入函数/定义进程判断函数/定义进程准备函数/定义进程运行函数/打开文件 result.txt, 用于记录结果int readyprocess()while(1)if(readyd

4、ata() = 0)return 1;elserunprocess();/判断是否还有进程没有执行完毕if(readydata() = 0)return 1;elserunprocess();/以执行完所有程序,退出/还有程序要执行,执行运行函数int readydata()/函数准备函数if(head_input.next = NULL) /判断是否还有没有运行过的进程 if(head_run.next = NULL)/判断是否有进程在运行,有则执行返回值1,/回到 readyprocess 函数,无则return 0;/说明没有进程了,退出elsereturn 1;PCB *p1,*p2,

5、*p3,*t;p1 PCB *p1,*p2,*p3,*t;p1 = head_input.next; p2 = &head_run;t = p1;p3 = &head_input; while(p1-next != NULL) /定义pl,使pl等于放入的第一个进程/定义p2,使p2等于运行函数的头指针定义t,使它和pl相等;/定义p3,是它等于放入进程队列的头指针/用循环,找出进程队列里到达时间最小的进程if(p1-next-arrivetime arrivetime)/使 p3 指向 tt = p1-next;/使 p3 指向 t p1 = p1-next;if(unsigned long

6、)t - arrivetime state = R) /判断 t 所指的进程是否符合条件,/是则开始运行coutTime current;Processpname start,n; foutTime current;Processpnamenext = t;p3-next = t -next;/将 t 从放入的队列中移到/run 队列中;return 1;int runprocess()PCB *p1,*p2; if(head_run.next int runprocess()PCB *p1,*p2; if(head_run.next = NULL) current+; return 1;el

7、se/定义运行进程函数/判断是否有进程符合条件运行,/有则执行运行代码,/无则将当前时间+1;p1 = head_run.next;p2 = &head_run; while(p1 != NULL) p1-runtimep1 = head_run.next;p2 = &head_run; while(p1 != NULL) p1-runtime-;/如果运行中的程序未运行完,则继续运行current+; while(1) if(p1-runtime = 0)/如果运行中的程序的 runtime 为 0,/则说明程序已经运行结束coutTime current;Processpname end.

8、n; foutTime current;Processpnamestate = C;p2-next = p1-next;p1 = NULL;break;将pl进程的状态标志为C完成状态/将 run 的头指针指向空删除pl进程/结束,跳出 elsep1-runtime-; current+;/如果 p1 没有执行完, /则继续执行直至结束return 1;void inputprocess()/-定义建立进程函数 /PCB *p1,*p2;int num;coutHow many processes do you want to run:; /提示输入进程数 foutnum;foutnum;p1

9、 = foutnext = new PCB;p1 = p1-next;for(int i = 0; i num; i+)coutnNo.i+1 process input pname:; foutnNo.i+1p1-pname;foutpname;cout foutp1-runtime;foutruntime;cout foutp1-arrivetime; foutarrivetimestate = R;p1-next = new PCB;p2 = p1; p1 = p1-next; p2-next = NULL;runtime:;runtime:;arrivetime:;arrivetime

10、:;让pl等于放入进程的头指针让p2等于pl都等于放入进程的头指针/为 p1 指向的下一个进程申请空间/让 p1 等于它所指向的下一个进程/循环输入 num 个进程的/进程名/运行时间/到达时间/初始化状态为R准备状态/为下一个进程申请空间让pl指向它所指的下一个进程/让最后一个进程所指的下一个进程为空/初始化时钟 /初始化时钟 current 为 0;/运行进程构造函数/运行进程运行函数/文件关闭void main()current = 0; inputprocess(); readyprocess(); getch(); fout.close();五:实验结果:1:初始状态:How many processes do you want to run:3No.1 process input pname:p1runtime:20arrivetime:1No.2 process input pname:p2runtime:23arrivetime:4No.3 process input pname:p3runtime:54arrivetime:102:具体运行过程Time 1; Processp1 start,Time 21; Processp1 end.Time 21; Processp2 start,Time 44; Processp2 end.Time 44; Pr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论