经典任务调度算法的模拟程序

        本科毕业设计写了一个经典任务调度算法的模拟仿真程序,测试了五种调度算法的运行性能。在程序中虚拟了任务的进程,按照不同调度算法策略从进程池中选取任务执行,在任务执行过程中,保存相关的统计参数,最后对这些参数进行处理,反映各个调度算法的实际运行性能,程序中可以通过修改任务的相关参数改变任务特点,测试不同任务环境情况下,调度算法的性能适应性。程序是通过C语言编写的,为了对运行结果图像化,采用了EasyX的图形库。由于计算机中实的任务调度过程比较复杂,在实验中为了简化程序的实现,在某些方面进行了特殊处理。

编程平台:VC6.0+EasyX图形库环境。

以下截取部分程序运行图:

源代码:

#include<stdio.h>
#include<stdlib.h>
#include<easyx.h>
#include<time.h>
#include<conio.h>
#include<graphics.h>
#define initProcessNum 10
#define initProcessID 1000
#define MAXProcessRunTime 10000
#define MAX_MEMORY  4000
#define exchange_IO_Compute 10
#define exchange_process 25
#define task_completed 1
#define task_not_completed 0
#define initclocknum 100
enum condition
{
dead,computing,IO_waiting,ready
};
struct process
{
int processID;
int comeingTime;
int ioNum;
int computeNum;
int * ioClock;
int * computeClock;
int ioTime;
int computeTime;
int askMemory;
condition flag;
int produceTime;  //生成时间,以下三个数据成员用以计算相关性能
int runningTime;  //第一次占用CPU的时间
int completedTime; //进程完成时的时间
};
typedef struct processCmpletedInfo  //保存完成一个作业的相关信息
{
int processID,comeingTime,askMemory;
int compute,waitio,exchange,completedTime,runningTime;
processCmpletedInfo * next;
}PCI;
struct processPool
{
process * p;
int proNum;
};
int MAX_COMING_TIME=0,ID_FIFO=0,systemClock=0,FLAG_FIFO=0,FLAG_TT=0,FLAG_PF=0,FLAG_SJF=0,FLAG_HR=0,FLAG_show=1;
int InitPool(processPool * p);
int produce_one_task(processPool * p,int i,int id);
int FIFO(processPool *storage,processPool* memory,int * spareMemory,PCI ** save,int draw);
int TimeTurning(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw);
int TimeTurining_storage_to_memory(processPool *storage,processPool* memory,int *spareMemory,int pos);
int FIFO_storage_to_memory(processPool *storage,processPool* memory,int *spareMemory);
int ShortJobFirst(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw);
int SJF_storage_to_memory(processPool *storage,processPool* memory,int *spareMemory);
int PriorityDispatch(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw);
int Priority_storage_to_memory(processPool *storage,processPool* memory,int *spareMemory);
int High_response_ratio_s_to_m(processPool *storage,processPool* memory,int *spareMemory,int flag,int disatch);
int High_response_ratio_dispatch(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw);


int sort_by_comingtime(processPool * p,int pos);
int showProcessInf(processPool p);
int drawPerformance_FIFO(int x,int y,int memorry);
int drawPerformance_HR(int x,int y,int memorry);
int drawPerformance_PF(int x,int y,int memorry);
int drawPerformance_TT(int x,int y,int memorry);
int drawPerformance_SJF(int x,int y,int memorry);
int MouseListening();
int MouseListening2(int *flagShow,int * showTime);
int showRunningResult(PCI* FIFO,PCI * timet,PCI * SJF,PCI* PR,PCI* HR);


void main()
{
PCI* FIFO_save_head=NULL,** FIFO_pri=NULL,* Timet_save_head=NULL,** Timet_pri=NULL,* SJF_save_head=NULL,** SJF_pri=NULL,
*Priority_save_head=NULL,** Priority_pri=NULL,*HR_save_head=NULL,** HR_pri=NULL,* p;
int i=0,FIFO_memory=MAX_MEMORY,count=0,Timet_memory=MAX_MEMORY,SJF_memory=MAX_MEMORY,Priority_memory=MAX_MEMORY,HR_memory=MAX_MEMORY,
flagDraw=0,showTime=100000;
processPool FIFO_p, FIFO_mmy,Timeturn_p,Timeturn_mmy,SJF_p,SJF_mmy,Priority_p,Priority_mmy,HR_p,HR_mmy;
FILE *fp=NULL;
//processPool HR_p,HR_mmy;


srand((unsigned)time(0));
systemClock=0;
initgraph( 1200,650,SHOWCONSOLE );
settextcolor(GREEN);
setlinecolor(GREEN);
setfillcolor(GREEN);
InitPool(&FIFO_p);
sort_by_comingtime(&FIFO_p,-1);
InitPool(&Timeturn_p);
InitPool(&SJF_p);
InitPool(&Priority_p);
InitPool(&HR_p);
//showRunningResult(FIFO_save_head,Timet_save_head,SJF_save_head,Priority_save_head,HR_save_head);
//Sleep(10000);


Timeturn_mmy.p = (process*)malloc(initProcessNum*sizeof(process));
SJF_mmy.p = (process*)malloc(initProcessNum*sizeof(process));
Priority_mmy.p = (process*)malloc(initProcessNum*sizeof(process));
HR_mmy.p = (process*)malloc(initProcessNum*sizeof(process));
for(i=0; i<initProcessNum ;i++)
{
Timeturn_mmy.p[i].ioClock= (int*)malloc(initclocknum*sizeof(int));
Timeturn_mmy.p[i].computeClock= (int*)malloc(initclocknum*sizeof(int));
SJF_mmy.p[i].ioClock= (int*)malloc(initclocknum*sizeof(int));
SJF_mmy.p[i].computeClock= (int*)malloc(initclocknum*sizeof(int));
Priority_mmy.p[i].ioClock= (int*)malloc(initclocknum*sizeof(int));
Priority_mmy.p[i].computeClock= (int*)malloc(initclocknum*sizeof(int));
HR_mmy.p[i].ioClock= (int*)malloc(initclocknum*sizeof(int));
HR_mmy.p[i].computeClock= (int*)malloc(initclocknum*sizeof(int));
}
Timeturn_mmy.proNum = 0;
TimeTurining_storage_to_memory(&Timeturn_p,&Timeturn_mmy,&Timet_memory,-1);
SJF_storage_to_memory(&SJF_p,&SJF_mmy,&SJF_memory);
Priority_storage_to_memory(&Priority_p,&Priority_mmy,&Priority_memory);
High_response_ratio_s_to_m(&HR_p,&HR_mmy,&HR_memory,0,1);
//showProcessInf(Timeturn_mmy);
FIFO_pri = &FIFO_save_head;
Timet_pri = &Timet_save_head;
SJF_pri = &SJF_save_head;
Priority_pri = &Priority_save_head;
    HR_pri = &HR_save_head;
setbkcolor(WHITE);
while(1)
{
if(MouseListening()==1)
flagDraw=1;
if(count==100)
{
if(FIFO(&FIFO_p,&FIFO_mmy,&FIFO_memory,FIFO_pri,1)==task_completed)
{
FIFO_pri = &((*FIFO_pri)->next);

//printf("hello");
}
if(TimeTurning(&Timeturn_p,&Timeturn_mmy,&Timet_memory,Timet_pri,1)==task_completed)
{
    Timet_pri = &((*Timet_pri)->next);
//printf("hello");
}
if(ShortJobFirst(&SJF_p,&SJF_mmy,&SJF_memory,SJF_pri,1)==task_completed)
{
SJF_pri = &((*SJF_pri)->next);
//printf("hello\n");
}
if(PriorityDispatch(&Priority_p,&Priority_mmy,&Priority_memory,Priority_pri,1)==task_completed)
{
Priority_pri = &((*Priority_pri)->next);
//printf("hello\n");
}
if(High_response_ratio_dispatch(&HR_p,&HR_mmy,&HR_memory,HR_pri,1)==task_completed)
{
HR_pri = &((*HR_pri)->next);
//printf("hello");
}
count=0;
}
else
{
if(FIFO(&FIFO_p,&FIFO_mmy,&FIFO_memory,FIFO_pri,0)==task_completed)
{
FIFO_pri = &((*FIFO_pri)->next);
// printf("hello");
}
if(TimeTurning(&Timeturn_p,&Timeturn_mmy,&Timet_memory,Timet_pri,0)==task_completed)
{
Timet_pri = &((*Timet_pri)->next);
// printf("hello");
}
if(ShortJobFirst(&SJF_p,&SJF_mmy,&SJF_memory,SJF_pri,0)==task_completed)
{
   SJF_pri = &((*SJF_pri)->next);
// printf("hello\n");
}
if(PriorityDispatch(&Priority_p,&Priority_mmy,&Priority_memory,Priority_pri,0)==task_completed)
{
Priority_pri = &((*Priority_pri)->next);
//printf("hello\n");
}
if(High_response_ratio_dispatch(&HR_p,&HR_mmy,&HR_memory,HR_pri,0)==task_completed)
{
HR_pri = &((*HR_pri)->next);
//printf("hello");
//Sleep(1000);
}
count++;
}
if(systemClock==showTime)
{
/*PCI * p=FIFO_save_head;
int i=0;
for( ;p!=NULL;p=p->next)
{
printf("Id %d\n",p->processID);
printf("comeingtime %d\n",p->comeingTime);
printf("runningtime %d\n",p->runningTime);
printf("asdmemory %d\n",p->askMemory);
printf("completedtime %d\n",p->completedTime);
printf("compute %d\n",p->compute);
printf("exchange %d\n",p->exchange);
printf("waitio %d\n",p->waitio);
i++;


}
printf("%d\n",i);*/
if( (fp  = fopen( "data.txt", "a" )) == NULL )
{
printf( "The file 'data.txt' was not opened\n" );
//return 1;
}
else
{
fprintf(fp,"FCFS \n");
for(p=FIFO_save_head;p!=NULL;p=p->next)
fprintf(fp,"%d %d %d %d %d %d %d %d\n",p->processID,p->askMemory,p->comeingTime,p->completedTime,p->compute,
p->exchange,p->waitio,p->runningTime);
fprintf(fp,"\nTime turn \n");
for(p=Timet_save_head;p!=NULL;p=p->next)
fprintf(fp,"%d %d %d %d %d %d %d %d\n",p->processID,p->askMemory,p->comeingTime,p->completedTime,p->compute,
p->exchange,p->waitio,p->runningTime);
fprintf(fp,"\nShort Job First \n");
for(p=SJF_save_head;p!=NULL;p=p->next)
fprintf(fp,"%d %d %d %d %d %d %d %d\n",p->processID,p->askMemory,p->comeingTime,p->completedTime,p->compute,
p->exchange,p->waitio,p->runningTime);
fprintf(fp,"\nPriority  \n");
for(p=Priority_save_head;p!=NULL;p=p->next)
fprintf(fp,"%d %d %d %d %d %d %d %d\n",p->processID,p->askMemory,p->comeingTime,p->completedTime,p->compute,
p->exchange,p->waitio,p->runningTime);
fprintf(fp,"\nHigh response \n");
for(p=HR_save_head;p!=NULL;p=p->next)
fprintf(fp,"%d %d %d %d %d %d %d %d\n",p->processID,p->askMemory,p->comeingTime,p->completedTime,p->compute,
p->exchange,p->waitio,p->runningTime);
fclose(fp);
}
showRunningResult(FIFO_save_head,Timet_save_head,SJF_save_head,Priority_save_head,HR_save_head);
MouseListening2(&flagDraw,&showTime);
}
systemClock++;
if(flagDraw==0)
  Sleep(10);
}


}
int showRunningResult(PCI* FIFO,PCI * timet,PCI * SJF,PCI* PR,PCI* HR)
{
PCI * p=NULL,* name[5];
int count=0,i=0;
char ch[5][10]={"FCFS","timet","SJF","PR","HR"};
double turnover=0,wait=0,c=0,w=0,change=0,pos[4]={0,0,0,0};
struct info
{
char name[10];
double throughput,turnover,wait,CPU_rate;
}inf[5];
name[0]=FIFO,name[1]=timet,name[2]=SJF,name[3]=PR,name[4]=HR;
printf("调度算法..........A\n");
printf("吞吐量............B\n");
printf("平均周转时间......C\n");
printf("等待时间..........D\n");
printf("CPU利用率.........E\n");
printf("A\tB\tC\t\tD\t\tE\n");
for(i=0;i<5;i++)
{
count=0,turnover=0,wait=0,c=0,w=0,change=0;
for(p=name[i]; p!=NULL; p=p->next)
{
count++;
turnover += p->completedTime - p->comeingTime;
wait += p->runningTime - p->comeingTime;
c += p->compute;
w += p->waitio;
change += p->exchange;
}
turnover = turnover/count;
printf("%s\t%d\t%.2f\t\t%.2f\t\t%.2f\n",ch[i],count,turnover,wait,c/(c+w+change));
strcpy(inf[i].name,ch[i]);
inf[i].throughput=count;
inf[i].turnover=turnover;
inf[i].wait=wait;
inf[i].CPU_rate=c/(c+w+change);
}
//画图
//cleardevice();
line(0,600,1200,600);
line(10,600,10,200);
line(10,200,5,205);
line(10,200,15,205);
line(310,600,310,200);
line(310,200,305,205);
line(310,200,315,205);
line(610,600,610,200);
line(610,200,605,205);
line(610,200,615,205);
line(910,600,910,200);
line(910,200,905,205);
line(910,200,915,205);//最高的长度400,宽度均为40
for(i=0;i<5;i++)
{
if(inf[i].throughput>pos[0])
pos[0]=inf[i].throughput;
if(inf[i].turnover>pos[1])
pos[1]=inf[i].turnover;
if(inf[i].wait>pos[2])
pos[2]=inf[i].wait;
if(inf[i].CPU_rate>pos[3])
pos[3]=inf[i].CPU_rate;
}
settextstyle(30, 15, _T("楷体"));
for(i=0;i<5;i++)
{
switch (i)
{
case 0:
setfillcolor(BLUE);
fillrectangle(100,50,150,100);
outtextxy(160,50,"FCFS");
break;
case 1:
setfillcolor(RED);
fillrectangle(250,50,300,100);
outtextxy(310,50,"timeTurn");
break;
case 2:
setfillcolor(YELLOW);
fillrectangle(450,50,500,100);
outtextxy(510,50,"SJf");
break;
case 3:
setfillcolor(BROWN);
fillrectangle(580,50,630,100);
outtextxy(640,50,"PR");
break;
case 4:
setfillcolor(GREEN);
fillrectangle(690,50,740,100);
outtextxy(750,50,"HR");
break;
}
fillrectangle(50+i*40,600-(int)(inf[i].throughput*400/pos[0]),90+i*40,600);
fillrectangle(350+i*40,600-(int)(inf[i].turnover*400/pos[1]),390+i*40,600);
fillrectangle(650+i*40,600-(int)(inf[i].wait*400/pos[2]),690+i*40,600);
fillrectangle(950+i*40,600-(int)(inf[i].CPU_rate*400/pos[3]),990+i*40,600);


}
    outtextxy(100,150,"吞吐量");
outtextxy(350,150,"平均周转时间");
outtextxy(650,150,"平均等待时间");
outtextxy(950,150,"CPU利用率");
return 0;
}
int MouseListening()
{
MOUSEMSG p;
if(MouseHit())
{
p=GetMouseMsg();
if(p.mkLButton==true)
{
if(p.x>0 && p.y>0 && p.x<400 && p.y<325 && (FLAG_HR + FLAG_SJF +FLAG_PF + FLAG_TT + FLAG_FIFO) == 0)
FLAG_FIFO = 1;
else if(p.x>400 && p.y>0 && p.x<800 && p.y<325 && (FLAG_HR + FLAG_SJF +FLAG_PF + FLAG_TT + FLAG_FIFO) == 0)
FLAG_TT = 1;
else if(p.x>800 && p.y>0 && p.x<1200 && p.y<325 && (FLAG_HR + FLAG_SJF +FLAG_PF + FLAG_TT + FLAG_FIFO) == 0)
FLAG_PF = 1;
else if(p.x>0 && p.y>325 && p.x<400 && p.y<650 && (FLAG_HR + FLAG_SJF +FLAG_PF + FLAG_TT + FLAG_FIFO) == 0)
FLAG_SJF = 1;
else if(p.x>400 && p.y>325 && p.x<800 && p.y<650 && (FLAG_HR + FLAG_SJF +FLAG_PF + FLAG_TT + FLAG_FIFO) == 0)
FLAG_HR=1;
else if(FLAG_HR + FLAG_SJF +FLAG_PF + FLAG_TT + FLAG_FIFO >0)
{
FLAG_HR=FLAG_SJF=FLAG_PF=FLAG_TT=FLAG_FIFO=0;
clearrectangle(800,325,1200,650);
}
else if((FLAG_HR + FLAG_SJF +FLAG_PF + FLAG_TT + FLAG_FIFO) == 0 && p.x>890 && p.y>455 && p.x&l

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值