#include
#include
#include
#include
/*进程控制块数据结构*/
typedef struct node
{
char name[10];/*进程名*/
int prio; /*进程优先级*/
int round; /*进程分配的时间片*/
int cputime; /*进程消耗的CUP时间*/
int needtime; /*进程需要的CUP时间*/
int count; /*进程运行时间*/
char state; /*进程的状态:'R':运行,'W':等待,'F':结束*/
struct node *next;/*指向下一个进程的指针*/
}PCB;
PCB *finish,*ready,*tail,*run;/*指向三个队列的队首的指针,tail为就绪队列的队尾指针*/
int N;/*定义进程的数目*/
/*
函数功能: 将进程就绪队列中第一个放进就绪队列
函数原型: void firstin(void)
函数参数: void
函数返回值:void
*/
void firstin(void)
{
if(ready!=NULL)
{
run=ready;
ready=ready->next;
run->state='R';
run->next=NULL;
}
else
{
run=NULL;
}
}
/*
函数功能:输出进程信息的标题函数
函数原型:void prt1(char a)
函数参数:char a :a=='p'为优先级,=='r'为时间片轮转
函数返回值:void
*/
void prt1(char a)
{
if(toupper(a)=='P')
{
printf(" name cputime needtime priority state /n");
}
else
{
printf(" name cputime needtime count round state /n");
}
}
/*
函数功能:输出单个进程信息的函数
函数原型:void prt2(char a,PCB *p)
函数参数:char a :a=='p'为优先级,=='r'为时间片轮转
PCB *p 为指向待输出的进程控制块的指针
函数返回值:void
*/
void prt2(char a,PCB *p)
{
if(toupper(a)=='P')
{
printf("%-10s,%-10d,%-10d,%-10d,%-5c/n",p->name,p->cputime,p->needtime,p->prio,p->state);
}
else
{
printf("%-10s,%-10d,%-10d,%-10d,%-10d,%-5c/n",p->name,p->cputime,p->needtime,p->count,p->round,p->state);
}
}
/*
函数功能:输出所有进程信息的函数
函数原型:void prt(char algo)
函数参数:char a :a=='p'为优先级,=='r'为时间片轮转
函数返回值:void
*/
void prt(char algo)
{
PCB *p;
prt1(algo);
if(run!=NULL)
{
prt2(algo,run);
}
p=ready;
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
getchar();
}
/*
函数功能:优先级法调度将进程插入到就绪队列算法
函数原型:void insert1(PCB *q)
函数参数:PCB *q 待插入的队列进程控制块
优先级越高,插入越靠前
函数返回值:void
*/
void insert1(PCB *q)
{
PCB *p,*s,*r; /*p,r用来控制就绪队列滚动,S指向插入的队列*/
int b; /*b作为插入控制标志的*/
s=q;
p=ready;
r=p;
b=1;
if(s->prio>=ready->prio)
{
s->next=ready;
ready=s;
}
else
{
while((p!=NULL)&&b)
{
if(p->prio>=s->prio)
{
r=p;
p=p->next;
}
else
{
b=0;
}
}
s->next=p;
r->next=s;
}
}
/*
函数功能:时间片轮转算法调度将进程插入到就绪队列算法
函数原型:void insert2(PCB *q)
函数参数:PCB *q 待插入的队列进程控制块
函数返回值:void
*/
void insert2(PCB *q)
{
tail->next=q;
tail=q;
q->next=NULL;
}
/*
函数功能:采用优先级进程调度法时,进程初始化函数
函数原型:void pcreate_task(char algo)
函数参数:char algo:
函数返回值:void
*/
void pcreate_task(char algo)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
for(i=0;i
p=(PCB*)malloc(sizeof(PCB));
printf("Enter the name of process/n");
scanf("%s",na);
printf("Enter the time of process/n");
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='W';
p->prio=time;
if(ready==NULL)
{
ready=p;
ready->next=NULL;
}
else
{
insert1(p);
}
printf("Output the waiting processes information/n");
prt(algo);
}
firstin();
}
/*
函数功能:采用时间片轮转法进程调度法时,进程初始化函数
函数原型:void rcreate_task(char algo)
函数参数:char algo: R
函数返回值:void
*/
void rcreate_task(char algo)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
for(i=0;i
p=(PCB*)malloc(sizeof(PCB));
printf("Enter the name of process/n");
scanf("%s",na);
printf("Enter the time of process/n");
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0;
p->state='W';
p->round=2;
if(ready!=NULL)
{
insert2(p);
}
else
{
p->next=ready;
ready=p;
tail=p;
}
printf("Output the waiting processes information/n");
prt(algo);
}
run=ready;
ready=ready->next;
run->state='R';
}
/*
函数功能:采用优先级进程调度法时,进程调度函数
函数原型:void priority(char algo)
函数参数:char algo:进程调度类别标志:'P'优先级 'R'时间片轮转
函数返回值:void
作者 : 李文塔 Wenta Li
日期: 2008年5月21日 11:19
*/
priority(char algo)
{
while(run!=NULL)
{
run->cputime+=1;
run->needtime-=1;
run->prio-=3;
if(run->needtime==0)
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
firstin();
}
else
{
if((ready!=NULL)&&(run->prio
{
run->state='W';
insert1(run);
run=NULL;
firstin();
}
}
prt(algo);
}
}
/*
函数功能:采用时间片轮转法进程调度法时,进程调度函数
函数原型:void roundrun(char algo)
函数参数:char algo: R
函数返回值:void
*/
void roundrun(char algo)
{
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
{
if(run->count==run->round)
{
run->count=0;
if(ready!=NULL)
{
run->state='W';
insert2(run);
firstin();
}
}
}
prt(algo);
}
}
/*main 函数*/
int main()
{
char algo;
printf("Choose the type of attemper P:priority R:timeround/n");
scanf("%c",&algo);
printf("Please enter the number of processes N:/n");
scanf("%d",&N);
if((algo=='P')||(algo=='p'))
{
pcreate_task(algo);
priority(algo);
}
else if((algo=='r')||(algo=='R'))
{
rcreate_task(algo);
roundrun(algo);
}
}