#include<stdio.h> #include <stdlib.h> typedef struct pcb { char name[5]; int yruntime; int arrivetime; int runtime; int finishtime; int time; char state; struct pcb* link; }PCB; PCB *ready = NULL, *p; void disp(PCB* p);
PCB * sort(PCB * p) { PCB *first, *second; int insert = 0; if (ready == NULL || (p->runtime-p->yruntime) >(ready->runtime-ready->yruntime))
{ p->link = ready; ready = p; } else if(( p->runtime-p->yruntime)==(ready->runtime-ready->yruntime)) { if(p->arrivetime<ready->arrivetime) { p->link=ready; ready=p; } else { p=ready->link; p->link=ready->link->link; } } else if((p->runtime-p->yruntime)<(ready->runtime-ready->yruntime)) { first = ready; second = first->link; while (second != NULL) { if ((p->runtime-p->yruntime) > (second->runtime-second->yruntime)) { p->link = second; first->link = p; insert = 1; break; } else if((p->runtime-p->yruntime)==(second->runtime-second->yruntime)) { if(p->arrivetime<second->arrivetime) { p->link = second; first->link = p; insert = 1; break; } else { p=second->link; p->link=second->link->link; }
} else if((p->runtime-p->yruntime)<(second->runtime-second->yruntime)) { first = first->link; second = second->link; }
} if (insert == 0) { first->link = p; p->link = NULL; } } return ready; }
void input() { int i,j; printf("\n请输入进程个数:"); scanf("%d",&j); for(i=1;i<=j;i++) { printf("\n第%d个进程\n",i); p=(PCB*)malloc(sizeof(PCB)); printf("\n输入进程名:"); scanf("%s",p->name); printf("\n输入进程需运行的时间:"); scanf("%d",&(p->runtime)); printf("\n输入进程的到达时间:"); scanf("%d",&(p->arrivetime)); p->state='R'; p->yruntime=0; p->link=NULL; sort(p); } //printf("按进程的优先级进行排序!\n"); p=ready; while (p != NULL) { disp(p); p = p->link; } }
int maxnumber(PCB *p) { int max; max=p->arrivetime; while(p->link!=NULL) { if(max<p->link->arrivetime) { max=p->link->arrivetime; } p=p->link; } return(max); }
void disp(PCB* p) { printf("进程名 到达时间 运行时间 状态\n"); printf("%s\t",p->name); printf("%d\t",p->arrivetime); printf("%d\t",p->runtime); printf("%c\t\n",p->state); }
void run(PCB *p) { PCB *s,*q; int time=0,a,sum=0,n=0; float sum1=0.0; printf("进程的调度开始!\n"); p=ready; s=p; a=maxnumber(p); while(p!=NULL) { if(p->state=='C') { break; }
while(s!=NULL) { n=n+1; s->time=s->runtime; s=s->link; } printf("进程%s运行一次\n",p->name); p->yruntime++; time=time+1; p->finishtime=time+a; if(p->runtime==p->yruntime) { p->state='C'; printf("进程名为%s,其周转时间为 %d\n",p->name,(p->finishtime)-p->arrivetime); sum+=(p->finishtime)-(p->arrivetime);
printf("进程名为%s的带权周转时间%f\n",p->name,((p->finishtime)-(p->arrivetime))*1.0/(p->time)); sum1+=(time*1.0)/((p->finishtime)-(p->arrivetime)); ready=p->link; p->link=NULL; p=ready; } else { ready=p->link; p->link=NULL;
p=sort(p); }
} printf("平均周转时间为%f\n",sum*1.0/n); printf("平均带权周转时间%f\n",sum1*1.0/n);
}
void main() { input(); p=ready; run(p); }
神呀,你也用代码编辑器贴出来呀,这样岂不是吓人吗?
我知道,你可以把它放到编译器里去修改运行一下啊!求帮忙!
@bkhappy: 并不是每个给你解决问题的人都有开发环境的,眼睛有时比编译器好使