标签:
设计一个允许n个进程并发运行的进程管理模拟系统。该系统包括有简单的进程控制、同步与通讯机构,其进程调度算法可任意选择(优先级调度,时间片轮转,短进程优先中的一种)。每个进程用一个PCB表示,其内容根据具体情况设置。各进程之间有一定的同步关系(可选)。系统在运行过程中应能显示或打印各进程的状态及有关参数的变化情况,以便观察诸进程的运行过程及系统的管理过程。
(1)PCB进程控制块
其中包括参数①进程名name;②要求运行时间 runtime;③优先级 prior;④状态 state;⑤已运行时间runedtime等。
(2)为简单起见,只设运行队列,就绪链表,阻塞队列三种数据结构,进程的调度在这两个队列中切换,如图1.1所示
图1.2 进程管理流程图
#include "stdlib.h" #include<stdio.h> #include<iostream> #include<time.h> using namespace std; typedef struct PCB { int name; int runtime; int runedtime; int state; int killtime; int waitpoint; int waittime; struct PCB *next; }PCB; #define NUM 10 PCB *runqueue=NULL; //运行队列 PCB *top=NULL,*tail=NULL,*temp,*temp_pre; //就绪队列 PCB *top_wait=NULL,*tail_wait=NULL; int timeslice=2; void CreateProcess() { int i; srand(10); 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("process 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); } } void ProcessSchedule() { int killtime_temp=0; while(top!=NULL||top_wait!=NULL) { if(top)//进行调度 { //从就绪队列选一个节点,插入运行队列 runqueue=top; top=top->next; runqueue->next=NULL; runqueue->waitpoint=runqueue->waitpoint-timeslice; if(runqueue->waitpoint>0) //不产生阻塞 { runqueue->runtime= runqueue->runtime-timeslice; if(runqueue->runtime<=0) { //进程将运行结束 printf("进程 %d 占用处理机,将销毁\n",runqueue->name); runqueue->killtime=runqueue->runtime+timeslice; //该进程占用处理机的时间 runqueue->runedtime=runqueue->runedtime+runqueue->killtime; //该进程以总共使用处理机的时间 killtime_temp=runqueue->killtime; runqueue->runtime=0; //该进程还需要占用处理机的时间 printf("process name %d, runtime=%d, runedtime=%d,killtime=%d, waitpoint=%d,waittime=%d\n" ,runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime,runqueue->waitpoint,runqueue->waittime); //结束该进程,释放该节点空间 //.................. } else { printf("进程%d 占用处理机\n",runqueue->name); runqueue->killtime=timeslice; killtime_temp=runqueue->killtime; runqueue->runedtime=runqueue->runedtime+runqueue->killtime; printf("process name %d, runtime=%d, runedtime=%d,killtime=%d, waitpoint=%d,waittime=%d\n" ,runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime,runqueue->waitpoint,runqueue->waittime); //将运行队列插入到就绪队列 tail->next=runqueue; tail=tail->next; } } else { printf("进程%d 产生堵塞!将在%ds后唤醒!\n",runqueue->name,runqueue->waittime); runqueue->killtime=runqueue->waitpoint+timeslice; runqueue->runedtime=runqueue->runedtime+runqueue->killtime; runqueue->runtime=runqueue->runtime-runqueue->killtime; runqueue->waitpoint=100; //将运行队列查到阻塞队列 if(tail_wait==NULL) { printf("阻塞队列为空!\n") ; tail_wait=runqueue; top_wait=runqueue; } else { tail_wait->next=runqueue; tail_wait=tail_wait->next; } } runqueue=NULL; } if(top_wait!=NULL) { temp=top_wait; temp_pre=NULL; while(temp!=NULL) //扫描阻塞队列,是否存在等待时间已到的节点 { temp->waittime=temp->waittime-killtime_temp; if(temp->waittime<=0) { printf("进程%d 已被唤醒,加入就绪队列!\n",temp->name); //节点从就绪队列删除,插入就绪队列 tail->next=temp; tail=tail->next; if(temp_pre) { temp_pre->next=temp->next; // free(temp); temp=temp_pre->next; } else { //temp 是top ,temp_pre=NULL top_wait=temp->next; //free(temp); temp=top_wait; if(tail_wait!=NULL) printf("堵塞队列对头%d\n",tail_wait->name); else printf("堵塞队列为空\n"); } if(temp==NULL) tail_wait=NULL; } else { temp_pre=temp; temp=temp->next; } } } if((top==NULL)&&(top_wait!=NULL)) { temp=top_wait; while(temp!=NULL) //扫描阻塞队列,是否存在等待时间已到的节点 { temp->waittime-10; temp=temp->next; } } } } int main(int argc, char* argv[]) { CreateProcess(); ProcessSchedule(); printf("Hello World!\n"); return 0; }
标签:
原文地址:http://blog.csdn.net/zuichudeheart/article/details/42934721