#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool. h>
typedef struct node{
char nane[10] ;
int prio;
int round;
int cputime;
int needtine;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*run,*r;
int N;
void firstin()
run = ready;
run->state' = 'R';
ready = ready->next;
void prt1(char a)
if(a=='P'l|a == 'p')
printf("Process-No.
CPU-Tine
Running-Time
Priority-No.
Status\n");
else if(a =='r'lla =='R'.
printf("Process-No..
CPU-Tine
Running- Time
Counting
Time-slice Status\n");
else if(a=='f'|la=='F')
printf("Process-No.
Running-Time
Status\n");|
void prt2(char a ,PCB *q)
if(a=='P'lla == 'p')
printf("%- 10s%- 10d%- 10d%- 10d%c\n" ,9->nane ,q->cputime ,q->needtine ,9->prio ,9->state);
else lf(a == 'r'll a == 'R')
printf("%- 10s%- 10d%- 10d%- 10d%- 10d%c\n" ,9->nane ,9->cputine ,9- >needtine ,9->count ,9->round,q->state);
else if(a=='f'lla=='F')
printf("%- 10s%- 10d%c\n" ,9->nane ,9- >needtine ,9->state);
vold prt(char algo)
PCB *P;
prt1(aigo);
tf(run != NULL& !(algo == 'r'llalgo =='R))//这个对RR不适应! !
prt2(algo,run);
p. = ready;
whtle(p 1= NULL)
prt2(algo,p);
p = p->next;
p = finish;
whtile(p != NULL)
prt2(algo,p);
P = p->next;
getchar();
void insert1(PCB *q)
PCB *p1,*s,*r;
int b;
s=q
p1 = ready;
「=P1;
whtle((p1 != NULL)&&b)
if(p1->prio >= s->prio)
r=p1;
P1 = p1->next;
elseb=0;
if(r!= p1)
r->next = s;
s->next = p1;
else
s->next = p1;
ready = s;
}
]
void creat1(char alg)
PCB *p;
int i,time;
char na[10] ;
ready = NULL ;
finish = NULL;
run = NULL;
printf("Enter the Process-No. and Running-Time:\n");
for(i=1;i<=N;t++)
P = (PCB *)nalloc(sizeof(PCB));
scanf("%s" ,na);
scanf("%d" ,&tine);
strcpy(p->name, ,na);
p->cputime = 0;
p->needtime = time;
p->state = 'W';
p->prio = 50-time;
if(ready != NULL)
insert1(p);
else{
p->next = ready;
ready = P;
//clrscr();
if(alg =='p'Ilalg ='P')
printf("Priority nunber algorithm output info:\n");
else
printf("FCFS algorithm output info:\n");
prt(alg);
run = ready;
ready = ready->next;
run->state = 'R';
void creat2(char alg)
{
PCB *p;
int i, tine , round;
char na[10];
ready = NULL;
finish = NULL;
un = NULL;
printf("Enter the. tine-slice:");
scanf("%d" , &round);
printf("Enter the Process-No. and Running-Tine:\n");
for(i=1;i<=N;i++)
p = (PCB *)malloc(sizeof(PCB));
scanf("%s" ,na);
scanf("%d" ,&time);
strcpy(p->nane,na);
p->cputime = 0;
p->needtime = tinme;
p->state = 'W' ;
p->count
=0
p->round = round ; .
p->next = NULL;
if(i == 1)/*按顺序插入到ready链表中*/
r_=ready=p;
else
r->next = p;
「=p;
//clrscr();
printf("RR algorithm output info:\n");
prt(alg);
}
void priority(char alg) //优先数调度算法
{
while(run != NULL)
run->cputine += 1;
run->needtine -= 1 ;
run->prio -= 3;
if(run->needtime == 0)
{
run->next = finish;
finish = run;
run->state = ' F ' ;
run = NULL;
if(ready != NULL)
firstin();
}
else .
if((ready != NULL) && (run->prio < ready->prio))
{
run->state = 'W';
insert1(run);
firstin();
prt(alg);
}
void roundrun(char alg)
//时间片轮转调度算法
bool flag;//当ready列里只有 -个时做标记
while(N){
flag = 1;//初始化为1
run = ready;//run每次运行ready的队头
run->count++;//每运行一-次计数器加1
if(run->needtine < run->round)// 当剩余时间小于时间片轮转时间时的情况
run->cputime += run->needtine;
run->needtime = 0;
else{
run->cputime += run->round;
run->needtine -= run->round;
run->state = 'W';//变为等待
if(ready->next != NULL)
ready = ready->next;
else flag = 0;//当ready剩-个时做标记
if(run->needtime == 0){// 当run结束时放入finish队列里
run->next = finish;
finish = run;
run->state = 'F';
N--;//进程数少1
else{
if(flag){//执行完如果不是剩一一个的话,就把run放到队尾
r->next = run;
「= run;
r->next = NULL;
}
if(N)ready->state = 'R' ;//结束时不应该有'R"
else
ready = NUL;//结束时应该为空
prt(alg);//输出
}
votd FCFSrun(char alg) 1/先来先服务调度算法
PCB *p;
while( run!=NULL)
run->cputime += run->needtine;
run->needtine =0;
run->next = finish;
finish = run;
run->state = 'F';
run = NULL ;
if(ready!=NULL)
firstin(); .
prt(alg);
int main()
char algo; .
//clrscr();
printf("Choose algorith:P/R/F(Priority- Number/RR/FCFS)");
scanf( "%C" , &algo); .
printf("Enter. processes nunber:");
scanf("%d" , &N);
if(algo == 'P'ilalgo == 'p')
creat1(algo);
priority(algo);
else if(algo == 'R'llalgo == 'r')
creat2(algo);
roundrun(algo);
else if(algo=='f'llalgo=='F')
creat1(algo);
FCFSrun(algo);
system("echo Press any button to exit");
return 0;