有一个具有两道作业的批处理系统,作业调度采用短作业优先的调度算法,进程调度采用以优先数为基础的抢占式调度算法(进程优先数越小,优先级越高)。
(1)流程图
(2)以下代码是用C语言的,其中有几句输出语句用的是C++的“cout”函数。本代码在VS上亲测可用。
#include #include #include #include using namespace std; typedef struct node { char id; int cputime; int needtime; int priority; }PCB; //定义PCB结构体 #define jobNum 7 //作业数量 #define batch 2 //批处理作业个数 PCB pcb[jobNum]; //pcb数组用来保存所有作业的信息,其大小等于作业数量 PCB memory[2]; //memory数组用来保存被调入到主存中的所有作业信息,其大小等于批处理作业个数 PCB wait[jobNum-batch]; //wait数组用来保存已到达作业,但没能被调入到主存中的作业。其大小等于jobNum-batch int memoryNum = 0; //调入至主存中的作业个数。初始化为0 int waitNum = 0; //等待被调入的作业个数。初始化为0 int MaxTime = 1000; //定义最大的完成时间100 float totalTime = 0; //保存总的周转时间 float totalEndTime = 0; //保存总的结束时间 void sort(PCB *pcb, int count, char key); void process(int currentTmp, int nextTmp); int main(void) { PCB *tmp = (PCB*)malloc(sizeof(PCB)); char id; int cputime, needtime, priority; int i, j; int current, next; printf("输入进程名(char),到达时间(int),所需运行时间(int),优先数(int). 【按到达时间由先到后进行输入】\n"); for (i = 0; iid = id; tmp->cputime = cputime; tmp->needtime = needtime; tmp->priority = priority; pcb[i] = *tmp; getchar(); } for (i = 0; i 1) sort(wait, waitNum, 'N'); while (memoryNum < 2 && waitNum>0) //若当前主存中作业数量小于2,则把当前进程放入到主存中 { memory[memoryNum] = wait[0]; cout << "作业 "<< memory[memoryNum].id <<" 进入内存的时刻是:"<< current<<'\n'; memory[memoryNum].cputime = current; memoryNum++; waitNum--; for (j = 1; j <= waitNum; j++) wait[j - 1] = wait[j]; sort(memory, memoryNum, 'P'); } process(current, next); //调用process时,memoryNum至少为1 }//end for for (i = 0; i < jobNum; i++) { totalTime -= pcb[i].cputime; } //平均周转时间 if (totalEndTime > 0) cout << "\n平均周转时间是:" <<(totalEndTime+totalTime)/jobNum<<"\n\n"; return 0; } void process(int currentTmp, int nextTmp) { int leftTime = memory[0].needtime - nextTmp + currentTmp; //memory[i].needtime-(nextTmp-currentTmp) if (leftTime <= 0) //下一个作业到达时,本作业已运行结束 { cout <<" "<< currentTmp<<" "; //输出不同时刻下内存中的运行作业序列、所有作业进入内存的时刻及结束时刻 currentTmp += memory[0].needtime; //修改当前时间 memoryNum--; memory[0].needtime = 0; cout << memory[0].id << " "; if (memoryNum > 0) { cout << memory[1].id << '\n'; cout << "作业 " << memory[0].id << " 的结束时刻是:" << currentTmp << '\n'; memory[0] = memory[1]; } else cout << "空\n"; totalEndTime += currentTmp; } else //下一个作业到达时,本作业未运行结束 { cout << " "< 0) { memory[1] = wait[0]; waitNum--; if (waitNum > 0) { for (int j = 1; j <= waitNum; j++) wait[j - 1] = wait[j]; } memoryNum++; cout << "作业 "<pcb[a + 1].needtime) { basic = pcb[a]; pcb[a] = pcb[a + 1]; pcb[a + 1] = basic; } } } else { if (count == 2) { for (int a = 0; a < count; a++) for (int b = 1; b < count - a; b++) { if (pcb[a].priority > pcb[a + 1].priority) { basic = pcb[a]; pcb[a] = pcb[a + 1]; pcb[a + 1] = basic; } } } } }/* 测试用例 A 1 4 5 B 3 3 3 C 4 5 4 D 6 2 6 E 6 100 2 F 100 500 1 G 200 100 6 */
处理机调度算法
猜你喜欢
转载自blog.csdn.net/aaricyang/article/details/72551851
今日推荐
周排行