作业调度进程c语言代码,用C实现三种进程作业调度FCFS,SJF,HRN的代码

我们的程序作业。

///FCFS。cpp

int wcompare(const PCB &e1,const PCB &e2)

{

if(e1。waittime>e2。

waittime)return 1;

elsereturn 0;

}

int maxwaittime(CLink &L_ready,PCB &maxwait)

{

//用maxwait返回等待时间最长的进程

//返回值为此进程在链表中的序号

//若链表为空,返回0

PCB tmp;

int index=0,t;

maxwait。

waittime=-1;

while(t=L_ready。LocateElem(maxwait,wcompare,tmp))

{

maxwait=tmp;

index=t;

}

return index;

}

void fcfs()

{

int time=0;//系统时间

int free=true;//CPU忙碌时为false,空闲时为true

int tag;//标志是否有新进程进入

int index;

PCB running;

CLink L_ready;//就绪进程链表对象

int task=process。

Length();//任务数

fprintf(fout,"先来先服务调度策略(FCFS):\n");

while(task)//开始选择和运行进程,直到所有进程运行完毕

{

if(tag=getready(L_ready,time))

{

fprintf(fout,"-----------time: %3d-----------\n",time);

fprintf(fout,"进程名称 到达时间 服务时间\n");

L_ready。

Traverse(print);

fputc('\n',fout);

}

if(free)//当前没有任务,CPU空闲

if(index=maxwaittime(L_ready,running))

{

fprintf(fout,"-----------time: %3d-----------\n选择执行的进程为:%s\n\n",time, );

free=false;//选择等待时间最久的进程

}

time++;

if(!free)

{

running。

function;//运行等待时间最久的进程

talrun++;

L_ready。Traverse(addwaittime);

if( talrun>= rvetime)

{

//进程任务完成

L_ready。

Delete(index,running);//把此进程从进程链表中删除

nishtime=time;

index=process。ElemIndex(running);

process。SetCurElem(index,running);

fprintf(fout,"-----------time: %3d-----------\n完成进程:%s\n\n",time, );free=true;

task--;

}

}

}

generalize();

fputc('\n',fout);

}

///FCFS。

cpp

///spn。cpp

int servetimecmp(const PCB &e1,const PCB &e2)

{

if( rvetime &L_ready,PCB &minserve)

{

//用minserve返回服务时间最短的进程

//返回值为此进程在链表中的序号

//若链表为空,返回0

PCB tmp;

int index,t;

index=L_ready。

GetFirst(minserve);

while(t=L_ready。LocateElem(minserve,servetimecmp,tmp))

{

minserve=tmp;

index=t;

}

return index;

}

void spn()

{

int time=0;//系统时间

int free=true;//CPU忙碌时为false,空闲时为true

int tag;//标志是否有新进程进入

int index;

PCB running;

CLink L_ready;//就绪进程链表对象

int task=process。

Length();//任务数

fprintf(fout,"最短进程Next调度策略(SPN):\n");

while(task)//开始选择和运行进程,直到所有进程运行完毕

{

if(tag=getready(L_ready,time))

{

fprintf(fout,"-----------time: %3d-----------\n",time);

fprintf(fout,"进程名称 到达时间 服务时间\n");

L_ready。

Traverse(print);

fprintf(fout,"\n");

}

if(free)//当前没有任务,CPU空闲

if(index=minservetime(L_ready,running))

{

fprintf(fout,"-----------time: %3d-----------\n选择执行的进程为:%s\n\n",time, );

free=false;//选择等待时间最久的进程

}

if(!free)

{

running。

function;//运行等待时间最久的进程

talrun++;

L_ready。Traverse(addwaittime);

if( talrun>= rvetime)

{

//进程任务完成

L_ready。

Delete(index,running);//把此进程从进程链表中删除

fprintf(fout,"完成进程%s:\n\n", );

free=true;

task--;

}

}

time++;

}

generalize();

fputc('\n',fout);

}

///spn。

cpp

///hrrn。cpp

int w_scompare(const PCB &e1,const PCB &e2)

{

if( rvetime==0|| rvetime==0)return ERROR;

if(e1。

waittime*1。0/ rvetime>e2。waittime*1。0/ rvetime)

return 1;

else

return 0;

}

int maxw_stime(CLink &L_ready,PCB &maxw_s)

{

//用maxw_s返回w与s之比最大的进程

//返回值为此进程在链表中的序号

//若链表为空,返回0

PCB tmp;

int index=0,t;

maxw_s。

waittime=-1;

maxw_ rvetime=1;

while(t=L_ready。LocateElem(maxw_s,w_scompare,tmp))

{

maxw_s=tmp;

index=t;

}

return index;

}

void hrrn()

{

int time=0;//系统时间

int index;

int tag;//标志是否有新进程进入

int free=true;//CPU忙碌时为false,空闲时为true

PCB running;

CLink L_ready;//就绪进程链表对象

int task=process。

Length();//任务数

fprintf(fout,"最高响应比调度策略(HRRN):\n");

while(task)//开始选择和运行进程,直到所有进程运行完毕

{

if(tag=getready(L_ready,time))

{

fprintf(fout,"-----------time: %3d-----------\n",time);

fprintf(fout,"进程名称 到达时间 服务时间\n");

L_ready。

Traverse(print);

fprintf(fout,"\n");

}

if(free)//当前没有任务,CPU空闲

if(index=maxw_stime(L_ready,running))

{

fprintf(fout,"-----------time: %3d-----------\n选择执行的进程为:%s\n\n",time, );

free=false;//选择等待时间最久的进程

}

if(!free)

{

running。

function;//运行进程

talrun++;

L_ready。Traverse(addwaittime);

if( talrun== rvetime)

{

//进程任务完成

L_ready。

Delete(index,running);//把此进程从进程链表中删除

fprintf(fout,"完成进程%s:\n\n", );

task--;

free=true;

}

}

time++;

}

generalize();

fputc('\n',fout);

}

///hrrn。

cpp

///主程序main。cpp///

#define FILEERROR-1//错误号

#define INPUTERROR-2

#define NAMELEN10//进程名最大长度

#define BASEID100//进程ID号分配基值

#include

#include

#include

#include "Link。

cpp"//链表类模板定义

#include "LinkQueue。cpp"//队列类模板定义

class PCB

{

public:

int ID;//每个ID号唯一标识一个进程

char name[NAMELEN];//进程名称

int arrivetime;//进程到达时间

int servetime;//进程服务时间

int waittime;//已等待时间

int totalrun;//总运行时间

int finishtime;//运行完成时间

void (*function)();//运行程序指针

int operator ==(const PCB &pcb0)

{

if(ID==pcb0。

ID)return true;

elsereturn false;

}

int operator !=(const PCB &pcb0)

{

if(ID!=pcb0。ID)return true;

elsereturn false;

}

};//PCB进程控制块

FILE *fin,*fout;//输入输出文件

CLink process;//要运行的进程,以链表形式

int task;//要运行的进程数

void run()//运行进程

{

}

int arrivetimecmp(const PCB &e1,const PCB &e2)

{

if( rivetime== rivetime&&e1。

ID!=e2。ID)

return true;

return false;

}

int addwaittime(PCB &e)

{

e。waittime++;

return OK;

}

int getready(CLink &L_ready,int time)

{

//找出process链表中到达时间为time的PCB挂到L_ready链表

//若有新进程进入就绪队列,返回值tag为true,否则为false

int tag=false;

PCB tmp0,tmp;

rivetime=time;

while(process。

LocateElem(tmp0,arrivetimecmp,tmp))

{

L_ready。Append(tmp);

tmp0=tmp;

tag=true;

}

return tag;

}

int print(PCB &toprint)//打印进程信息

{

fprintf(fout,"%-10s%-10d%-10d\n", , rivetime, rvetime);

return OK;

}

void generalize()

{

PCB toprint;

int i,Tr_total=0,task=process。

Length();

double Trs_total=0;

fprintf(fout,"%10s%10s%10s%10s%10s%10s\n","进程名称","到达时间","服务时间","完成时间","周转时间","带权周转");

for(i=1;i>choice;

if(choice=='y'||choice=='Y')

{

fin=fopen("input。

txt","r");//读方式打开文件

if(!fin)errordeal(FILEERROR);

fgets(buf,sizeof(buf),fin);//接收输入

while(!feof(fin))//接收进程信息,检验数据有效性并送入链表

{

if(getinfo(buf,tmp)==OK)

{

process。

Append(tmp);//入链表

tmp。ID++;

}

elseerrordeal(INPUTERROR);

fgets(buf,sizeof(buf),fin);

}

}

else

{

fin=fopen("input。

txt","w");//写方式打开文件

if(!fin)errordeal(FILEERROR);

printf("请按到达时间顺序依次输入进程名称、到达时间和服务时间:\n");

printf("(以空格为分隔,每行一个进程,进程名不超过%d个字符,结束输入请在行首键入\\字符)\n",NAMELEN);

fgets(buf,sizeof(buf),stdin);//接收输入

while(buf[0]!='\\')//接收进程信息,检验数据有效性并送入链表

{

fputs(buf,fin);

if(getinfo(buf,tmp)==OK)

{

process。

Append(tmp);//入链表

tmp。ID++;

}

elseerrordeal(INPUTERROR);

fgets(buf,sizeof(buf),stdin);

}

}

fclose(fin);

if(process。

Empty())printf("不存在有效的进程信息!程序中止!\n");

else

{

fout=fopen("output。txt","w");

fprintf(fout,"输入进程信息:\n");

fprintf(fout,"进程名称 到达时间 服务时间\n");

process。

Traverse(print);

fputc('\n',fout);

printf("进程信息接收完毕!接着请选择CPU调度策略(输入序号):\n");

printf("1。 先来先服务(FCFS)\n2。 循环\n3。 最短进程Next(SPN)\n");

printf("4。

最短剩余时间(SRT)\n5。 最高响应比(HRRN)\n6。

退出\n");

cin>>choice;

while(choice'6')

{

errordeal(INPUTERROR);//错误处理函数

choice=fgetc(stdin);

fgetc(stdin);

}

while(choice!='6')

{

switch(choice)

{

case '1':

fcfs();

break;

case '2':

printf("请输入循环时间的长度:");

cin>>q;

circulate(q);

break;

case '3':

spn();

break;

case '4':

srt();

brea。

全部

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值