📄 os_good.cpp
字号:
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#include "iostream.h"
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定义进程控制块PCB */
char name[10];
char state;
int super;
int btime;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
char sflag;
int time,t;
//先来先服务
void fcfs_sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
if(ready==NULL) ready=p; /*插入队首*/
else /*插入队尾*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
first=first->link;
second=second->link;
}
first->link=p;
}
}
//最高优先数优先
void hf_sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
//短进程优先
void spf_sort()
{
PCB *first, *second;
int ins=0;
if((ready==NULL)||(p->ntime)<(ready->ntime)) /*插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->ntime)<(second->ntime)){
p->link=second;
first->link=p;
second=NULL;
ins=1;
}
else{
first=first->link;
second=second->link;
}
}
if(ins==0) first->link=p;
}
}
//响应比高者优先
void hrn_sort()
{
PCB *first, *second;
int ins=0,rp;
rp=(p->ntime+t-p->btime)/p->ntime;
if((ready==NULL)||(rp>(ready->ntime+t-ready->btime)/ready->ntime)
||((rp==(ready->ntime+t-ready->btime)/ready->ntime)&&(p->ntime)<(ready->ntime))) /*插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((rp>(second->ntime+t-second->btime)/second->ntime)
||((rp==(second->ntime+t-second->btime)/second->ntime)&&((p->ntime)<(second->ntime))))
{
p->link=second;
first->link=p;
second=NULL;
ins=1;
}
else{
first=first->link;
second=second->link;
}
}
if(ins==0) first->link=p;
}
}
void input1() /* 建立进程控制块函数*/
{
int i,num;
printf("\n 请输入进程数:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程所需时间:");
scanf("%d",&p->ntime);
printf("\n");
p->btime=i;
p->super=1;p->rtime=0;p->state='w';
p->link=NULL;
switch(sflag) /* 调用sort函数*/
{
case '1': fcfs_sort();break;
case '3': spf_sort();break;
default: printf("error!\n");
}
}
}
void input2() /* 建立进程控制块函数*/
{
int i,num;
printf("\n 请输入进程数:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先数:");
scanf("%d",&p->super);
printf("\n 输入进程所需时间:");
scanf("%d",&p->ntime);
printf("\n");
p->btime=i;
p->rtime=0;p->state='w'; p->super=1;
p->link=NULL;
hf_sort();
}
}
void input3() /* 建立进程控制块函数*/
{
int i,num;
printf("\n 请输入作业数:");
scanf("%d",&num);
t=0;
for(i=0;i<num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入作业名:");
scanf("%s",p->name);
p->btime=i;
printf("\n 输入作业所需运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->state='w'; p->rtime=0;p->super=1;
p->link=NULL;
hrn_sort(); /* 调用sort函数*/
}
}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
return(l);
}
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n qname\tstate\tsuper\tbtime\tndtime\truntime\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t",pr->btime);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",p->name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
t++;
(p->super)--;
p->state='w';
switch(sflag) /* 调用sort函数*/
{
case '1': fcfs_sort();break;
case '2': hf_sort();break;
case '3': fcfs_sort();break;
case '4': hrn_sort();break;
default: printf("error!\n");
}
}
}
void dd()
{
printf("\n****************************************************************************\n");
printf(" 1.先来先服务 2.最高优先数优先 3.短进程优先 4.响应比高者优先\n");
printf("****************************************************************************\n");
printf("\n 请选择进程调度算法:");
scanf("%c",&sflag);
switch(sflag)
{
case '1': input1();break;
case '2': input2();break;
case '3': input1();break;
case '4': input3();break;
default: printf(" 输入错误,请重新输入!\n");dd();
}
}
void begin()
{
int len,h=0;
char ch;
t=0;
dd();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("\n The execute number:%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n 按任一键继续......");
ch=getchar();
}
printf("\n\n 进程已经完成.\n");
}
void main() /*主函数*/
{
while(1) { begin();}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -