进程调度-时间片轮转

2019-07-14 09:25发布

#include "stdio.h" #include #include #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 struct pcb { /* 定义进程控制块PCB */ char name[10]; char state; int super; int ntime; int rtime; struct pcb* link; }*ready=NULL,*p,*tail=NULL; //ready ,p 为全局变量; typedef struct pcb PCB; void sort() /* 按先来先服务原则排序,将进程插入到就绪队列末尾*/ { if(ready==NULL) /*就绪队列空,插入队首*/ { //p->link=ready; ready=p; tail=p; } else /* 进程插入到就绪队列末尾*/ { tail->link=p; tail=p; } } void input() /* 建立进程控制块函数*/ { int i,num; //clrscr(); /*清屏*/ printf(" 请输入进程数量:"); scanf("%d",&num); for(i=0;iname); printf(" 输入进程优先数:"); scanf("%d",&p->super); printf(" 输入进程运行时间:"); scanf("%d",&p->ntime); printf(" "); p->rtime=0;p->state='w'; p->link=NULL; sort(); /* 调用sort函数*/ } } int space() { int l=0; PCB* pr=ready; while(pr!=NULL) { l++; pr=pr->link; } return(l); } void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ { printf(" qname state super ndtime runtime "); printf("|%s ",pr->name); printf("|%c ",pr->state); printf("|%d ",pr->super); printf("|%d ",pr->ntime); printf("|%d ",pr->rtime); printf(" "); } void check() /* 建立进程查看函数 */ { PCB* pr; printf(" **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf(" ****当前就绪队列状态为: "); /*显示就绪队列状态*/ while(pr!=NULL) { disp(pr); pr=pr->link; } } void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ { printf(" 进程 [%s] 已完成. ",p->name); free(p); } void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ { (p->rtime)++; if(p->rtime==p->ntime) destroy(); /* 进程运行结束,调用destroy函数撤销进程*/ else { (p->super)--; p->state='w'; sort(); /*调用sort函数,将当前进程重新插入就绪队列中*/ } } void main() /*主函数*/ { int len,h=0; char ch; input(); /* 调用建立进程控制块函数,创建就绪队列*/ len=space(); while((len!=0)&&(ready!=NULL)) { //ch=getchar(); h++; printf(" The execute number:%d ",h); p=ready; ready=p->link; p->link=NULL; p->state='R'; check(); running(); printf(" 按任一键继续......"); ch=getchar(); } printf(" 进程已经完成. "); //ch=getchar(); }