#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
struct pcb {
char name[10];
char state;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
sort()
{
PCB *first, *second;
if(ready==NULL)
{
p->link=ready;
ready=p;
}
else
{
first=ready;
second=first->link;
while(second!=NULL)
{
first=first->link;
second=second->link;
}
first->link=p;
}
}
input()
{
int i,num;
printf("\n 请输入进程个数?");
scanf("%d",&num);
for(i=1;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();
}
}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
disp(PCB * pr)
{
printf("\n qname \t state \t ndtime \t runtime \n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
output()
{
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);
}
fcfs()
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();
else
{
p->state='w';
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';
output();
fcfs();
printf("\n 按任意键继续......");
ch=getchar();
}
printf("\n\n 进程已经完成.\n");
ch=getchar();
}
shijianpian.zip_shijianpian
版权申诉
8 浏览量
2022-09-24
00:10:20
上传
评论
收藏 1KB ZIP 举报
钱亚锋
- 粉丝: 90
- 资源: 1万+