📄 djfk.cpp
字号:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node
{
char name[10]; /*进程标识符*/
int prio; /*进程优先数*/
int round[3]; /*进程时间轮转时间片*/
int cputime; /*进程占用CPU时间*/
int needtime; /*进程到完成还要的时间*/
int count; /*计数器*/
int curpri;
char state; /*进程的状态*/
struct node *next; /*链指针*/
}PCB;
PCB *finish,*ready[3],*tail[3],*run; /*队列指针*/
int M=3;//时间片级数
/*将各级就绪队列中的第一个进程投入运行*/
partfirstin(int i)
{
run=ready[i]; /*就绪队列头指针赋值给运行头指针*/
run->state='R'; /*进程状态变为运行态*/
ready[i]=ready[i]->next; /*就绪对列头指针后移到下一进程*/
}
firstin()
{/*将就绪队列中的第一个进程投入运行*/
if(ready[0]!=NULL) partfirstin(0);
else if(ready[1]!=NULL) partfirstin(1);
else if(ready[2]!=NULL) partfirstin(2);
}
/*标题输出函数*/
void prt1(char a)
{
if(toupper(a)=='P') /*优先数法*/
printf(" name cputime needtime priority state\n");
else
printf(" name cputime needtime count curpri state\n");
}
/*进程PCB输出*/
void prt2(char a,PCB *q)
{
if(toupper(a)=='P') /*优先数法的输出*/
printf(" %-10s%-10d%-10d%-10d %c\n",q->name,
q->cputime,q->needtime,q->prio,q->state);
else/*轮转法的输出*/
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
q->cputime,q->needtime,q->count,q->curpri,q->state);
}
/*输出函数*/
void prt(char algo)
{
PCB *p;
prt1(algo); /*输出标题*/
if(run!=NULL) /*如果运行指针不空*/
prt2(algo,run); /*输出当前正在运行的PCB*/
p=ready[0]; /*输出就绪队列1的PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=ready[1]; /*输出就绪队列2的PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=ready[2]; /*输出就绪队列3的PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
getchar(); /*压任意键继续*/
}
/*优先数的插入算法*/
insert1(PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q; /*待插入的PCB指针*/
p1=ready[0]; /*就绪队列头指针*/
r=p1; /*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与p1之间*/
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1; /*否则插入在就绪队列的头*/
ready[0]=s;
}
}
/*轮转法插入函数*/
insert2(PCB *p2,int i)
{if(tail[i]!=NULL)
{tail[i]->next=p2; /*将新的PCB插入在当前就绪队列的尾*/
tail[i]=p2;
p2->next=NULL;
}
else if(ready[i]==NULL)
{p2->next=ready[i];
ready[i]=p2;
tail[i]=p2;
}
}
/*优先数创建初始PCB信息*/
void create1(char alg,int N)
{
PCB *p;
int i,time;
char na[10];
ready[0]=NULL; /*就绪队列头指针*/
finish=NULL; /*完成队列头指针*/
run=NULL; /*运行队列指针*/
printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='w';
p->prio=50-time;
if(ready[0]!=NULL) /*就绪队列不空调用插入函数插入*/
insert1(p);
else
{
p->next=ready[0]; /*创建就绪队列的第一个PCB*/
ready[0]=p;
}
}
// clrscr();
printf(" output of priority:\n");
printf("************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready[0]; /*将就绪队列的第一个进程投入运行*/
ready[0]=ready[0]->next;
run->state='R';
}
/*轮转法创建进程PCB*/
void create2(char alg,int N)
{
PCB *p;
int i,time;
char na[10];
ready[0]=NULL;
ready[1]=NULL;
ready[2]=NULL;
finish=NULL;
run=NULL;
printf("Enter name and time of round process\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0; /*计数器*/
p->state='w';
p->round[0]=2; /*时间片1*/
p->round[1]=4; /*时间片2*/
p->round[2]=8; /*时间片3*/
p->curpri=2;
if(ready[0]!=NULL)
insert2(p,0);
else
{
p->next=ready[0];
ready[0]=p;
tail[0]=p;
}
tail[1]=NULL;
tail[2]=NULL;
}
// clrscr();
printf(" output of round\n");
printf("*************************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready[0]; /*将就绪队列的第一个进程投入运行*/
ready[0]=ready[0]->next;
run->state='R';
}
void createnew(char alg,int N)
{
PCB *p;
int i,time;
char na[10];
printf("Enter name and time of round process\n");
for(i=1;i<=N;i++)
{
p=(PCB *)malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0; /*计数器*/
p->state='w';
p->round[0]=2; /*时间片1*/
p->round[1]=4; /*时间片2*/
p->round[2]=8; /*时间片3*/
p->curpri=2;
if(ready[0]!=NULL)
insert2(p,0);
else
{
p->next=ready[0];
ready[0]=p;
tail[0]=p;
}
}
// clrscr();
printf(" output of round\n");
printf("*************************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready[0]; /*将就绪队列的第一个进程投入运行*/
ready[0]=ready[0]->next;
run->state='R';
}
/*优先数调度算法*/
priority(char alg)
{
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
if(run->needtime==0) /*如所需时间为0将其插入完成队列*/
{
run->next=finish;
finish=run;
run->state='F'; /*置状态为完成态*/
run=NULL; /*运行队列头指针为空*/
if(ready[0]!=NULL) /*如就绪队列不空*/
firstin(); /*将就绪对列的第一个进程投入运行*/
}
else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/
if((ready[0]!=NULL)&&(run->prio<ready[0]->prio))
{
run->state='W';
insert1(run);
firstin(); /*将就绪队列的第一个进程投入运行*/
}
prt(alg); /*输出进程PCB信息*/
}
}
/*时间片轮转法*/
roundrun(char alg)
{
while(run!=NULL)
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->count=run->count+1;
if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
//if(ready!=NULL)
firstin(); /*就绪对列不空,将第一个进程投入运行*/
}
else
for(int i=0;i<M;i++)
if((run->curpri==2-i)&&(run->count==run->round[i]))
/*如果该级的时间片到*/
{
run->count=0; /*计数器置0*/
if(i+1<M) /*如就绪队列不空*/
{
run->curpri=run->curpri-1;
run->state='W'; /*将进程插入到就绪队列中等待轮转*/
insert2(run,i+1);
firstin(); /*将就绪对列的第一个进程投入运行*/
}
else if(i+1==M)
{run->curpri=0;
run->state='w';
insert2(run,2);
firstin();
}
}
prt(alg); /*输出进程信息*/
printf("If you want to enter new process,please type 'y'\n");
char np;
int n;
scanf("%c",&np);
if(np=='y'||np=='Y')
{
if(run->curpri=0) {insert2(run,0);run->state='w';}
else if(run->curpri=1) {insert2(run,1);run->state='w';}
else {insert2(run,2);run->state='w';}
printf("Enter new process number\n");
scanf("%d",&n);
createnew(alg,n);
}
}
}
/*主函数*/
main()
{
char algo; /*算法标记*/
int N; /*进程数*/
// clrscr();
printf("type the algorithm:P/R(priority/roundrobin)\n");
scanf("%c",&algo); /*输入字符确定算法*/
printf("Enter process number\n");
scanf("%d",&N);
if(algo=='P'||algo=='p')
{
create1(algo,N); /*优先数法*/
priority(algo);
}
else
if(algo=='R'||algo=='r')
{
create2(algo,N); /*轮转法*/
roundrun(algo);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -