********************************************
*****1.******* 先来先服务算法 **********
*****2.******** 时间片轮转 ************
*****3.******** 短作业优先 ************
*****4.******** 优先级调度 ************
*****5.******** 最短剩余时间 ***********
*****0.**********退出**********************
具体算法的细节想必读者可以从很多地方了解到,下面给出简单的实现代码:
#include<iostream>
using namespace std;
#define MAX_PEOC_ID 65536
#define TIME_SLICE 2
//进程控制块
typedef struct PCB
{
char name[10];//进程名 id
char state; //进程状态 W/R
int ArriveTime; //进程到达时间
int StartTime; //进程开始时间
int FinshTime;//进程结束时间
int ServiceTime;//进程服务时间
float WholeTime; //周转时间
float Weight_WholeTime;//带权周转时间
double Average_WholeTime;//平均周转时间
double Average_Weight_WholeTime;//带权平均周转时间
int RunTime;//已经占用CPU时间
int NeedTime;//还要占用CPU时间
int Prio;//优先级
struct PCB *next;
}pcb;
double Sum_WholeTime=0,Sum_Weight_WholeTime=0;
int time=0;
int Proc_Num=0;
pcb *head = NULL;
pcb *tail=NULL;
void FCFS_RunProccess(pcb *proc)
{
proc->StartTime=time;
cout<<"时刻 "<<time<<" 开始执行当前作业 "<<proc->name<<endl;
time+=proc->ServiceTime;
proc->state='R';
proc->FinshTime=time;
proc->WholeTime=proc->FinshTime-proc->ArriveTime;
proc->Weight_WholeTime=proc->WholeTime/proc->ServiceTime;
Sum_WholeTime+=proc->WholeTime;
Sum_Weight_WholeTime+=proc->Weight_WholeTime;
proc->Average_WholeTime=Sum_WholeTime/Proc_Num;
proc->Average_Weight_WholeTime=Sum_Weight_WholeTime/Proc_Num;
printf(" 到达时间 开始时间 服务时间 完成时间 周转时间 带权周转时间\n");
printf("%6d %6d %6d %6d %8.1f %8.2f\n",
proc->ArriveTime,proc->StartTime,proc->ServiceTime,
proc->FinshTime,proc->WholeTime,proc->Weight_WholeTime);
//printf("共 %d 个进程\n",Proc_Num);
printf(" 平均周转时间 平均带权周转时间 \n");
printf(" %10.2f