#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct node
{
char name[10]; /*进程名*/
int prio; /*进程优先级*/
int round; /*进程分配的时间片*/
int cputime; /*进程消耗的CUP时间*/
int needtime; /*进程需要的CUP时间*/
int count; /*进程运行时间*/
char state; /*进程的状态*/
struct node *next; /*指向下一个进程的指针*/
}PCB;
PCB *finish,*ready,*tail,*run;/*指向三个队列的队首的指针,tail为就绪队列的队尾指针*/
int N; /*定义进程的数目*/
void firstin(void)//将进程就绪队列中第一个放进就绪队列
{
if(ready!=NULL)
{
run=ready;
ready=ready->next;
run->state='R';
run->next=NULL;
}
else
{
run=NULL;
}
}
void prt1(char a)//函数功能:输出进程信息的标题函数
{
if(toupper(a)=='P')
{
printf("进程名 CPU时间 需要时间 优先级 状态\n");
}
else
{
printf("进程名 CPU时间 需要时间 运行时间 时间片 状态\n");
}
}
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)//函数功能:输出所有进程信息的函数
{
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 *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)//时间片轮转算法调度将进程插入到就绪队列算法
{
tail->next=q;
tail=q;
q->next=NULL;
}
void pcreate_task(char algo)//采用优先级进程调度法时,进程初始化函数
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
for(i=0;i<N;i++)
{
p=(PCB*)malloc(sizeof(PCB));
printf("输入进程名:\n");
scanf("%s",na);
printf("输入进程运行时间:\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("输出等待进程信息:\n");
prt(algo);
}
firstin();
}
void rcreate_task(char algo)//函数功能:采用时间片轮转法进程调度法时,进程初始化函数
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
for(i=0;i<N;i++)
{
p=(PCB*)malloc(sizeof(PCB));
printf("输入进程名:\n");
scanf("%s",na);
printf("输入进程运行时间:\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("输出等待进程信息:\n");
prt(algo);
}
run=ready;
ready=ready->next;
run->state='R';
}
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<ready->prio))
{
run->state='W';
insert1(run);
run=NULL;
firstin();
}
}
prt(algo);
}
}
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);
}
}
int main()
{
char algo;
printf("选择调度的类型:P(进程优先数)R(时间片)\n");
scanf("%c",&algo);
printf("请输入进程的数量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);
}
}