#include<stdio.h>
#include <dos.h>
#include<stdlib.h>
#include<conio.h>
#define P_NUM 5 /*宏定义进程的数目*/
#define P_TIME 50 /*宏定义进程的初始优先级*/
enum state{ /*定义说明进程状态的枚举类型*/
ready, /*进程就绪*/
execute, /*进程执行*/
block, /*进程阻塞*/
finish /*进程完成*/
};
typedef struct pcb { /*定义进程的PCB结构*/
char name[4]; /*定义进程的名字*/
int priority; /*定义进程的优先级*/
int cputime; /*定义进程的需要的CPU时间*/
int needtime; /*定义进程的还需要的时间*/
int count; /*定义进程的执行次数*/
int round; /*定义进程的轮转次数*/
enum state process; /*定义进程的状态*/
struct pcb * next; /*定义指向下一进程的指针*/
}pcb;
pcb * get_process(){ /*创建进程队列*/
pcb *q; /*定义用于创建新进程的指针*/
pcb *t; /*定义用于连接队列指针*/
pcb *p; /*定义用于确定队列的指针*/
int i=0;
printf("输入进程和时间\n");
while (i<P_NUM){
q=(struct pcb *)malloc(sizeof(pcb)); /*创建新的进程*/
scanf("%s",q->name);
scanf("%d",&q->needtime);
q->cputime=0;
q->priority=P_TIME-q->needtime; /*分配进程优先级*/
q->process=ready;
q->next=NULL;
if (i==0){
p=q;
t=q;
}
else{
t->next=q;
t=q;
}
i++;
}
return p;/*返回进程队列的头指针*/
}
void display( pcb *p){ /*打印进程队列*/
printf("name cputime needtime priority state\n");
while(p){
printf("%s",p->name);
printf(" ");
printf("%d",p->cputime);
printf(" ");
printf("%d",p->needtime);
printf(" ");
printf("%d",p->priority);
printf(" ");
switch(p->process){ /*选择进程的状态*/
case ready:printf("ready\n");break;
case execute:printf("execute\n");break;
case block:printf("block\n");break;
case finish:printf("finish\n");break;
}
p=p->next;
}
}
int process_finish( pcb *q){/*判断当前进程是否完成*/
int bl=1;
while(bl&&q){
bl=bl&&q->needtime==0;
q=q->next;
}
return bl;
}
void cpuexe(pcb *q){ /*进程执行*/
struct pcb *t=q;
int tp=0;
while(q){
if (q->process!=finish){
q->process=ready;
if(q->needtime==0){
q->process=finish;
}
}
if(tp<q->priority&&q->process!=finish){/*选择优先级大而又未完成的进程*/
tp=q->priority;
t=q;
}
q=q->next;
}
if(t->needtime!=0){
t->priority-=3;
t->needtime--;
t->process=execute;
t->cputime++;
}
}
void priority_cal(){ /*执行优先级调度*/
pcb * p;
int cpu=0;
clrscr();
p=get_process();
/*int cpu=0;*/
clrscr();
while(!process_finish(p)){
cpu++;
printf("cputime:");
printf("%d\n",cpu);
cpuexe(p);
display(p);
sleep(20);
/*clrscr(); */
}
printf("按下任何键退出");
getch();
}
void display_menu(){ /*显示菜单*/
printf("\n*************************************");
printf("\n* 进程调度演示系统 *");
printf("\n*************************************");
printf("\n 选择调度算法: ");
printf("\n 1 优先数算法 ");
printf("\n 2 时间片轮转算法 ");
printf("\n 3 退出 ");
printf("\n*************************************");
}
pcb * get_process_round(){ /*创建进程队列*/
pcb *q;
pcb *t;
pcb *p;
int i=0;
printf("输入进程和时间\n");
while (i<P_NUM){
q=(struct pcb *)malloc(sizeof(pcb));
scanf("%s",q->name);
scanf("%d",&q->needtime);
q->cputime=0;
q->round=0;
q->count=0;
q->process=ready;
q->next=NULL;
if (i==0){
p=q;
t=q;
}
else{
t->next=q;
t=q;
}
i++;
}
return p;
}
void cpu_round(pcb *q){
q->cputime+=2;
q->needtime-=2;
if(q->needtime<0) {
q->needtime=0;
}
q->count++;
q->round++;
q->process=execute;
}
pcb * get_next(pcb * k,pcb * head){/*按轮转法得到下一个将要执行的进程*/
pcb * t;
t=k;
do{
t=t->next;
}
while (t && t->process==finish);
if(t==NULL){
t=head;
while (t->next!=k && t->process==finish){
t=t->next;
}
}
return t;
}
void set_state(pcb *p){ /*设置进程状态*/
while(p){
if (p->needtime==0){
p->process=finish;
}
if (p->process==execute){
p->process=ready;
}
p=p->next;
}
}
void display_round(pcb *p){
printf("NAME CPUTIME NEEDTIME COUNT ROUND STATE\n");
while(p){
printf("%s",p->name);
printf(" ");
printf("%d",p->cputime);
printf(" ");
printf("%d",p->needtime);
printf(" ");
printf("%d",p->count);
printf(" ");
printf("%d",p->round);
printf(" ");
switch(p->process){
case ready:printf("ready\n");break;
case execute:printf("execute\n");break;
case finish:printf("finish\n");break;
}
p=p->next;
}
}
void round_cal(){/*执行循环轮转调度*/
pcb * p;
pcb * r;
int cpu=0;
clrscr();
p=get_process_round();
/*int cpu=0;*/
clrscr();
r=p;
while(!process_finish(p)){
cpu+=2;
cpu_round(r);
r=get_next(r,p);
printf("cpu ");
printf("%d\n",cpu);
display_round(p);
set_state(p);
sleep(20);
/*clrscr();*/
}
}
void main()
{
int k;
display_menu();
/*int k;*/
printf("\n请选择:");
scanf("%d",&k);
switch(k){
case 1:priority_cal();break;
case 2:round_cal();break;
case 3:break;
default:{display_menu();
scanf("%d",&k); }
}
}