用C语言实现对N个进程的简单时间片轮转法Round Robin的调度模拟
来源:互联网 发布:nodejs解析json.parse 编辑:程序博客网 时间:2024/05/17 07:39
小弟初出茅庐,菜鸟一个,各路大神请多指教。这是实验课题目,上课时写的,不是很完整,仅当留着做个纪念,有问题大家一起学习讨论。
第一次发博客,哈哈,挺乐的。废话不多说,直接上代码!
/*****时间片轮转法进行CPU调度算法********/#include<stdio.h>#include<malloc.h>#include<string.h>#define N 10 //定义最大进程数#define TIME 2//定义时间片大小typedef struct pcb{char id[10];//进程标识数int arrivetime;//到达时间int runtime;//进程已经占用的cpu时间int needtime;//进程还需要的时间char state[12];//进程运行状态:wait or runingstruct pcb *next;}pcb,*PCB;PCB head;//设置全局变量用来修改就绪队列PCB tail;int count=0;//记录就绪队列中进程数void CreatProcess(){//创建进程PCB p,q;//进程的头尾指针都有int num;//记录要创建的进程数int i,j;int arrive[N];head=tail=(PCB)malloc(sizeof(pcb));head->next=NULL;p=head;printf("输入你要创建的进程数:");scanf("%d",&num);count=num;printf("********按照进程到达时间从小到大创建就绪队列******\n"); //初始对其排序来创建就绪队列for(i=1;i<=num;i++){p->next=(PCB)malloc(sizeof(pcb));p=p->next;tail=p;printf("输入进程%d的标示符:",i);scanf("%s",p->id);printf("输入进程%d的到达时间:",i);scanf("%d",&p->arrivetime);printf("输入进程%d已占用的cpu时间:",i);scanf("%d",&p->runtime);printf("输入进程%d还需要的cpu时间:",i);scanf("%d",&p->needtime);printf("输入进程%d当前状态:(run 或者wait):",i);scanf("%s",p->state);}tail->next=p->next=NULL;}void RR_RunProcess(){//运行进程,简单轮转法Round RobinPCB p,q,temp;p=head->next;while(1){if(head->next==NULL){printf("此时就绪队列中已无进程!\n");return ;}else {while(p){if((p->needtime>0)&&!(strcmp(p->state,"wait"))){printf("进程%s开始,\n",p->id );strcpy(p->state,"run");p->runtime+=TIME;p->needtime-=TIME;if(p->needtime<0)p->needtime=0;} temp=p;//把该时间片内运行完的进程存到临时temp中 //把temp接到链表尾部,销毁P; if(temp->needtime>0){//把该时间片内运行完的进程接到就绪队列的尾部 if(count>1){ head->next=temp->next; tail->next=temp; tail=tail->next; strcpy(tail->state,"wait"); tail->next=NULL; } else if(count==1){//当只有一个进程等待时,分开讨论 head->next=temp; tail=temp; strcpy(tail->state,"wait"); tail->next=NULL; } } if(temp->needtime==0){//销毁就绪队列中已经结束的进程 count--;//此时就绪队列中进程数减1 printf("进程%s结束.\n",p->id); head->next=temp->next; free(temp);//撤销就绪队列中已经结束的进程 }p=head->next;}}}}void main(){printf("**************进程的初始状态!**************\n"); CreatProcess(); printf("*******************************************\n\t\t程序运行结果如下:\n\n"); printf("*******************************************\n"); RR_RunProcess();//简单轮转法Round Robin }//运行结果如下附件:
0 0
- 用C语言实现对N个进程的简单时间片轮转法Round Robin的调度模拟
- 时间片轮转调度算法的C语言模拟实现
- 进程调度算法模拟,用动态优先数及时间片轮转法实现进程调度_Java语言模拟实现
- 进程调度-时间片轮转法(Java简单实现)
- 时间片轮转算法和优先级调度算法 C语言模拟实现
- 时间片轮转算法和优先级调度算法 C语言模拟实现
- 模拟时间片轮转的JavaScript实现
- C++实现操作系统进程调度(时间片轮转法)
- 基于mykernel的时间片轮转调度的实现
- 基于mykernel实现的时间片轮转调度代码分析
- 进程调度:时间片轮转调度算法
- CPU进程调度简单模拟(Round Robin)--Java
- 操作系统 进程调度 基于时间片轮转(C实现 Visual Stdio 2005)
- 操作系统进程调度模拟程序 基于优先级调度和时间片轮转调度算法
- 操作系统时间片轮转法进程CPU调度
- 时间片轮转进程调度算法
- 进程调度-时间片轮转算法
- 进程调度—时间片轮转
- Customs and history of the origin of the Mid-Autum
- 软件工程(四)——生存周期
- South Korean garment industry demands approaching
- Venture breakfast shop open election Which Xi'an s
- 长按POWER键, 一般会弹出关机选择界面, 提示关机或切换模式
- 用C语言实现对N个进程的简单时间片轮转法Round Robin的调度模拟
- Gao Hua How the Red Sun rises Chapter IX
- Java中request获取IP
- (四)Z-Stack 软件架构分析
- [Designer develop a plan special courses brochures
- Most of the opportunity to have died in someone el
- LeetCode Subsets
- Redis在linux上的安装过程 和遇到的问题
- .NET2.0程序集无法在.net 4.0 中运行的解决方案