#include
{ char name[10]; /*进程名*/
int count; /*计数器,判断是否=时间片的大小*/ int cputime; /*占用cpu时间*/ int needtime; /*要求运行时间*/ char state; /*状态*/ struct node *next; /*指针*/ }PCB;
PCB *ready,*run,*finish,*tail; /*就绪 执行 结束 尾指针*/ int N,round;
void prt() /*输出函数,可以方便看到进程执行的演示*/ { /*略 同优先数法*/ }
void insert(PCB *q) /*在队尾插入新的进程*/ { tail->next=q; tail=q; q->next=NULL; }
void create() { PCB *p; int i;
ready=NULL; run=NULL; finish=NULL; printf(\ /*输入进程名、和*/
for(i=0;i scanf(\ /*输入进程要求运行时间*/ p->cputime=0; p->state='W'; /*表示就绪队列中未在队首先执行,但也是就绪状态*/ if (ready!=NULL) insert(p); /*就绪队首不为NULL,插入新进程*/ else {p->next=ready; ready=p; tail=p; } } printf(\ Display is going to start: \\n\ printf(\ prt(); run=ready; /*队列排好,run指向就绪队列队首*/ ready=ready->next; /*ready指向下一个进程*/ run->state='R'; } /*队首进程的状态为就绪*/ void count() { while(run!=NULL) { run->cputime=run->cputime+1; /*运行一次cpu占用时间加一*/ run->needtime=run->needtime-1; /*运行一次要求运行时间减一*/ run->count=run->count+1; /*运行一次计数器加一*/ if(run->needtime==0) /*若要求运行时间为0时*/ { run->next=finish; /*退出队列*/ finish=run; /*finish为结束进程的队列 */ run->state='E'; /*修改状态为结束*/ run=NULL; /*释放run指针*/ if (ready!=NULL) /*创建新就绪队列的头指针*/ { run=ready; run->state='R'; ready=ready->next; } } else if(run->count==round) /*如果时间片到*/ { run->count=0; /*计数器置0*/ if(ready!=NULL) /*如就绪队列不空*/ { run->state='W'; insert(run); /*在进程队列中重新插入原来的队首进程*/ run=ready; /*重新置就绪队列的头指针*/ run->state='R'; ready=ready->next; } prt(); } } void main() { printf(\ scanf(\ printf(\ scanf(\ /*输入时间片的大小,不应太大*/ create(); /*模拟创建进程,并输入相关信息*/ count(); } /*优先数调度算法*/ 四、程序运行的初值及执行结果: 1、优先数调度算法: 以下是输出的结果(符合优先数调度): 2、时间片轮转法调度算法: 以下是输出结果(符合时间片轮转法调度):
相关推荐: