时间片的轮转算法

时间片的轮转算法

ID:39927115

大小:86.00 KB

页数:4页

时间:2019-07-15

时间片的轮转算法_第1页
时间片的轮转算法_第2页
时间片的轮转算法_第3页
时间片的轮转算法_第4页
资源描述:

《时间片的轮转算法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、#include#include#definetrue1;#definefalse0;#defineERROR0;#defineOK1;structPCB_type{intpid;//进程名intstate;//进程状态2--表示"执行"状态1--表示"就绪"状态0--表示"阻塞"状态intcpu_time;//运行需要的CPU时间)};typedefstructQNode{PCB_typedata;structQNode*next;}QNode,*QueueP

2、tr;typedefstruct{QueuePtrfront;QueuePtrrear;}LinkQueue;intInitQueue(LinkQueue&Q){Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));if(!Q.front)exit(-2);Q.front->next=NULL;return1;}intQueueEmpty(LinkQueueQ){intisempty=false;if(Q.front==Q.rear)isempty=true;

3、returnisempty;}intEnQueue(LinkQueue*Q,PCB_typee){/*插入元素e为Q的新的队尾元素*/QueuePtrp=(QueuePtr)malloc(sizeof(QNode));if(!p)/*存储分配失败*/exit(-2);p->data=e;p->next=NULL;(*Q).rear->next=p;(*Q).rear=p;returnOK;}intDeQueue(LinkQueue*Q,PCB_type*e){/*若队列不空,删除Q的队头元素,用e返

4、回其值,并返回OK,否则返回ERROR*/QueuePtrp;if((*Q).front==(*Q).rear)returnERROR;p=(*Q).front->next;*e=p->data;(*Q).front->next=p->next;if((*Q).rear==p)(*Q).rear=(*Q).front;free(p);returnOK;}voidmain(){PCB_typee,k,h;inti,m,n,t,time;intuse_cpu=0;intx=0;intunuse_cpu=

5、0;printf("ttt**************");printf("ttt*模拟进程调度*");printf("t进程状态2--表示执行状态1--表示就绪状态0--表示阻塞状态");printf("tt请输入时间片大小(以1为单位时间片大小为3表示一个时间片运行3秒):");scanf("%d",&time);printf("请输入隔几个时间片释放系统资源:");scanf("%d",&t);LinkQueueReadyQueue,BlockQueu

6、e;InitQueue(ReadyQueue);InitQueue(BlockQueue);printf("请输入就绪队列的个数:");scanf("%d",&m);for(i=1;i<=m;i++){printf("请分别输入第%d进程的信息",i);printf("pid:");scanf("%d",&e.pid);e.state=1;printf("cpu_time:");scanf("%d",&e.cpu_time);EnQueue(&ReadyQueue,e);}printf("请输入

7、堵塞队列的个数:");scanf("%d",&n);for(i=1;i<=n;i++){printf("请分别输入第%d进程的信息",i);printf("pid:");scanf("%d",&e.pid);e.state=0;printf("cpu_time:");scanf("%d",&e.cpu_time);EnQueue(&BlockQueue,e);}printf("进程cputime状态");while(!QueueEmpty(ReadyQueue)){DeQueue(&Read

8、yQueue,&k);if(k.cpu_time<=time){use_cpu+=k.cpu_time;unuse_cpu-=k.cpu_time;k.state=2;x++;printf("%4d%4d%4d",k.pid,k.cpu_time,k.state);}else{k.cpu_time-=time;k.state=2;use_cpu+=time;EnQueue(&ReadyQueue,k);printf("%4d%4d%4d",k.pid,time

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。