1、作业调度程序#define N 20#define M 1000000#define NULL 0#includetypedef struct qNode int name;float arrive;float runtime;float response;struct qNode *next;qNode,*linkPtr;typedef struct linkQueue /*定义队列*/ linkPtr front;linkPtr rear;linkQueue;initQueue(linkQueue *q) (*q).front=(*q).rear=(linkPtr)malloc(sizeo
2、f(qNode);if(!(*q).front) printf(“creat fail!n“);exit();(*q).rear-next=NULL;createWork(linkQueue *work) /*按输入创建工作队列*/ qNode p;qNode *ptr;int i,n;char get=1;ptr=(*work).rear;printf(“how many work do you want to do:n“);scanf(“%d“,printf(“input the work as:name arrive-time runtime,as: 6 8.9 3.1n“);/*pri
3、ntf(“Press Twice when input the works name,q to quit inputn“);*/for(i=0;inext=ptr;(*work).rear=ptr;return n;prinInf() printf(“input your choicen“);printf(“t1 first come first servertt2 shortest job firstn“);printf(“t3 shortest surplus_time firsttt4 high response_ratio nextn“);printf(“t0 to quitn“);p
4、rintf(“: “);printJob(linkPtr ptr) /*打印输入作业*/ ptr=ptr-next;printf(“input work were:n“);while(ptr) printf(“%d %10.3f %10.3fn“,ptr-name,ptr-arrive,ptr-runtime);ptr=ptr-next;fcfs(int n,linkPtr avp) /*先来先服务*/ linkQueue fcfs;linkPtr ptr,tmp;int i;float t,T,Tw,TtN2;T=Tw=0.0;i=0;initQueue(avp=avp-next;while
5、(avp) /*按到达时间排序*/ ptr=fcfs.front;tmp=(linkPtr)malloc(sizeof(qNode);while(ptr-next-arrivearrivetmp-name=avp-name;tmp-arrive=avp-arrive;tmp-runtime=avp-runtime;tmp-next=ptr-next;ptr-next=tmp;if(ptr=fcfs.rear) fcfs.rear=tmp;avp=avp-next;ptr=fcfs.front-next;t=ptr-arrive;while(ptr) /*按先来先服务进行相关计算*/ if(pt
6、r-arriveruntime;Tti0=t-ptr-arrive;Tti1=Tti0/ptr-runtime;ptr=ptr-next;+i;else t=ptr-arrive;t=t+ptr-runtime;Tti0=t-ptr-arrive;Tti1=Tti0/ptr-runtime;ptr=ptr-next;+i;ptr=fcfs.front-next;for(i=0;iname);ptr=ptr-next;printf(“njob cost time cost time with weightn“);ptr=fcfs.front-next;for(i=0;iname,Tti0,Tti
7、1);ptr=ptr-next;printf(“naverage cost time: %10.3fnaverage cost time with weight: %10.3fn“,T,Tw);sjf(int n,linkPtr avp) /*短作业优先*/ linkQueue fcfs;linkPtr ptr,tmp,temp;int i,j,k,num,turn;double t,T,Tw,TtN2;int R2*N,RrN;T=Tw=0.0;turn=num=k=i=0;initQueue(avp=avp-next;while(avp) /*按到达时间排序*/ ptr=fcfs.fron
8、t;tmp=(linkPtr)malloc(sizeof(qNode);while(ptr-next-arrivearrivetmp-name=avp-name;tmp-arrive=avp-arrive;tmp-runtime=avp-runtime;tmp-next=ptr-next;ptr-next=tmp;if(ptr=fcfs.rear) fcfs.rear=tmp;avp=avp-next;fcfs.front-arrive=fcfs.front-runtime=M;ptr=fcfs.front-next;t=ptr-arrive;temp=fcfs.front;while(ptr
9、!=NULL) /*到达最后一个作业之后停止第一步计算*/ tmp=fcfs.front-next;temp=fcfs.front;j=0;while(ptr-arrivenext;while(tmp!=ptr) if(tmp-runtimeruntime) temp=tmp;tmp=tmp-next;if(temp-runtime!=M) Ri=Rrk=temp-name;Ttk0=t+temp-runtime-temp-arrive;Ttk1=Ttk0/temp-runtime;turn=i;t=t+temp-runtime;temp-runtime=M;while(ptr-arriven
10、ext;k+;i+;tmp=fcfs.front-next;while(tmp!=ptr) /*查看在 PTR指针指向作业之前有没有作业还未运行完*/ if(tmp-runtime!=M)j+;tmp=tmp-next;if(j=0) /*若在 PTR之前作业都已做完,将时间置为PTR指向的作业的时间*/ t=ptr-arrive;ptr=fcfs.front-next;for(j=0;jruntime!=M) num+;ptr=ptr-next;for(j=0;jnext;while(ptr) while(ptr) /*找第一个未完成的作业*/ if(ptr-runtimenext;whil
11、e(ptr) /*找最短作业时间*/if(ptr-runtimeruntime)temp=ptr;ptr=ptr-next;if(temp-runtime!=M) Rrk=temp-name;Ttk0=t+temp-runtime-temp-arrive;Ttk1=Ttk0/temp-runtime;Ri=temp-name;turn=i;i+;k+;t=t+temp-runtime;temp-runtime=M;for(i=0;inext;while(avp) /*按先来先服务排序,便于以后调度用*/ ptr=fcfs.front;tmp=(linkPtr)malloc(sizeof(qNo
12、de);while(ptr-next-arrivearrivetmp-name=avp-name;tmp-arrive=avp-arrive;tmp-runtime=avp-runtime;tmp-next=ptr-next;ptr-next=tmp;if(ptr=fcfs.rear) fcfs.rear=tmp;avp=avp-next;fcfs.front-arrive=fcfs.front-runtime=M;ptr=fcfs.front-next;t=ptr-arrive;while(ptr!=fcfs.rear) T=ptr-next-arrive-ptr-arrive; /*作业调
13、度时间差*/while(T0.0) tmp=temp=fcfs.front;while(tmp!=ptr) if(tmp-next-runtimeruntime) temp=tmp-next;tmp=tmp-next;if(temp-runtimeT) temp-runtime=temp-runtime-T;Ri=temp-name;t=t+T;T=0.0;turn=i;i+;else if(temp-runtime=T) Ri=Rrk=temp-name;Ttk0=t+temp-runtime-temp-arrive;Ttk1=Ttk0/temp-runtime;temp-runtime=M
14、;t=t+T;T=0.0;turn=i;i+;k+;else Ri=Rrk=temp-name;Ttk0=t+temp-runtime-temp-arrive;Ttk1=Ttk0/temp-runtime;turn=i;i+;k+;t=t+temp-runtime;T=T-temp-runtime;temp-runtime=M;ptr=ptr-next;ptr=fcfs.front-next;for(j=0;jruntime!=M) num+;ptr=ptr-next;for(j=0;jnext;while(ptr) while(ptr) /*找第一个未完成的作业*/ if(ptr-runti
15、menext;while(ptr) /*找最短作业时间*/if(ptr-runtimeruntime)temp=ptr;ptr=ptr-next;if(temp-runtime!=M) Ri=Rrk=temp-name;Ttk0=t+temp-runtime-temp-arrive;Ttk1=Ttk0/temp-runtime;turn=i;i+;k+;t=t+temp-runtime;temp-runtime=M;for(i=0;inext;while(avp) /*按先来先服务排序,便于以后调度用*/ ptr=fcfs.front;tmp=(linkPtr)malloc(sizeof(qN
16、ode);while(ptr-next-arrivearrivetmp-name=avp-name;tmp-arrive=avp-arrive;tmp-runtime=avp-runtime;tmp-next=ptr-next;ptr-next=tmp;if(ptr=fcfs.rear) fcfs.rear=tmp;avp=avp-next;fcfs.front-response=0.5;tmp=ptr=fcfs.front-next;temp=fcfs.front;t=ptr-arrive;done=n;while(done0) j=0;tmp=fcfs.front-next;temp=fc
17、fs.front;while(t=ptr-arrivewhile(tmp!=ptr) if(tmp-response)!=0.5) tmp-response=1+(t-tmp-arrive)/tmp-runtime;if(tmp-responsetemp-responsetmp=tmp-next;if(temp-response)!=0.5) t=t+temp-runtime;Rk=temp-name;Ttk0=t-temp-arrive;Ttk1=Ttk0/temp-runtime;temp-response=0.5; /*将完成作业的响应比置为0.5*/k+;tmp=fcfs.front-
18、next;while(tmp!=ptr) if(tmp-response)!=0.5)+j;tmp=tmp-next;if(j=0-done;for(i=0;in;i+) T=T+Tti0;Tw=Tw+Tti1;T=T/n;Tw=Tw/n;printf(“nturn:n“);for(i=0;in;i+) printf(“%d “,Ri);printf(“n“);printf(“njob cost time cost time with weightn“);for(i=0;in;i+) printf(“%d %10.3f%10.3fn“,Ri,Tti0,Tti1);printf(“naverag
19、e cost time: %10.3fnaverage cost time with weight: %10.3fn“,T,Tw);main() linkQueue work;linkPtr ptr;int num;char choice;choice=5;initQueue(num=createWork(ptr=work.front;while(choice!=0) system(“cls“);prinInf();choice=getchar();printJob(work.front); /*打印作业程序使用者输入的作业*/switch(choice) case 1: fcfs(num,ptr);break;case 2: sjf(num,ptr);break;case 3: rsjf(num,ptr);break;case 4: hrn(num,ptr);break;case 0: exit();printf(“npress anykey to continue.n“);getch();