操作系统课程设计进程调度模拟源代码
操作系统课程设计,进程调度模拟源代码
//
// 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字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [专业资料]《蜜蜂之家》教学反思
- [专业资料]过去分词作定语和表语1
- [专业资料]苏州工业园区住房公积金贷款申请表
- [专业资料]保安管理制度及处罚条例细则
- [专业资料]2018年中国工程咨询市场发展现状调研及
- [专业资料]2015年电大本科《学前教育科研方法》期
- [专业资料]数字信号处理实验 matlab版 离散傅里叶
- [专业资料]“十三五”重点项目-虎杖白藜芦醇及功
- [专业资料]2015-2020年中国竹木工艺市场需求及投
- [专业资料]国际贸易理论与实务作业五:理论案例分
- [专业资料]财政部修订发布事业单位会计制度
- [专业资料]BCA蛋白浓度测定试剂盒(增强型)
- [专业资料]工程进度总计划横道图模板(通用版)
- [专业资料]七年级地理同步练习(天气与气候)
- [专业资料]X光安检机介绍火灾自动报警系统的组成
- [专业资料]衢州市人民政府办公室关于印发衢州市区
- [专业资料]经济全球化及其影响[1]
- [专业资料]质粒DNA限制性酶切图谱分析
- [专业资料]国家安全人民防线工作“六项”制度
- [专业资料]劳动力投入计划及保证措施
- 电子账册联网监管培训手册
- 人教版语文七年级上第1课《在山的那边
- 对我区担保行业发展现状的思考与建议
- 平面四边形网格自动生成方法研究
- 2016年党课学习心得体会范文
- 如何设置电脑定时关机
- 全球最美人妖排行榜新鲜出炉
- 社会实践调查报告及问卷
- Visual Basic习题集
- 《鱼我所欲也》课件2
- 浙江省会计从业资格考试试卷
- 全遥控数字音量控制的D 类功率放大器资
- 鞍钢宪法与后福特主义
- 电表的改装与校准实验报告(1)
- 2014年高考理科数学真题解析分类汇编:
- Windows 7 AIK 的使用
- 风电场全场停电事故应急处置方案
- 化工原理选填题题库(下)
- 关于产学研合作教育模式的学习与思考
- 西安先锋公馆项目前期定位报告




