FCFS算法实验报告.docx
《FCFS算法实验报告.docx》由会员分享,可在线阅读,更多相关《FCFS算法实验报告.docx(7页珍藏版)》请在冰豆网上搜索。
![FCFS算法实验报告.docx](https://file1.bdocx.com/fileroot1/2022-11/17/4ac08db3-640f-4213-be92-1bb40c287f5b/4ac08db3-640f-4213-be92-1bb40c287f5b1.gif)
FCFS算法实验报告
一:
实验题目:
设计一个先来先服务调度算法
二:
源程序名:
fcfs.cpp,执行程序名:
fcfs.exe
三:
使用的数据结构及符号说明如下
typedefstructpcb//定义进程控制块pcb的数据结构{
charpname[N];//进程名
intruntime;//进程运行时间
intarrivetime;//进程到来时间
charstate;//进程状态
structpcb*next;//指向下一进程的指针}PCB;
PCBhead_input;//定义初始放入进程的队列的头指针PCBhead_run;//定义运行程序的头指针
四:
FCFS算法源码及注释:
#defineN20
#include
#include//输入输出流头文件
#include//文件头文件
usingnamespacestd;
typedefstructpcb//定义进程控制块pcb的数据结构
{
charpname[N];//进程名
intruntime;//进程运行时间
intarrivetime;//进程到来时间
charstate;//进程状态
structpcb*next;//指向下一进程的指针
}PCB;
PCBhead_input;//定义初始放入进程的队列的头指针PCBhead_run;//定义执行中进程的头指针
PCB*pcb_input;//定义运行程序的头指针
staticcharR='r',C='c';
unsignedlongcurrent;//定义当前时间变量
voidinputprocess();//定义进程进入函数
intreadydata();//定义进程判断函数
intreadyprocess();//定义进程准备函数
intrunprocess();//定义进程运行函数
ofstreamfout("result.txt");//打开文件result.txt,用于记录结果
intreadyprocess()
{
while
(1)
if(readydata()==0)//判断是否还有进程没有执行完毕,如果返回结果是1,那么代表没有进
//程要执行了
return1;//以执行完所有程序,退出
else
runprocess();//还有程序要执行,执行运行函数
}
}
intreadydata()//函数准备函数
{
if(head_input.next==NULL)//判断是否还有没有运行过的进程
{
if(head_run.next==NULL)//判断是否有进程在运行,有则执行返回值1,
//回到readyprocess函数,无则
return0;//说明没有进程了,退出
else
return1;
}
PCB*p1,*p2,*p3,*t;
p1=head_input.next;//定义p1,使p1等于放入的第一个进程
p2=&head_run;//定义p2,使p2等于运行函数的头指针
t=p1;//定义t,使它和p1相等;
p3=&head_input;//定义p3,是它等于放入进程队列的头指针
while(p1->next!
=NULL)//用循环,找出进程队列里到达时间最小的进程
{
if(p1->next->arrivetimearrivetime)
{
t=p1->next;
p3=p1;//使p3指向t
}
p1=p1->next;
}
if(((unsignedlong)t->arrivetime<=current)&&(t->state==R))//判断t所指的进程是否符合条件,
//是则开始运行
{
cout<<"Time"<pname<<"start,\n";
fout<<"Time"<pname<<"start,\n";
p2->next=t;
p3->next=t->next;//将t从放入的队列中移到
//run队列中;
}
return1;
}
intrunprocess()//定义运行进程函数//
{
PCB*p1,*p2;
if(head_run.next==NULL)//判断是否有进程符合条件运行,
//有则执行运行代码,{//无则将当前时间+1;
current++;
return1;
}
else
{
p1=head_run.next;
p2=&head_run;
while(p1!
=NULL)//如果运行中的程序未运行完,则继续运行
{
p1->runtime--;
current++;
while
(1)
{
if(p1->runtime<=0)//如果运行中的程序的runtime为0,
//则说明程序已经运行结束
{
cout<<"Time"<pname<<"end.\n";
fout<<"Time"<pname<<"end.\n";
p1->state=C;//将p1进程的状态标志为"C"完成状态
p2->next=p1->next;//将run的头指针指向空
p1=NULL;//删除p1进程
break;//结束,跳出
}
else
{
p1->runtime--;//如果p1没有执行完,
current++;//则继续执行直至结束
}
}
}
return1;
}
}
voidinputprocess()//--定义建立进程函数//
{
PCB*p1,*p2;
intnum;
cout<<"Howmanyprocessesdoyouwanttorun:
";//提示输入进程数
fout<<"Howmanyprocessesdoyouwanttorun:
";
cin>>num;
fout<p1=&head_input;//让p1等于放入进程的头指针
p2=p1;//让p2等于p1都等于放入进程的头指针p1->next=newPCB;//为p1指向的下一个进程申请空间
p1=p1->next;//让p1等于它所指向的下一个进程
for(inti=0;i{
cout<<"\nNo."<
";//进程名
fout<<"\nNo."<
";
cin>>p1->pname;
fout<pname;
cout<<"runtime:
";//运行时间
fout<<"\nruntime:
";
cin>>p1->runtime;
fout<runtime;
cout<<"arrivetime:
";//到达时间
fout<<"\narrivetime:
";
cin>>p1->arrivetime;
fout<arrivetime<<"\n";
p1->state=R;//初始化状态为"R"准备状态
p1->next=newPCB;//为下一个进程申请空间
p2=p1;
p1=p1->next;//让p1指向它所指的下一个进程}
p2->next=NULL;//让最后一个进程所指的下一个进程为空}
voidmain()
{
current=0;//初始化时钟current为0;
inputprocess();//运行进程构造函数
readyprocess();//运行进程运行函数
getch();
fout.close();//文件关闭
}
五:
实验结果:
1:
初始状态:
Howmanyprocessesdoyouwanttorun:
3No.1processinputpname:
p1
runtime:
20
arrivetime:
1
No.2processinputpname:
p2
runtime:
23
arrivetime:
4
No.3processinputpname:
p3
runtime:
54
arrivetime:
10
2:
具体运行过程
Time1;Processp1start,
Time21;Processp1end.
Time21;Processp2start,
Time44;Processp2end.
Time44;Processp3start,
Time98;Processp3end.