📄 时间片.~cpp
字号:
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type)(type*)malloc(sizeof(type))
#define NUll 0
struct pcb{ /*定义进程控制块PCB*/
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
sort()/*建立对进程进行优先级排列函数*/
{
PCB *head;
int insert=0;
if(ready==NULL)/*插入队首*/
{
p->link=ready;
ready=p;
}
else/*插入队尾*/
{
head=ready;
while(head->link)
head=head->link;
head->link=p;
p->link=NULL;
insert=1;
}
}
input()/*建立进程控制块函数*/
{
int i,num;
system("cls");
printf("\n请输入进程数量:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n输入进程名:");
scanf("%s",p->name);
printf("\n输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='w';
p->link=NULL;
sort();/*调用sort函数*/
}
}
int space()
{
int l=0;PCB*pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
disp(PCB *pr)/*建立进程现实函数,用于显示当前进程*/
{
printf("\n 进程名\t 状态\t 进程运行总时间\t 距离完成时间 \n");
printf(" |%s \t",pr->name);
printf(" |%c \t",pr->state);
printf(" |%d \t",pr->ntime);
printf(" |%d \t",pr->ntime-pr->rtime-1);
printf("\n");
}
disp(PCB *pr)/*建立进程现实函数,用于显示就绪进程*/
{
printf("\n 进程名\t 状态\t 进程运行总时间\t 距离完成时间 \n");
printf(" |%s \t",pr->name);
printf(" |%c \t",pr->state);
printf(" |%d \t",pr->ntime);
printf(" |%d \t",pr->ntime-pr->rtime);
printf("\n");
}
check()/*建立进程查看函数*/
{
PCB *pr;
printf("\n ****当前正在运行的进程是:%s",p->name);/*显示当前运行的进程*/
disp(p);
pr=ready;
printf("\n****当前就绪队列状态为:\n");/*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
destroy()/*建立进程撤销函数(进程运行结束,撤销进程)*/
{
printf("\n进程[%s]已完成.\n",p->name);
free(p);
}
running()/*建立进程就绪函数(进程运行时间到,置就绪状态)*/
{
(p->rtime)++;
if(p->ntime-p->rtime<=0)
destroy();/*调用destroy函数*/
else
{
(p->super)--;
p->state='w';
sort();/*调用sort函数*/
}
}
main()
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("\n当前运行进程:%d\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n按任意键继续...");
ch=getchar();
}
printf("\n\n 进程已经完成\n");
ch=getchar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -