📄 mqa.c
字号:
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
#define LNUM 3
#define QNUM 3-Psuper/*队列的优先级由大到小为Queues[0]>Queues[1]>Queues[2],相应的Psuper值为:3,2,1*/
struct pcb { /* 定义进程控制块PCB */
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
struct queue {
struct pcb *LINKS;
int Qsuper;
};
struct queue Queues[LNUM]={NULL,7,NULL,4,NULL,0};/*最高支持队列数目*/
int Psuper=0;/*用于归类进程*/
PCB *m[3]={NULL};
JPsuper()/*判断进程的优先级,并付给Psuper特定的值*/
{
if(p->super<=3) Psuper=3;
if(p->super<=5&&p->super>=4) Psuper=2;
if(p->super>=6) Psuper=1;
}
FcreatQ()/*将进程添加到空队列中*/
{
Queues[QNUM].LINKS=p;
m[QNUM]=p;
}
ScreatQ()/*将进程添加到不为空队列中*/
{
m[QNUM]->link=p;
m[QNUM]=p;
}
CreatP() /*建立进程控制块函数*/
{
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先数(优先数为0~7):");
scanf("%d",&p->super);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='w';
p->link=NULL;
}
SingleMainQ()/*将进程放到相应的队列中*/
{
int i,num;
printf("\n 请输入进程数:");
scanf("%d",&num);
while(num!=0)
{
CreatP();/*创建一个进程函数*/
JPsuper();/*判断进程优先数函数*/
if(Queues[QNUM].LINKS==NULL)
FcreatQ();
else
ScreatQ();
num--;
}
}
destory(PCB* pro)/*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",pro->name);
free(pro);
}
running(PCB *pro) /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(pro->rtime)++;
if(pro->rtime==pro->ntime)
destory(pro); /* 调用destory函数*/
else
{
(pro->super)--;
pro->state='w';
sort(); /*调用sort函数*/
}
}
main() /*主函数*/
{
SingleMainQ();
while(Queues[0].LINKS!=NULL||Queues[1].LINKS!=NULL||Queues[2].LINKS!=NULL)
{
while(Queues[0].LINKS!=NULL)/*运行队列0*/
running(Queues[0].LINKS);
while(Queues[1].LINKS!=NULL)/*运行队列1*/
running(Queues[1].LINKS);
while(Queues[2].LINKS!=NULL)/*运行队列2*/
running(Queues[2].LINKS);
}
printf("\n\n 所有进程已经完成.\n");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -