操作系统实验——处理器调度算法模拟实现与比较
参考文章
1.实验要求
-
选取和设计实现三种以上的处理器调度算法; -
针对特定的处理器调度算法,分析处理器实施进程调度的前提条件和要求(譬如进程创建时刻、运行时间长短、各【集中计算运行/入 输出操作】时间段长短、优先级),并随机发生和模拟处理对应的进程创建及相关事件; -
编程实现处理器调度机制,针对特定的处理器调度算法和随机事件序列,给出相应的调度处理过程,主要涵盖进程相关事件、处理器 调度操作或处理措施以及各状态进程列表; -
测试验证处理器调度机制的有效性及有关处理器调度算法设计方案的正确性。
2.代码实现
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
typedef struct PCB{
char name[20];
int running_time;
int enter_time;
int priority;
int done_time;
int copyRunning_time;
int start_time;
struct PCB* next;
} PCB;
typedef struct PCBQueue{
PCB* firstProg;
PCB* LastProg;
int size;
} PCBQueue;
void Queueinit(PCBQueue* queue){
if(queue==NULL){
return;
}
queue->size = 0;
queue->LastProg = (PCB*)malloc(sizeof(PCB));
queue->firstProg = queue->LastProg;
}
void EnterQueue(PCBQueue* queue,PCB* pro){
queue->LastProg->next = (PCB*)malloc(sizeof(PCB));
queue->LastProg = queue->LastProg->next;
queue->LastProg->enter_time = pro->enter_time;
memcpy(queue->LastProg->name,pro->name,sizeof(pro->name));
queue->LastProg->priority = pro->priority;
queue->LastProg->running_time = pro->running_time;
queue->LastProg->copyRunning_time = pro->copyRunning_time;
queue->LastProg->start_time = pro->start_time;
queue->size++;
}
PCB* poll(PCBQueue* queue){
PCB* temp = queue->firstProg->next;
if(temp == queue->LastProg){
queue->LastProg=queue->firstProg;
queue->size--;
return temp;
}
queue->firstProg->next = queue->firstProg->next->next;
queue->size--;
return temp;
}
void inputPCB(PCB pro[],int num){
for(int i=0;i<num;i++){
PCB prog ;
printf("请输入第%d个进程的名字,到达时间 ,服务时间,优先级\n",i+1);
scanf("%s",prog.name);
scanf("%d",&prog.enter_time) ;
scanf("%d",&prog.running_time);
prog.copyRunning_time = prog.running_time;
scanf("%d",&prog.priority);
pro[i]=prog;
}
}
void sortWithEnterTime(PCB pro[],int num){
for(int i=1;i<num;i++){
for(int j= 0;j<num-i;j++){
if(pro[j].enter_time>pro[j+1].enter_time){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
void FCFS(PCB pro[],int num){
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));
Queueinit(queue);
EnterQueue(queue,&pro[0]);
int time = pro[0].enter_time;
int pronum=1;
float sum_T_time = 0;
float sum_QT_time = 0 ;
while(queue->size>0){
PCB* curpro = poll(queue);
if(time < curpro->enter_time)
time = curpro->enter_time;
int done_time = time+curpro->running_time;
int T_time = done_time - curpro->enter_time;
sum_T_time += T_time;
float QT_time = T_time / (curpro->running_time+0.0) ;
sum_QT_time += QT_time;
for(int tt = time;tt<=done_time && pronum<num;tt++){
if(tt >= pro[pronum].enter_time){
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
printf("%s\t%d\t%d\t%d\t%d\t%d\t%.2f\n",curpro->name,curpro->enter_time,curpro->running_time,time,done_time
,T_time,QT_time);
time += curpro->running_time;
if(queue->size==0 && pronum < num){
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
printf("平均周转时间为%.2f\t平均带权周转时间为%.2f\n",sum_T_time/(num+0.0),sum_QT_time/(num+0.0));
}
void sortWithLongth(PCB pro[],int start,int end){
int len = end - start;
if(len == 1)
return;
for(int i=1;i<len;i++){
for(int j= start;j<end-i;j++){
if(pro[j].running_time>pro[j+1].running_time){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
void SJF(PCB pro[],int num) {
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
Queueinit(queue);
EnterQueue(queue,&pro[0]);
int time = pro[0].enter_time;
int pronum=1;
float sum_T_time = 0,sum_QT_time = 0;
while(queue->size>0){
PCB* curpro = poll(queue);
if(time < curpro->enter_time)
time = curpro->enter_time;
int done_time = time+curpro->running_time;
int T_time = done_time - curpro->enter_time;
float QT_time = T_time / (curpro->running_time+0.0) ;
sum_T_time += T_time;
sum_QT_time += QT_time;
int pre = pronum;
for(int tt = time;tt<=done_time&&pronum<num;tt++){
if(tt>=pro[pronum].enter_time){
pronum++;
}
}
sortWithLongth(pro,pre,pronum);
for(int i=pre;i<pronum;i++){
EnterQueue(queue,&pro[i]);
}
pre = pronum;
printf("%s\t%d\t%d\t%d\t%d\t%d\t%.2f\n",curpro->name,curpro->enter_time,curpro->running_time,time,done_time
,T_time,QT_time);
time += curpro->running_time;
if(queue->size==0&&pronum<num){
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
printf("平均周转时间为%.2f\t平均带权周转时间为%.2f\n",sum_T_time/(num+0.0),sum_QT_time/num);
}
void sortWithResponse(PCB pro[],int start,int end){
int len = end - start;
if(len == 1)
return;
for(int i=1;i<len;i++){
for(int j= start;j<end-i;j++){
float m = (pro[j].start_time-pro[j].enter_time+pro[j].running_time)/(pro[j].running_time+0.0);
float n = (pro[j+1].start_time-pro[j+1].enter_time+pro[j+1].running_time)/(pro[j+1].running_time+0.0);
if(m < n){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
void HRRN(PCB pro[],int num) {
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
Queueinit(queue);
EnterQueue(queue,&pro[0]);
int time = pro[0].enter_time;
int pronum=1;
float sum_T_time = 0,sum_QT_time = 0;
while(queue->size>0){
PCB* curpro = poll(queue);
if(time < curpro->enter_time)
time = curpro->enter_time;
int done_time = time+curpro->running_time;
int T_time = done_time - curpro->enter_time;
float QT_time = T_time / (curpro->running_time+0.0) ;
sum_T_time += T_time;
sum_QT_time += QT_time;
int pre = pronum;
for(int tt = time;tt<=done_time&&pronum<num;tt++){
if(tt>=pro[pronum].enter_time){
pronum++;
}
}
sortWithResponse(pro,pre,pronum);
for(int i=pre;i<pronum;i++){
EnterQueue(queue,&pro[i]);
}
pre = pronum;
printf("%s\t%d\t%d\t%d\t%d\t%d\t%.2f\n",curpro->name,curpro->enter_time,curpro->running_time,time,done_time
,T_time,QT_time);
time += curpro->running_time;
if(queue->size==0&&pronum<num){
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
printf("平均周转时间为%.2f\t平均带权周转时间为%.2f\n",sum_T_time/(num+0.0),sum_QT_time/num);
}
void sortWithPriority(PCB pro[],int start,int end){
int len = end - start;
if(len == 1) return ;
for(int i=1;i<len;i++){
for(int j= start;j<end-i;j++){
if(pro[j].priority>pro[j+1].priority){
PCB temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
}
void HPF(PCB pro[],int num){
printf("进程 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
Queueinit(queue);
EnterQueue(queue,&pro[0]);
int time = pro[0].enter_time;
int pronum=1;
float sum_T_time = 0,sum_QT_time = 0;
while(queue->size>0){
PCB* curpro = poll(queue);
if(time<curpro->enter_time)
time = curpro->enter_time;
int done_time = time+curpro->running_time;
int T_time = done_time - curpro->enter_time;
float QT_time = T_time / (curpro->running_time+0.0) ;
sum_T_time += T_time;
sum_QT_time += QT_time;
int pre = pronum;
for(int tt = time;tt<=done_time&&pronum<num;tt++){
if(tt>=pro[pronum].enter_time){
pronum++;
}
}
sortWithPriority(pro,pre,pronum);
for(int i=pre;i<pronum;i++){
EnterQueue(queue,&pro[i]);
}
pre = pronum;
printf("%s\t%d\t%d\t%d\t%d\t%d\t%.2f\n",curpro->name,curpro->enter_time,curpro->running_time,time,done_time
,T_time,QT_time);
time += curpro->running_time;
if(queue->size==0&&pronum<num){
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
printf("平均周转时间为%.2f\t平均带权周转时间为%.2f\n",sum_T_time/(num+0.0),sum_QT_time/(num+0.0));
}
void RR(PCB pro[],int num){
printf("请输入时间片大小");
int timeslice;
scanf("%d",×lice);
printf("进程 到达时间 服务时间 进入时间 完成时间 周转时间 带权周转时间\n");
sortWithEnterTime(pro,num);
PCBQueue* queue = (PCBQueue*)malloc(sizeof(PCBQueue));;
Queueinit(queue);
pro[0].start_time = pro[0].enter_time;
EnterQueue(queue,&pro[0]);
int time = 0;
int pronum = 1;
float sum_T_time = 0,sum_QT_time = 0;
while(queue->size>0){
PCB* curpro = poll(queue);
if(time<curpro->enter_time)
time = curpro->enter_time;
if(timeslice >= curpro->running_time){
for(int tt = time;tt<=time+curpro->running_time&&pronum<num;tt++){
if(tt>=pro[pronum].enter_time){
pro[pronum].start_time = tt;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
time += curpro->running_time;
curpro->running_time = 0;
curpro->done_time = time;
int T_time = curpro->done_time-curpro->start_time;
float QT_time = T_time / (curpro->copyRunning_time+0.0);
sum_T_time += T_time;
sum_QT_time += QT_time;
printf("%s\t%d\t%d\t %d\t %d\t %d\t %.2f\n",curpro->name,curpro->enter_time,curpro->copyRunning_time,
curpro->start_time,curpro->done_time,T_time,QT_time);
if(queue->size==0&&pronum<num){
pro[pronum].start_time = pro[pronum].enter_time;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
continue;
}
for(int tt = time;tt<=time+timeslice&&pronum<num;tt++){
if(tt>=pro[pronum].enter_time){
pro[pronum].start_time = tt;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
time += timeslice;
curpro->running_time -= timeslice;
EnterQueue(queue,curpro);
if(queue->size==0&&pronum<num){
pro[pronum].start_time = pro[pronum].enter_time;
EnterQueue(queue,&pro[pronum]);
pronum++;
}
}
printf("平均周转时间为%.2f\t平均带权周转时间为%.2f\n\n",sum_T_time/(num+0.0),sum_QT_time/(num+0.0));
}
void choiceMenu(){
printf("请选择进程调度算法:\n\n");
printf("1.先来先服务算法\n2.短进程优先算法\n3.高优先级优先\n4.时间片轮转算法\n5.高响应比优先算法\n6.退出\n");
}
void menu(){
int proNum;
printf("请输入进程的个数:");
scanf("%d",&proNum);
PCB pro[proNum];
inputPCB(pro,proNum);
choiceMenu();
int choice;
while(1){
scanf("%d",&choice);
switch(choice){
case 1:FCFS(pro,proNum);choiceMenu();break;
case 2:SJF(pro,proNum);choiceMenu();break;
case 3:HPF(pro,proNum);choiceMenu();break;
case 4:RR(pro,proNum);choiceMenu();break;
case 5:HRRN(pro,proNum);choiceMenu();break;
case 6:return;
}
}
}
int main(){
menu();
return 0;
}
3.运行结果
-
高优先级优先算法和时间片轮转算法 -
高响应比优先算法
|