📄 pcd.c
字号:
//进程调度-----优先级算法源程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct node //创建PCB
{
char name[10]; //进程标识
int prio; //进程优先数
int cputime; //进程占用CPU时间
int needtime; //进程完成所需时间
int count; //计数器
char state; //进程的状态
struct node *next; //链指针
}PCB;
PCB *finish,*ready,*tail,*run;
int N;
firstin() //创建就绪队列对头指针
{
run=ready;
run->state='R';
ready=ready->next;
}
void prt(char algo) //演示进程调度
{
PCB *p;
printf(" NAME CPUTIME NEEDTIME PRIORITY STATUS\n");
if(run!=NULL)
printf(" %-10s%-10d%-10d%-10d %c\n",run->name,
run->cputime,run->needtime,run->prio,run->state);
p=ready;
while(p!=NULL)
{ printf(" %-10s%-10d%-10d%-10d %c\n",p->name,
p->cputime,p->needtime,p->prio,p->state);
p=p->next;
}
p=finish;
while(p!=NULL)
{ printf(" %-10s%-10d%-10d%-10d %c\n",p->name,
p->cputime,p->needtime,p->prio,p->state);
p=p->next;
}
printf("************************************************\n");
getch();
}
insert(PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q;
p1=ready;
r=p1;
b=1;
while((p1!=NULL)&&b)
if(p1->prio>=s->prio)
{
r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1)
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1;
ready=s;
}
}
void create(char alg) //创建各个进程
{
PCB *p;
int i,time,priority;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
for(i=1;i<=N;i++)
{
p=malloc(sizeof(PCB));
printf("\nEnter NAME of the NO.%d process:\n",i);
scanf("%s",na);
loop1:
printf("Enter TIME of process(less than 10 is better):\n");
scanf("%d",&time);
if(time<=0)
{
printf("Time can't below zero,Please Enter AGain:\n");//时间不能小于或等于零
goto loop1;
}
printf("Enter PRIORITY of process:\n");
scanf("%d",&priority);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='R'; //R为就绪状态
p->prio=priority;
if(ready!=NULL)
insert(p);
else
{
p->next=ready;
ready=p;
}
}
printf(" DISPLAY OF THE PROGRESS:\n");
printf("************************************************\n");
prt(alg);
run=ready;
ready=ready->next;
run->state='W'; //除就绪和结束状态的其它状态
}
priority(char alg) //优先级算法调度
{
while(run!=NULL&&run->prio>=0)
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-1;
if(run->needtime==0)
{
run->next=finish;
finish=run;
run->state='E'; //E为结束状态
run=NULL;
if(ready!=NULL)
firstin();
}
else
if((ready!=NULL)&&(run->prio<ready->prio))
{
run->state='R';
insert(run);
firstin(); //创建就绪队列对头指针
}
prt(alg); //演示进程调度
}
}
main() //主函数
{
char algo;
printf("\n\n");
printf("\t\t\t******模拟进程调度程序演示******\n");
loop:
printf("****Enter THE TOTAL NUMBER of PCB(less than 10):\n");
scanf("%d",&N);
if(N<=0||N>10)
{
printf("Your input NUMBER is too big or less than 1.\n");
printf("Enter AGAIN:\n\n");
goto loop;
}
create(algo);
priority(algo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -