1、 Process head; /队列头指针 Process end; /队列尾指针Process_Queue; /进程队列Process_Queue PQ; /定义一个全局队列变量int t; /全局时间Process Run_Now; /当前正在运行的进程,作为全局变量void InitQueue(Process_Queue PQ) PQ.head -next = NULL; PQ.end -next = PQ.head;/*初始化队列*/int IsEmpty(Process_Queue PQ) if(PQ.end-next = PQ.head) return 1; /队列空的条件为头指针
2、指向尾指针并且尾指针指向头指针 else return 0;/*判定队列是否为空队列*/void EnQueue(Process_Queue PQ,Process P) Process temp =(Process)malloc(sizeof(_Block); temp = PQ.end; temp-next-next = P; PQ.end-/*插入队列操作*/Process DeQueue(Process_Queue PQ) if(IsEmpty(PQ) return NULL; Process temp = PQ.head-next; PQ.head-next= temp -next =
3、 temp) PQ.end- return temp;/*出列操作*/Process ShortestProcess(Process_Queue PQ) if(IsEmpty(PQ) /如果队列为空,返回 if(!Run_Now) return NULL; else return Run_Now; Process temp,shortest,prev; int min_time; if(Run_Now) /如果当前有进程正在执行, shortest = Run_Now; /那么最短进程初始化为当前正在执行的进程, min_time = Run_Now-PB.remain_time; else
4、/如果当前没有进程执行, shortest = PQ.head- /则最短进程初始化为队列中第一个进程 min_time = PQ.head- temp = PQ.head; prev = temp; while(temp-next) if(temp-PB.remain_time /则保存当前进程, min_time = shortest- prev=temp; /及其前驱 temp=temp- if(shortest = PQ.end-next) /如果最短剩余时间进程是队列中最后一个进程, PQ.end-next = prev; /则需要修改尾指针指向其前驱 prev-next = sho
5、rtest- /修改指针将最短剩余时间进程插入到队头 return shortest;/*调度最短剩余时间的进程至队头*/void Run() Run_Now-PB.remain_time-; /某一时间运行它的剩余时间减 return;/*运行函数*/void Wait() return ;int sum(int array,int n) int i,sum=0; for(i=0;iPB.number =i+1;next =NULL; wti =0; circle_ti =0; printf(输入第%d个进程的到达时间及剩余执行时间:,i+1); scanf(%d %dPi-PB.arriv
6、e_time,&PB.remain_time); Total_Time+=Pi-n进程按顺序运行依次为: i=0; int k=0; for(t=0;t+) if(Run_Now) /如果当前有进程正在执行 Run(); if(t = Pi-PB.arrive_time) /如果当前时间正好有进程进入 if(Pi-PB.remain_time) temp = Pi; Pi = Run_Now; Run_Now = temp; /则调度它至运行队列中, Run_Now-PB.Tp=t;PB.Tc=t; wtRun_Now-PB.number-1+=Run_Now-PB.Tc-Run_Now-PB
7、.Tp; printf(%d ,Run_Now-PB.number); EnQueue(PQ,Pi); /并将当前运行进程重新插入队列中 Pi- k+; i=(i+1)(N-1)?(N-1):(i+1); if(Run_Now-PB.remain_time = 0) /如果当前进程运行结束, Run_Now-PB.To=t; /进程运行结束的时间 circle_tRun_Now-PB.number-1 +=t-Run_Now-PB.arrive_time; free(Run_Now); /则将它所占资源释放掉, Run_Now =NULL; /并修改Run_Now为NULL Run_Now =
8、 ShortestProcess(PQ); /从就绪队列中调出最短剩余时间进程至队头, if(!Run_Now) /如果队列为空,转为等待状态 if(IsEmpty(PQ) & k = N) break; Wait(); continue; else else /如果当前运行进程为空,那么PB.arrive_time) /如果正好这时有进程入队 Run_Now = DeQueue(PQ); /则直接被调入运行队列中 printf( else Wait(); continue; 平均等待时间是:n%fn,(float)sum(wt,N)/N);平均周转时间是:,(float)sum(circle
9、_t,N)/N); return 0;/【Process.cpp代码如下:#includestringusing namespace std;class Processpublic:string ProcessName; / 进程名字 int Time; / 进程需要时间 int leval; / 进程优先级 int LeftTime; / 进程运行一段时间后还需要的时间;void Copy ( Process proc1, Process proc2); / 把proc2赋值给proc1void Sort( Process pr, int size) ; / 此排序后按优先级从大到小排列vo
10、id sort1(Process pr, int size) ; / 此排序后按需要的cpu时间从小到大排列void Fcfs( Process pr, int num, int Timepice); / 先来先服务算法void TimeTurn( Process process, int num, int Timepice); / 时间片轮转算法void Priority( Process process, int num, int Timepice); / 优先级算法void main() int a; couta;const int Size =30; Process processSi
11、ze ; int num; int TimePice;cout 输入进程个数:num; 输入此进程时间片大小: TimePice;for( int i=0; i num; i+) string name; int CpuTime; int Leval; 输入第 i+1Leval; processi.ProcessName =name; processi.Time =CpuTime; processi.leval =Leval;for ( int k=0;kk+) processk.LeftTime=processk.Time ;/对进程剩余时间初始化 cout0 & temp.levalsiz
12、e/2;d-) temp=pr d; pr d = pr size-d-1; pr size-d-1=temp; / 此排序后按优先级从大到小排列/* 最短作业优先算法的实现*/void sort1 ( Process pr, int size) / 以进程时间从低到高排序 temp.Time prj-1.Time ) /* 先来先服务算法的实现*/void Fcfs( Process process, int num, int Timepice) / process 是输入的进程,num是进程的数目,Timepice是时间片大小while(true) if(num=0) 所有进程都已经执行完
13、毕! exit(1); if(process0.LeftTime=0) 进程process0.ProcessName 已经执行完毕! for (int i=0; processi=processi+1; num-; else if(processnum-1.LeftTime=0)processnum-1.ProcessName /输出正在运行的进程 process0.LeftTime=process0.LeftTime- Timepice; process0.leval =process0.leval-1;process0.ProcessName process0.Time process0.
14、LeftTime process0.leval 运行 for(int s=1;ss+)processs.ProcessName processs.Time processs.LeftTime processs.leval 等待 ; / else system( pause / while /* 时间片轮转调度算法实现*/void TimeTurn( Process process, int num, int Timepice) if( processnum-1.LeftTime =0 ) processnum-1.ProcessName 0)processs.leval; if(s=1) 就绪 temp = process0; for( int j=0;jj+) processj = processj+1; processnum-1 = temp; / else / while /* 优先级调度算法的实现*/void Priority( Proces
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1