多级队列调度算法和多级反馈队列的调度算法 优缺点

发布网友 发布时间:2022-04-22 07:04

我来回答

1个回答

热心网友 时间:2022-06-17 04:33

#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
int time[3];
struct program { /* 定义进程控制块PCB */
char name[10];
char state;
int queue;//进程队列
int priority; // 数字越小优先级越高
int needtime;//需运行时间
int runtime; //已经运行时间
struct program *link;
}*ready=NULL;
typedef struct program PROGRAM;
PROGRAM *run=NULL,*head1=NULL,*head2=NULL,*head3=NULL,*end1=NULL,*end2=NULL,*end3=NULL;
/*PROCESS *high_priority(PROCESS *P) //选择优先级最高的进程
{
PROCESS *q,*p; //问题,入口形参中
q=p;

while(p->next!=NULL)
{
if(q->priority>p->priority)
q=p;
p=p->next;
}
return q;
}*/

void sort(PROGRAM *p)
{switch(p->queue)
{case 1:
{if(head1==NULL) {head1=p;end1=p;}
else
{end1->link=p;end1=p;p->link=NULL;}
p->state='w';
break;
}
case 2:
{if(head2==NULL)
{head2=p;end2=p;p->state='w';}
else
{end2->link=p;end2=p;p->link=NULL;}
p->state='w';
break;
}
case 3:
{if(head3==NULL)
{head3=p;end3=p;}
else
{end3->link=p;end3=p;p->link=NULL;}
p->state='w';
break;
}
}
}
void input() /* 建立进程控制块函数*/
{ PROGRAM *p;
int i,num;
/*clrscr(); 清屏*/
system("cls");
printf("\n 多级反馈队列调度算法 \n");
printf("\n 请输入进程个数:\n");
scanf("%d",&num);

//printf("\n qname \t state \t queue \t needtime \t runtime \n");
printf("\n 输入第一个进程名:");
ready=getpch(PROGRAM);
scanf("%s",ready->name);
printf("\n 输入第一个进程优先级:");
scanf("%d",&ready->priority);
printf("\n 输入第一个进程运行时间:");

scanf("%d",&ready->needtime);
printf("\n");
ready->runtime=0;ready->state='r';
ready->queue=1;
ready->link=NULL;

for(i=0;i<num-1;i++)
{ printf("\n 进程号No.%d:\n",i+2);
p=getpch(PROGRAM);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先级:");
scanf("%d",&ready->priority);
p->queue=1;
//printf("\n 输入进程队伍数1~3:");
//scanf("%d",&p->queue);
printf("\n 输入进程运行时间:");
scanf("%d",&p->needtime);
printf("\n");
p->runtime=0;p->state='w';
p->link=NULL;
sort(p);
}
}
void disp(PROGRAM * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n qname\t priority\t state\t queue\t needtime\t runtime \n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->priority);
printf("|%c\t",pr->state);
printf("|%d\t",pr->queue);
printf("|%d\t",pr->needtime);
printf("|%d\t",pr->runtime);
printf("\n");
}
void check()//进程查看函数
{PROGRAM *pr;
printf("\n****当前正在运行的进程%s",run->name);
disp(run);
printf("\n****当前正准备好的进程%s",ready->name);
if(ready!=NULL)
{disp(ready);
printf("\n****当前就绪的队列状态为:\n");}
pr=head1;
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
pr=head2;
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
pr=head3;
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
//三个队伍的进程的执行函数
void runpro()
{
run=ready;
if(head1!=NULL)
{ready=head1;head1=head1->link;ready->state='r';}
else
{
if(head2!=NULL)
{ready=head2;head2=head2->link;ready->state='r';}
else
{if(head3!=NULL)
{ready=head3;head3=head3->link;ready->state='r';}
else
ready=NULL;}
}
if(run!=NULL)
{check();//显示运行及等待队列的状况
run->runtime=run->runtime+time[run->queue-1];
if(run->runtime>run->needtime)
run->runtime=run->needtime;
if(run->queue<3)
run->queue+=1;
//disp(run);
if(run->runtime<run->needtime)
sort(run);
}
}
void main() /*主函数*/
{
int h=0;
char ch;
input();
time[0]=1;
time[1]=2;
time[2]=3;

while(ready!=NULL)
{
ch=getchar();
h++;//标志执行的步骤
printf("\n The execute number:%d \n",h);

runpro();
if(run->runtime==run->needtime)
{printf("\n进程%s已经完成\n",run->name);/*run=NULL;*/}
printf("\n 按任一键继续......");
ch=getchar();
}
printf("整个进程运行完毕");
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com