教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 专业资料 >

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

来源:网络收集 时间:2026-01-03
导读: 操作系统课程设计,进程调度模拟源代码 // // experiment1.cpp : Defines the entry point for the console application. // #include stdio.h #include stdlib.h #include time.h typedef struct PCB { int name; int runtime; int runedtime; int state; in

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

//

// experiment1.cpp : Defines the entry point for the console application. // #include "stdio.h" #include "stdlib.h" #include "time.h" typedef struct PCB { int name; int runtime; int runedtime; int state; int killtime; int waitpoint; int waittime; struct PCB *next; }PCB; #define NUM 10 int main(int argc, char* argv[]) { int 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;i<NUM;i++)//创建进程 { temp=new PCB; temp->name=i; temp->runtime=rand()%20+1; temp->runedtime=0; temp->next=NULL; temp->killtime=0; temp->waitpoint=rand()%temp->runtime+1; temp->waittime=rand()%20+1; if(i==0) {top=temp; tail=temp;} else{ tail->next=temp; tail=temp;

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

}

printf("create name %d, runtime=%d, runedtime=%d,killtime=%d, waitpoint=%d,waittime=%d\n"

,tail->name,tail->runtime,tail->runedtime,tail->killtime,tail->waitpoint,tail->waittime); }

while(top!=NULL||top_wait!=NULL)

{

if(top==NULL)

goto 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->waitpoint<timeslice)){//判断是否到达等待时间点如果是,则从运行队列上删除节点,插入阻塞队列

runqueue->runtime=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;//插入阻塞队列

}else{

tail_wait->next=runqueue;

tail_wait=tail_wait->next;//插入阻塞队列

}

printf("runtowait name %d,runtime=%d,runedtime=%d,killtime=%d\n"

,runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime); }

else if(runqueue->runtime<=0){//判断是否结束

runqueue->killtime=0;

runqueue->runtime=0;

printf("kill name %d, runtime=%d, runedtime=%d,killtime=%d\n"

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

,runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime); n++;

delete(runqueue);

}//如果是,删除节点

else{//如果时间片结束后进程没有被杀死且不需要等待,插入就绪队列队尾 if(top!=NULL){//判断就绪队列是否为空

tail->next=runqueue;

tail=tail->next;

}else{

top=runqueue;

tail=runqueue;

}

printf("running name %d, runtime=%d, runedtime=%d,killtime=%d\n"

,runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime); }

flag:

if (top_wait!=NULL)

{

temp=top_wait;

while(temp!=NULL) //扫描阻塞队列,是否存在等待时间已到的节点 {

// printf("waiting name %d waittime=%d\n",temp->name,temp->waittime); if(temp->waittime<=0){

temp->killtime=0;

printf("waittorun name %d,runtime=%d,runedtime=%d,,killtime=%d\n" ,temp->name,temp->runtime,temp->runedtime,temp->killtime); if(top!=NULL){//判断就绪队列是否为空

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;

}else{

top=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; }

…… 此处隐藏:1477字,全部文档内容请下载后查看。喜欢就下载吧 ……
操作系统课程设计进程调度模拟源代码.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/51332.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)