操作系统课程设计进程调度模拟源代码.doc

上传人:laozhun 文档编号:2388243 上传时间:2023-02-17 格式:DOC 页数:4 大小:20.50KB
返回 下载 相关 举报
操作系统课程设计进程调度模拟源代码.doc_第1页
第1页 / 共4页
操作系统课程设计进程调度模拟源代码.doc_第2页
第2页 / 共4页
操作系统课程设计进程调度模拟源代码.doc_第3页
第3页 / 共4页
操作系统课程设计进程调度模拟源代码.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《操作系统课程设计进程调度模拟源代码.doc》由会员分享,可在线阅读,更多相关《操作系统课程设计进程调度模拟源代码.doc(4页珍藏版)》请在三一办公上搜索。

1、/ experiment1.cpp : Defines the entry point for the console application./#include stdio.h#include stdlib.h#include time.htypedef struct PCB int name; int runtime; int runedtime;/ int state; int killtime; int waitpoint; int waittime;struct PCB *next;PCB;#define NUM 10int main(int argc, char* argv)int

2、 n=0;int timeslice=3;PCB *runqueue=NULL; /运行队列PCB *top=NULL,*tail=NULL,*temp; /就绪队列PCB *top_wait=NULL,*tail_wait=NULL,*p;int i;srand(10);/给定随机数种子srand(int)time(NULL); for(i=0;iname=i; temp-runtime=rand()%20+1; temp-runedtime=0; temp-next=NULL; temp-killtime=0; temp-waitpoint=rand()%temp-runtime+1; t

3、emp-waittime=rand()%20+1;if(i=0) top=temp; tail=temp; elsetail-next=temp;tail=temp;printf(create name %d, runtime=%d, runedtime=%d,killtime=%d, waitpoint=%d,waittime=%dn,tail-name,tail-runtime,tail-runedtime,tail-killtime,tail-waitpoint,tail-waittime); while(top!=NULL|top_wait!=NULL)if(top=NULL)goto

4、 flag;runqueue=top;top=top-next;runqueue-next=NULL;/从就绪队列选一个节点,插入运行队列runqueue-killtime=timeslice;/计算消耗的时间runqueue-runedtime=runqueue-runedtime+runqueue-killtime;/计算总共运行时间runqueue-runtime=runqueue-runtime-timeslice;/计算剩余运行时间if(runqueue-runedtime-runqueue-waitpoint=0)&(runqueue-runedtime-runqueue-wait

5、pointruntime=runqueue-runtime+runqueue-runedtime-runqueue-waitpoint;runqueue-killtime=timeslice-(runqueue-runedtime-runqueue-waitpoint);runqueue-runedtime=runqueue-waitpoint;if(top_wait=NULL)/判断阻塞队列是否为空top_wait=runqueue;tail_wait=top_wait;/插入阻塞队列elsetail_wait-next=runqueue;tail_wait=tail_wait-next;/

6、插入阻塞队列printf(runtowait name %d,runtime=%d,runedtime=%d,killtime=%dn,runqueue-name,runqueue-runtime,runqueue-runedtime,runqueue-killtime);else if(runqueue-runtimekilltime=0;runqueue-runtime=0;printf(kill name %d, runtime=%d, runedtime=%d,killtime=%dn,runqueue-name,runqueue-runtime,runqueue-runedtime,

7、runqueue-killtime);n+;delete(runqueue);/如果是,删除节点else/如果时间片结束后进程没有被杀死且不需要等待,插入就绪队列队尾if(top!=NULL)/判断就绪队列是否为空tail-next=runqueue;tail=tail-next;elsetop=runqueue;tail=runqueue;printf(running name %d, runtime=%d, runedtime=%d,killtime=%dn,runqueue-name,runqueue-runtime,runqueue-runedtime,runqueue-killtim

8、e);flag: if (top_wait!=NULL) temp=top_wait; while(temp!=NULL) /扫描阻塞队列,是否存在等待时间已到的节点 / printf(waiting name %d waittime=%dn,temp-name,temp-waittime); if(temp-waittimekilltime=0;printf(waittorun name %d,runtime=%d,runedtime=%d,killtime=%dn,temp-name,temp-runtime,temp-runedtime,temp-killtime);if(top!=NU

9、LL)/判断就绪队列是否为空tail-next=temp;/插入就绪队列tail=tail-next;if(temp=top_wait)top_wait=top_wait-next;else if(top_wait-next!=NULL)/p=top_wait;while(p-next!=temp)p=p-next;if(temp=tail_wait)tail_wait=p;p-next=temp-next;temp=p;/把节点从阻塞队列删除tail-next=NULL;elsetop=temp;tail=temp;if(temp=top_wait)top_wait=top_wait-next;else if(top_wait-next!=NULL)/p=top_wait;while(p-next!=temp)p=p-next;if(temp=tail_wait)tail_wait=p;p-next=temp-next;temp=p;/把节点从阻塞队列删除tail-next=NULL;else temp-waittime=temp-waittime-timeslice;/修改继续等待时间 temp=temp-next; printf(所有%d个进程已执行完毕n,n);return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号