📄 单道作业调度.cpp
字号:
#include "stdio.h"
#include "stdlib.h"
#include<conio.h>
#define getpch(type)(type*)malloc(sizeof(type))
#define NULL 0
struct JCB
{ char name[10];
char state;
int Arrive_time;
int Finish_time;
int Serve_time;
double Rp; //高响应优先权
double Turn_time;
double Carry_time;
struct JCB *link;
} *ready=NULL,*p;
typedef struct JCB jcb;
void sort()
{
jcb *first,*second;
int insert=0;
if((ready==NULL)||((p->Arrive_time)<(ready->Arrive_time)))
{
p->link=ready;
ready=p;
}
else
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->Arrive_time)<(second->Arrive_time))
{
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else
{
first=first->link;
second=second->link;
}
}
if(insert==0)first->link=p;
}
}
void input()
{
int num,i,t=0;
printf("\n请输入作业个数:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("\n作业 %d",i);
// p=(jcb*)malloc(sizeof(jcb));
p=getpch(jcb);
printf("\n作业名:");
scanf("%s",p->name);
printf("\n到达的时间:");
scanf("%d",&p->Arrive_time);
printf("\n作业运行时间:");
scanf("%d",&p->Serve_time);
p->state='w'; p->link=NULL;
sort();
}
}
void count()
{
jcb *p;
p=ready;
//double all_turn=0,n=0;
// double all_carry=0;
while(p!=NULL)
{
p->Turn_time =(p->Finish_time)-(p->Arrive_time) ;
p->Carry_time =(p->Turn_time)/(p->Serve_time) ;
p=p->link;
}
}
void FCFS()//先来先服务
{input();
jcb *p,*q;
p=ready;
p->Finish_time=p->Arrive_time +p->Serve_time ;
//p=p->link;
while(p->link!=NULL)
{
q=p->link;
q->Finish_time=q->Serve_time +p->Finish_time ;
p=p->link;
}
}
void SJF()//短作业优先
{ input();
int nowtime=0;
jcb *front=NULL,*back=NULL,*p=NULL,*q=NULL,*temp=NULL;
nowtime=ready->link->Arrive_time ;
p=ready;
while(p->link!=NULL)
{
front=p;
q=front->link;
while( q!=NULL && q->Arrive_time<=nowtime)
{
q=q->link;
back=q;
}
temp=front->link;
q=front->link;
while(q!=back)
{
if(temp->Serve_time >q->Serve_time )
{
temp=q;
}
q=q->link;
}
if(front->link!=temp)
{
q=front;
while(q->link!=temp)
{
q=q->link;
}
q->link=temp->link;
temp->link=front->link;
front->link=temp;
}
temp->Finish_time =nowtime+temp->Serve_time;
nowtime=temp->Finish_time ;
p=p->link;
}
p=ready;
p->Finish_time=p->Arrive_time +p->Serve_time ;
//p=p->link;
while(p->link!=NULL)
{
q=p->link;
q->Finish_time=q->Serve_time +p->Finish_time ;
p=p->link;
}
}
void HRN()//最高响应比
{ input();
int nowtime=0;
jcb *front=NULL,*back=NULL,*p=NULL,*q=NULL,*temp=NULL;
nowtime=ready->link->Arrive_time ;
p=ready;
while(p->link!=NULL)
{
front=p;
q=front->link;
while( q!=NULL && nowtime>=q->Arrive_time)
{
q=q->link;
back=q;
}
temp=front->link;
q=front->link;
while(q!=back)
{
q->Rp=(q->Serve_time+nowtime-q->Arrive_time)/q->Serve_time ;
q=q->link;
}
q=front->link;
while(q!=back)
{
if(temp->Rp<q->Rp )
{
temp=q;
}
q=q->link;
}
if(front->link!=temp)
{
q=front;
while(q->link!=temp)
{
q=q->link;
}
q->link=temp->link;
temp->link=front->link;
front->link=temp;
}
temp->Finish_time =nowtime+temp->Serve_time;
nowtime=temp->Finish_time ;
p=p->link;
}
p=ready;
p->Finish_time=p->Arrive_time +p->Serve_time ;
//p=p->link;
while(p->link!=NULL)
{
q=p->link;
q->Finish_time=q->Serve_time +p->Finish_time ;
p=p->link;
}
}
void destroy(jcb *p)
{
printf("\n进程[%s]已经完成\n",p->name);
free(p);
}
void print(JCB *pr)
{system("cls");
printf("-----------------------------以下是算法的演示--------------------------\n\n");
printf("\n***正在运行的作业是 :%s ",pr->name);
printf("\n进程名\t到达时间\t服务时间\t状态\t周转时间\t带权周转时间\n");
printf("%s\t", pr->name);
printf("%d\t\t", pr->Arrive_time);
printf("%d\t\t", pr->Serve_time);
printf("r\t" );
printf("%f\t", pr->Turn_time);
printf("%f\t\t", pr->Carry_time);
printf("\n\n***就绪队列");
for(pr=ready->link;pr!=NULL;pr=pr->link)
{
printf("\n进程名\t到达时间\t服务时间\t状态\n");
printf("%s\t",pr->name);
printf("%d\t\t",pr->Arrive_time);
printf("%d\t\t",pr->Serve_time);
printf("%c\t\n",pr->state);
//printf("%f\t",pr->Turn_time);
//printf("%f\t",pr->Carry_time);
}
}
void xlxfw()//先来先服务
{char ch;
FCFS();
count();
while(ready!=NULL)
{
ch=getchar();
system("cls");
p=ready;
print(p);
printf("\n按任意键继续");
ch=getchar();
ready=p->link;
destroy(p);
}
printf("\n进程已经完成!\n\n");
ch=getchar();
}
void zdzy() //短作业优先
{char ch;
SJF();
count();
while(ready!=NULL)
{
ch=getchar();
system("cls");
p=ready;
print(p);
printf("\n按任意键继续");
ch=getchar();
ready=p->link;
destroy(p);
}
printf("\n进程已经完成!\n\n");
ch=getchar();
}
void gxyb()//高响应比
{
char ch;
HRN();
count();
while(ready!=NULL)
{
ch=getchar();
//system("cls");
p=ready;
print(p);
printf("\n按任意键继续");
ch=getchar();
ready=p->link;
destroy(p);
}
printf("\n进程已经完成!\n\n");
ch=getchar();
}
void main()
{int i;
char ch;
int flag=1;
while(flag)
{
printf("\n ------------------------操作系统调度算法演示--------------------------\n");
printf("\n<1>先来先服务(FCFS)\n");
printf("\n<2>最短作业优先(SJF)\n");
printf("\n<3>响应比高者优先(HRN)\n\n");
printf("请选择调度算法:\n");
scanf("%d",&i);
system("cls");
switch(i)
{
case 1:xlxfw();
break;
case 2:zdzy();
break;
case 3:gxyb();
break;
default:
break;
}
printf("你是否想继续(Y or N)?\n");
scanf("%s",&ch);getchar();
system("cls");
if(ch=='Y'||ch=='y')
{
flag=1;
}
else
{flag=0;
printf("这个世界很无奈!!!!\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -