```c++
```#include <stdio.h>
#include <stdlib.h>
#define getpch(type) (type*)malloc(sizeof(type))
struct pcd{
char name[10];
char state;
int nice,ntime,rtime;
struct pcd* link;
struct pcd* p;
struct pcd* ready;
}*ready=NULL,*p;
typedef struct pcd PCB;
char sort();
char input();
char disp();
char destroy();
int spare();
int main(){
//input();
int len,h=0;
//int *ready;
char ch;
len=spare();
while((len!=0)&&(ready!=NULL)){
h++;
ch=getchar();
printf("\n the execute number:%d\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
char check();
char running();
printf("\n按任一键继续");
ch=getchar();
}
printf("\n\n所有进程已经运行完成!\n");
ch=getchar();
return 0;
}
char input(){
int i,num;
printf("请输入被调度的进程数目:");
scanf("%d",&num);
for(i=0;i<num;i++){
printf("\n进程号NO.%d:",i);
p=getpch(PCB);
printf("\n输入进程名:");
scanf("%s",p->name);
printf("输入进程优先数:");
scanf("%d",&p->nice);
printf("输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;
p->state='w';
p->link=NULL;
sort();
}
}
char sort(){
PCB* first, *second;
int insert=0;
if((ready==NULL)||((p->nice)>(ready->nice))){
p->link=ready;
ready=p;
}
else{
first=ready;
second=first->link;
while(second!=NULL){
if((p->nice)>(second->nice)){
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else{
first=first->link;
second=second->link;
}
}
if(insert==0)
first->link=p;
}
}
int space(){
int l=0;
PCB* pr=ready;
while(pr!=NULL){
l++;
pr=pr->link;
}
return(1);
}
char check(){
PCB* pr;
printf("\n当前正在运行的进程是:%s",p->name);
disp(p);
pr=ready;
if(pr!=NULL)
printf("\n当前就绪队列状态为:");
else
printf("\n当前就绪队列状态为:\n");
while(pr!=NULL){
disp(pr);
pr=pr->link;
}
}
char disp(PCB* pr){
printf("\n qname \t state \t nice \t ndtime \t runtime \n");
printf("%s\t",pr->name);
printf("%c\t",pr->state);
printf("%d\t",pr->nice);
printf("%d\t",pr->ntime);
printf("%d\t",pr->rtime);
printf("\n");
}
char destroy(){
printf("进程[%s]已完成。\n",p->name);
free(p);
}
char running(){
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();
else{
(p->nice)--;
p->state='W';
sort();
}
【抱拳】