經典任務調度算法的模擬程序

        本科畢業設計寫了一個經典任務調度算法的模擬仿真程序,測試了五種調度算法的運行性能。在程序中虛擬了任務的進程,按照不同調度算法策略從進程池中選取任務執行,在任務執行過程中,保存相關的統計參數,最後對這些參數進行處理,反映各個調度算法的實際運行性能,程序中可以通過修改任務的相關參數改變任務特點,測試不同任務環境情況下,調度算法的性能適應性。程序是通過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<1110 && p.y<510 )
{
FLAG_show=0;
cleardevice();
return 1;
}
}
}
return 0;
}
int MouseListening2(int *flagShow,int * showTime)
{
MOUSEMSG p;
rectangle(1150,0,1200,50);
outtextxy(1160,10,"X");
while(1)
{
if(MouseHit())
{
p=GetMouseMsg();
if(p.mkLButton==true)
{
if(p.x>1150 && p.y>0 && p.x<1200 && p.y<50 )
{
(*flagShow) = 0;
(*showTime) += (*showTime);
FLAG_show=1;
break;
}

}
}
Sleep(20);
}
cleardevice();
return 0;
}
int drawPerformance_FIFO(int x,int y,int memorry,int com,int wai,int cha)
{
static int info[11][3],num=0;
int i=0,j=0,pos[10][3][2];
float temp,t;
//畫按鈕,不屬於FIFO圖的範圍
if(FLAG_FIFO+FLAG_HR+FLAG_SJF+FLAG_PF+FLAG_TT==0 && FLAG_show==1)
{
settextstyle(30, 15, _T("楷體"));
rectangle(890,455,1110,510);
rectangle(900,465,1100,500);
outtextxy(910,468,"查看運行結果");
}
//結束畫按鈕
info[num][0] = com;
info[num][1] = wai;
info[num++][2] = cha;
if(num>10)  //存儲10個點
{
for(i=0 ;i<10;i++)
{
info[i][0] = info[i+1][0];
info[i][1] = info[i+1][1];
info[i][2] = info[i+1][2];
}
num--;
}
if(FLAG_show==0)
return 0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*90;
pos[i][0][0] = (10-num+i)*25+x+100;
pos[i][0][1] = y+110-(int)temp;
t = (float)info[i][1];
temp = (t/101)*90;
pos[i][1][0] = (10-num+i)*25+x+100;
pos[i][1][1] = y+210-(int)temp;
t = (float)info[i][2];
temp = (t/101)*90;
pos[i][2][0] = (10-num+i)*25+x+100;
pos[i][2][1] = y+310-(int)temp;
}
if(FLAG_FIFO+FLAG_HR+FLAG_SJF+FLAG_PF+FLAG_TT==0)
{
clearrectangle(x,y,x+400,y+315);
settextstyle(20, 10, _T("楷體"));
rectangle(x,y,x+400,y+325);
outtextxy(x+40,y+10,"CPU");       //畫柱狀圖
outtextxy(x+45,y+10+100,"IO");
outtextxy(x+40-15,y+10+200,"change");
rectangle(x+35,y+30,x+75,y+110);
rectangle(x+35,y+130,x+75,y+210);
rectangle(x+35,y+230,x+75,y+310);
outtextxy(x+180,y+310,"FCFS");
t = (float)com;
temp=(t/101)*80;
fillrectangle(x+35,y+110-(int)temp,x+75,y+110);
t = (float)wai;
temp=(t/101)*80;
fillrectangle(x+35,y+210-(int)temp,x+75,y+210);
t = (float)cha;
temp=(t/101)*80;
fillrectangle(x+35,y+310-(int)temp,x+75,y+310);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+100,y+110+100*i,x+350,y+110+100*i);
line(x+350,y+110+100*i,x+350-5,y+110-5+100*i);
line(x+350,y+110+100*i,x+350-5,y+110+5+100*i);

line(x+100,y+110+100*i,x+100,y+20+100*i);
line(x+100,y+20+100*i,x+100-5,y+20+5+100*i);
line(x+100,y+20+100*i,x+100+5,y+20+5+100*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}
else if(FLAG_FIFO==1)
{
x=0,y=0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*180;
pos[i][0][0] = (10-num+i)*50+x+650;
pos[i][0][1] = y+210-(int)temp;
t = (float)info[i][1];
temp = (t/101)*180;
pos[i][1][0] = (10-num+i)*50+x+650;
pos[i][1][1] = y+420-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*180;
pos[i][2][0] = (10-num+i)*50+x+650;
pos[i][2][1] = y+630-(int)temp;
}
clearrectangle(x,y,x+1200,y+650);
settextstyle(40, 20, _T("楷體"));
outtextxy(x+50,y+50,"FCFS");
outtextxy(x+280,y+20,"CPU");       //畫柱狀圖
outtextxy(x+285,y+20+200,"IO");
outtextxy(x+250,y+20+400,"change");
rectangle(x+270,y+65,x+330,y+215);
rectangle(x+270,y+265,x+330,y+415);
rectangle(x+270,y+465,x+330,y+615);
outtextxy(x+290,y+620,"TT");
t = (float)com;
temp=(t/101)*150;
fillrectangle(x+270,y+215-(int)temp,x+330,y+215);
t = (float)wai;
temp=(t/101)*150;
fillrectangle(x+270,y+415-(int)temp,x+330,y+415);
t = (float)cha;
temp=(t/101)*150;
fillrectangle(x+270,y+615-(int)temp,x+330,y+615);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+650,y+210+210*i,x+1150,y+210+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210-10+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210+10+210*i);

line(x+650,y+210+210*i,x+650,y+20+210*i);
line(x+650,y+20+210*i,x+650-10,y+20+10+210*i);
line(x+650,y+20+210*i,x+650+10,y+20+10+210*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}


return 0;
}
int drawPerformance_HR(int x,int y,int memorry,int com,int wai,int cha)
{
static int info[11][3],num=0;
int i=0,j=0,pos[10][3][2];
float temp,t;
info[num][0] = com;
info[num][1] = wai;
info[num++][2] = cha;
if(num>10)  //存儲10個點
{
for(i=0 ;i<10;i++)
{
info[i][0] = info[i+1][0];
info[i][1] = info[i+1][1];
info[i][2] = info[i+1][2];
}
num--;
}
if(FLAG_show==0)
return 0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*90;
pos[i][0][0] = (10-num+i)*25+x+100;
pos[i][0][1] = y+110-(int)temp;
t = (float)info[i][1];
temp = (t/101)*90;
pos[i][1][0] = (10-num+i)*25+x+100;
pos[i][1][1] = y+210-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*90;
pos[i][2][0] = (10-num+i)*25+x+100;
pos[i][2][1] = y+310-(int)temp;
}
if(FLAG_FIFO+FLAG_HR+FLAG_SJF+FLAG_PF+FLAG_TT==0)
{
clearrectangle(x,y,x+400,y+315);
settextstyle(20, 10, _T("楷體"));
rectangle(x,y,x+400,y+325);
outtextxy(x+40,y+10,"CPU");       //畫柱狀圖
outtextxy(x+45,y+10+100,"IO");
outtextxy(x+40-15,y+10+200,"change");
rectangle(x+35,y+30,x+75,y+110);
rectangle(x+35,y+130,x+75,y+210);
rectangle(x+35,y+230,x+75,y+310);
outtextxy(x+180,y+310,"HR");
t = (float)com;
temp=(t/101)*80;
fillrectangle(x+35,y+110-(int)temp,x+75,y+110);
t = (float)wai;
temp=(t/101)*80;
fillrectangle(x+35,y+210-(int)temp,x+75,y+210);
t = (float)cha;
temp=(t/101)*80;
fillrectangle(x+35,y+310-(int)temp,x+75,y+310);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+100,y+110+100*i,x+350,y+110+100*i);
line(x+350,y+110+100*i,x+350-5,y+110-5+100*i);
line(x+350,y+110+100*i,x+350-5,y+110+5+100*i);

line(x+100,y+110+100*i,x+100,y+20+100*i);
line(x+100,y+20+100*i,x+100-5,y+20+5+100*i);
line(x+100,y+20+100*i,x+100+5,y+20+5+100*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}
else if(FLAG_HR==1)
{
x=0,y=0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*180;
pos[i][0][0] = (10-num+i)*50+x+650;
pos[i][0][1] = y+210-(int)temp;
t = (float)info[i][1];
temp = (t/101)*180;
pos[i][1][0] = (10-num+i)*50+x+650;
pos[i][1][1] = y+420-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*180;
pos[i][2][0] = (10-num+i)*50+x+650;
pos[i][2][1] = y+630-(int)temp;
}
clearrectangle(x,y,x+1200,y+650);
settextstyle(40, 20, _T("楷體"));
outtextxy(x+50,y+50,"HR");
outtextxy(x+280,y+20,"CPU");       //畫柱狀圖
outtextxy(x+285,y+20+200,"IO");
outtextxy(x+250,y+20+400,"change");
rectangle(x+270,y+65,x+330,y+215);
rectangle(x+270,y+265,x+330,y+415);
rectangle(x+270,y+465,x+330,y+615);
outtextxy(x+290,y+620,"TT");
t = (float)com;
temp=(t/101)*150;
fillrectangle(x+270,y+215-(int)temp,x+330,y+215);
t = (float)wai;
temp=(t/101)*150;
fillrectangle(x+270,y+415-(int)temp,x+330,y+415);
t = (float)cha;
temp=(t/101)*150;
fillrectangle(x+270,y+615-(int)temp,x+330,y+615);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+650,y+210+210*i,x+1150,y+210+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210-10+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210+10+210*i);

line(x+650,y+210+210*i,x+650,y+20+210*i);
line(x+650,y+20+210*i,x+650-10,y+20+10+210*i);
line(x+650,y+20+210*i,x+650+10,y+20+10+210*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}


return 0;
}
int drawPerformance_TT(int x,int y,int memorry,int com,int wai,int cha)
{
static int info[11][3],num=0;
int i=0,j=0,pos[10][3][2];
float temp,t;
info[num][0] = com;
info[num][1] = wai;
info[num++][2] = cha;
if(num>10)  //存儲10個點
{
for(i=0 ;i<10;i++)
{
info[i][0] = info[i+1][0];
info[i][1] = info[i+1][1];
info[i][2] = info[i+1][2];
}
num--;
}
if(FLAG_show==0)
return 0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*90;
pos[i][0][0] = (10-num+i)*25+x+100;
pos[i][0][1] = y+110-(int)temp;
t = (float)info[i][1];
temp = (t/101)*90;
pos[i][1][0] = (10-num+i)*25+x+100;
pos[i][1][1] = y+210-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*90;
pos[i][2][0] = (10-num+i)*25+x+100;
pos[i][2][1] = y+310-(int)temp;
}
if(FLAG_FIFO+FLAG_HR+FLAG_SJF+FLAG_PF+FLAG_TT==0)
{
clearrectangle(x,y,x+400,y+315);
settextstyle(20, 10, _T("楷體"));
rectangle(x,y,x+400,y+325);
outtextxy(x+40,y+10,"CPU");       //畫柱狀圖
outtextxy(x+45,y+10+100,"IO");
outtextxy(x+40-15,y+10+200,"change");
rectangle(x+35,y+30,x+75,y+110);
rectangle(x+35,y+130,x+75,y+210);
rectangle(x+35,y+230,x+75,y+310);
outtextxy(x+180,y+310,"TT");
t = (float)com;
temp=(t/101)*80;
fillrectangle(x+35,y+110-(int)temp,x+75,y+110);
t = (float)wai;
temp=(t/101)*80;
fillrectangle(x+35,y+210-(int)temp,x+75,y+210);
t = (float)cha;
temp=(t/101)*80;
fillrectangle(x+35,y+310-(int)temp,x+75,y+310);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+100,y+110+100*i,x+350,y+110+100*i);
line(x+350,y+110+100*i,x+350-5,y+110-5+100*i);
line(x+350,y+110+100*i,x+350-5,y+110+5+100*i);

line(x+100,y+110+100*i,x+100,y+20+100*i);
line(x+100,y+20+100*i,x+100-5,y+20+5+100*i);
line(x+100,y+20+100*i,x+100+5,y+20+5+100*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}
else if(FLAG_TT==1)
{
x=0,y=0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*180;
pos[i][0][0] = (10-num+i)*50+x+650;
pos[i][0][1] = y+210-(int)temp;
t = (float)info[i][1];
temp = (t/101)*180;
pos[i][1][0] = (10-num+i)*50+x+650;
pos[i][1][1] = y+420-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*180;
pos[i][2][0] = (10-num+i)*50+x+650;
pos[i][2][1] = y+630-(int)temp;
}
clearrectangle(x,y,x+1200,y+650);
settextstyle(40, 20, _T("楷體"));
outtextxy(x+50,y+50,"TT");
outtextxy(x+280,y+20,"CPU");       //畫柱狀圖
outtextxy(x+285,y+20+200,"IO");
outtextxy(x+250,y+20+400,"change");
rectangle(x+270,y+65,x+330,y+215);
rectangle(x+270,y+265,x+330,y+415);
rectangle(x+270,y+465,x+330,y+615);
outtextxy(x+290,y+620,"TT");
t = (float)com;
temp=(t/101)*150;
fillrectangle(x+270,y+215-(int)temp,x+330,y+215);
t = (float)wai;
temp=(t/101)*150;
fillrectangle(x+270,y+415-(int)temp,x+330,y+415);
t = (float)cha;
temp=(t/101)*150;
fillrectangle(x+270,y+615-(int)temp,x+330,y+615);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+650,y+210+210*i,x+1150,y+210+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210-10+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210+10+210*i);

line(x+650,y+210+210*i,x+650,y+20+210*i);
line(x+650,y+20+210*i,x+650-10,y+20+10+210*i);
line(x+650,y+20+210*i,x+650+10,y+20+10+210*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}


return 0;
}
int drawPerformance_PF(int x,int y,int memorry,int com,int wai,int cha)
{
static int info[11][3],num=0;
int i=0,j=0,pos[10][3][2];
float temp,t;
info[num][0] = com;
info[num][1] = wai;
info[num++][2] = cha;
if(num>10)  //存儲10個點
{
for(i=0 ;i<10;i++)
{
info[i][0] = info[i+1][0];
info[i][1] = info[i+1][1];
info[i][2] = info[i+1][2];
}
num--;
}
if(FLAG_show==0)
return 0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*90;
pos[i][0][0] = (10-num+i)*25+x+100;
pos[i][0][1] = y+110-(int)temp;
t = (float)info[i][1];
temp = (t/101)*90;
pos[i][1][0] = (10-num+i)*25+x+100;
pos[i][1][1] = y+210-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*90;
pos[i][2][0] = (10-num+i)*25+x+100;
pos[i][2][1] = y+310-(int)temp;
}
if(FLAG_FIFO+FLAG_HR+FLAG_SJF+FLAG_PF+FLAG_TT==0)
{
clearrectangle(x,y,x+400,y+315);
settextstyle(20, 10, _T("楷體"));
rectangle(x,y,x+400,y+325);
outtextxy(x+40,y+10,"CPU");       //畫柱狀圖
outtextxy(x+45,y+10+100,"IO");
outtextxy(x+40-15,y+10+200,"change");
rectangle(x+35,y+30,x+75,y+110);
rectangle(x+35,y+130,x+75,y+210);
rectangle(x+35,y+230,x+75,y+310);
outtextxy(x+180,y+310,"PF");
t = (float)com;
temp=(t/101)*80;
fillrectangle(x+35,y+110-(int)temp,x+75,y+110);
t = (float)wai;
temp=(t/101)*80;
fillrectangle(x+35,y+210-(int)temp,x+75,y+210);
t = (float)cha;
temp=(t/101)*80;
fillrectangle(x+35,y+310-(int)temp,x+75,y+310);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+100,y+110+100*i,x+350,y+110+100*i);
line(x+350,y+110+100*i,x+350-5,y+110-5+100*i);
line(x+350,y+110+100*i,x+350-5,y+110+5+100*i);

line(x+100,y+110+100*i,x+100,y+20+100*i);
line(x+100,y+20+100*i,x+100-5,y+20+5+100*i);
line(x+100,y+20+100*i,x+100+5,y+20+5+100*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}
else if(FLAG_PF==1)
{
x=0,y=0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*180;
pos[i][0][0] = (10-num+i)*50+x+650;
pos[i][0][1] = y+210-(int)temp;
t = (float)info[i][1];
temp = (t/101)*180;
pos[i][1][0] = (10-num+i)*50+x+650;
pos[i][1][1] = y+420-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*180;
pos[i][2][0] = (10-num+i)*50+x+650;
pos[i][2][1] = y+630-(int)temp;
}
clearrectangle(x,y,x+1200,y+650);
settextstyle(40, 20, _T("楷體"));
outtextxy(x+50,y+50,"PF");
outtextxy(x+280,y+20,"CPU");       //畫柱狀圖
outtextxy(x+285,y+20+200,"IO");
outtextxy(x+250,y+20+400,"change");
rectangle(x+270,y+65,x+330,y+215);
rectangle(x+270,y+265,x+330,y+415);
rectangle(x+270,y+465,x+330,y+615);
outtextxy(x+290,y+620,"TT");
t = (float)com;
temp=(t/101)*150;
fillrectangle(x+270,y+215-(int)temp,x+330,y+215);
t = (float)wai;
temp=(t/101)*150;
fillrectangle(x+270,y+415-(int)temp,x+330,y+415);
t = (float)cha;
temp=(t/101)*150;
fillrectangle(x+270,y+615-(int)temp,x+330,y+615);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+650,y+210+210*i,x+1150,y+210+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210-10+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210+10+210*i);

line(x+650,y+210+210*i,x+650,y+20+210*i);
line(x+650,y+20+210*i,x+650-10,y+20+10+210*i);
line(x+650,y+20+210*i,x+650+10,y+20+10+210*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}
return 0;
}
int drawPerformance_SJF(int x,int y,int memorry,int com,int wai,int cha)
{
static int info[11][3],num=0;
int i=0,j=0,pos[10][3][2];
float temp,t;
info[num][0] = com;
info[num][1] = wai;
info[num++][2] = cha;
if(num>10)  //存儲10個點
{
for(i=0 ;i<10;i++)
{
info[i][0] = info[i+1][0];
info[i][1] = info[i+1][1];
info[i][2] = info[i+1][2];
}
num--;
}
if(FLAG_show==0)
return 0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*90;
pos[i][0][0] = (10-num+i)*25+x+100;
pos[i][0][1] = y+110-(int)temp;
t = (float)info[i][1];
temp = (t/101)*90;
pos[i][1][0] = (10-num+i)*25+x+100;
pos[i][1][1] = y+210-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*90;
pos[i][2][0] = (10-num+i)*25+x+100;
pos[i][2][1] = y+310-(int)temp;
}
if(FLAG_FIFO+FLAG_HR+FLAG_SJF+FLAG_PF+FLAG_TT==0)
{
clearrectangle(x,y,x+400,y+315);
settextstyle(20, 10, _T("楷體"));
rectangle(x,y,x+400,y+325);
outtextxy(x+40,y+10,"CPU");       //畫柱狀圖
outtextxy(x+45,y+10+100,"IO");
outtextxy(x+40-15,y+10+200,"change");
rectangle(x+35,y+30,x+75,y+110);
rectangle(x+35,y+130,x+75,y+210);
rectangle(x+35,y+230,x+75,y+310);
outtextxy(x+180,y+310,"SJF");
t = (float)com;
temp=(t/101)*80;
fillrectangle(x+35,y+110-(int)temp,x+75,y+110);
t = (float)wai;
temp=(t/101)*80;
fillrectangle(x+35,y+210-(int)temp,x+75,y+210);
t = (float)cha;
temp=(t/101)*80;
fillrectangle(x+35,y+310-(int)temp,x+75,y+310);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+100,y+110+100*i,x+350,y+110+100*i);
line(x+350,y+110+100*i,x+350-5,y+110-5+100*i);
line(x+350,y+110+100*i,x+350-5,y+110+5+100*i);

line(x+100,y+110+100*i,x+100,y+20+100*i);
line(x+100,y+20+100*i,x+100-5,y+20+5+100*i);
line(x+100,y+20+100*i,x+100+5,y+20+5+100*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}
else if(FLAG_SJF==1)
{
x=0,y=0;
for(i=0; i<num; i++)
{
t = (float)info[i][0];
temp = (t/101)*180;
pos[i][0][0] = (10-num+i)*50+x+650;
pos[i][0][1] = y+210-(int)temp;
t = (float)info[i][1];
temp = (t/101)*180;
pos[i][1][0] = (10-num+i)*50+x+650;
pos[i][1][1] = y+420-(int)temp;
t = (float)info[i][2];
temp = (float)(t/101)*180;
pos[i][2][0] = (10-num+i)*50+x+650;
pos[i][2][1] = y+630-(int)temp;
}
clearrectangle(x,y,x+1200,y+650);
settextstyle(40, 20, _T("楷體"));
outtextxy(x+50,y+50,"SJF");
outtextxy(x+280,y+20,"CPU");       //畫柱狀圖
outtextxy(x+285,y+20+200,"IO");
outtextxy(x+250,y+20+400,"change");
rectangle(x+270,y+65,x+330,y+215);
rectangle(x+270,y+265,x+330,y+415);
rectangle(x+270,y+465,x+330,y+615);
outtextxy(x+290,y+620,"TT");
t = (float)com;
temp=(t/101)*150;
fillrectangle(x+270,y+215-(int)temp,x+330,y+215);
t = (float)wai;
temp=(t/101)*150;
fillrectangle(x+270,y+415-(int)temp,x+330,y+415);
t = (float)cha;
temp=(t/101)*150;
fillrectangle(x+270,y+615-(int)temp,x+330,y+615);
for(i=0; i<3; i++)   //畫座標   high=90,length=250
{
line(x+650,y+210+210*i,x+1150,y+210+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210-10+210*i);
line(x+1150,y+210+210*i,x+1150-10,y+210+10+210*i);

line(x+650,y+210+210*i,x+650,y+20+210*i);
line(x+650,y+20+210*i,x+650-10,y+20+10+210*i);
line(x+650,y+20+210*i,x+650+10,y+20+10+210*i);
for(j=0;j<num-1;j++)
{
line(pos[j][0][0],pos[j][0][1],pos[j+1][0][0],pos[j+1][0][1]);
line(pos[j][1][0],pos[j][1][1],pos[j+1][1][0],pos[j+1][1][1]);
line(pos[j][2][0],pos[j][2][1],pos[j+1][2][0],pos[j+1][2][1]);
}
}
}
return 0;
}
int High_response_ratio_dispatch(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw)
{
static int compute=0,waitio=0,change=0;
static int computeLast=0,waitioLast=0,changeLast=0;
int i=0;
if(draw==1)
{
//printf("%d %d %d\n",compute-computeLast,waitio-waitioLast,change-changeLast);
drawPerformance_HR(400,325,MAX_MEMORY-(*spareMemory),compute-computeLast,waitio-waitioLast,change-changeLast);
computeLast=compute;
waitioLast=waitio;
changeLast=change;
}
if((* memory).p[0].flag==ready)
(* memory).p[0].flag=computing;
if((* memory).p[0].computeNum>0&&(* memory).p[0].flag==computing)
{
i=(*memory).p[0].computeNum;
if(--((*memory).p[0].computeClock[i-1])<=0)// one compute tase is completed
{
(*memory).p[0].flag=IO_waiting;
(*memory).p[0].computeNum--;
change += exchange_IO_Compute;
compute++;
}
else
{
compute++;
}
}
else if((* memory).p[0].ioNum>0&&(* memory).p[0].flag==IO_waiting)
{

i=(*memory).p[0].ioNum;
if(--((*memory).p[0].ioClock[i-1])<=0)// one io tase is completed
{
(*memory).p[0].flag=computing;
(*memory).p[0].ioNum--;
change += exchange_IO_Compute;
waitio++;
}
else
{
waitio++;
}
}
else if((* memory).p[0].ioNum>0&&(* memory).p[0].flag==computing)
{
(* memory).p[0].flag=IO_waiting;
change += exchange_IO_Compute;
}
else if((* memory).p[0].computeNum>0&&(* memory).p[0].flag==IO_waiting)
{
(* memory).p[0].flag=computing;
change += exchange_IO_Compute;
}
if((* memory).p[0].computeNum<=0&&(* memory).p[0].ioNum<=0)  //一個作業已經完成
{
(*save) = (PCI*)malloc(sizeof(PCI));
(*save)->next = NULL;
(*save)->processID = (*memory).p[0].processID;
(*save)->comeingTime = (*memory).p[0].produceTime;
(*save)->askMemory = (*memory).p[0].askMemory;
(*save)->compute = compute;
(*save)->waitio = waitio;
(*save)->exchange = change;
(*save)->completedTime = systemClock;
(*save)->runningTime = (*memory).p[0].runningTime;
*spareMemory = MAX_MEMORY;         //*spareMemory + (*memory).p[0].askMemory;
compute=waitio=change=0;
computeLast=waitioLast=changeLast=0;
High_response_ratio_s_to_m(&(*storage),&(* memory),&(*spareMemory),1,1);
change += exchange_process;
return task_completed;
}
else
return task_not_completed;
}
int High_response_ratio_s_to_m(processPool *storage,processPool* memory,int *spareMemory,int flag,int dispatch)
{
static int waitTime[initProcessNum],i=0,j=0;
static double HR_ratio[initProcessNum];
if(flag==0)                    //初始化數組
{
for(i=0; i<initProcessNum; i++)
{
waitTime[i]=0;
HR_ratio[i]=1;
}
}
for(i=0; i<(*storage).proNum; i++)  //等待時間增加,重置響應比
{
waitTime[i]++;
HR_ratio[i] = (waitTime[i]+(*storage).p[i].computeTime+(*storage).p[i].ioTime)/((*storage).p[i].computeTime+(*storage).p[i].ioTime);
if(HR_ratio[i] > HR_ratio[j])
j = i;
}
if(dispatch==1)                    //需要調度一個進程到內存中執行
{
if((*storage).p[j].askMemory < *spareMemory)
{
(*memory).p[0].askMemory = (*storage).p[j].askMemory;
(*memory).p[0].comeingTime = (*storage).p[j].comeingTime;
(*memory).p[0].computeNum = (*storage).p[j].computeNum;
(*memory).p[0].ioNum = (*storage).p[j].ioNum;
(*memory).p[0].processID = (*storage).p[j].processID;
(*memory).p[0].flag = (*storage).p[j].flag;
(*memory).p[0].ioTime = (*storage).p[j].ioTime;
(*memory).p[0].computeTime = (*storage).p[j].computeTime;
(*memory).p[0].runningTime = systemClock;
(*memory).p[0].produceTime = (*storage).p[j].produceTime;
for(i=0; i<(*memory).p[0].ioNum; i++)
(*memory).p[0].ioClock[i] = (*storage).p[j].ioClock[i];
for(i=0; i<(*memory).p[0].computeNum; i++)
(*memory).p[0].computeClock[i] = (*storage).p[j].computeClock[i];
(*memory).proNum=1;
*spareMemory = *spareMemory - (*memory).p[j].askMemory;
produce_one_task(&(*storage),j,ID_FIFO++);           //調入一個進程後,再在進程池中新生成一個進程
(*storage).p[j].produceTime = systemClock;
MAX_COMING_TIME = (*storage).p[j].comeingTime;
waitTime[j] = 0;
HR_ratio[j] = 1;
}
else
{
printf("A process ask for a memory overed the system capacity\n ");
exit(1);
}
}
return 0;
}
int PriorityDispatch(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw)
{
static int compute=0,waitio=0,change=0;
static int computeLast=0,waitioLast=0,changeLast=0;
int i=0;
if(draw==1)
{
//printf("%d %d %d\n",compute-computeLast,waitio-waitioLast,change-changeLast);
drawPerformance_PF(800,0,MAX_MEMORY-(*spareMemory),compute-computeLast,waitio-waitioLast,change-changeLast);


computeLast=compute;
waitioLast=waitio;
changeLast=change;
}
if((* memory).p[0].flag==ready)
(* memory).p[0].flag=computing;
if((* memory).p[0].computeNum>0&&(* memory).p[0].flag==computing)
{
i=(*memory).p[0].computeNum;
if(--((*memory).p[0].computeClock[i-1])<=0)// one compute tase is completed
{
(*memory).p[0].flag=IO_waiting;
(*memory).p[0].computeNum--;
change += exchange_IO_Compute;
compute++;
}
else
{
compute++;
}
}
else if((* memory).p[0].ioNum>0&&(* memory).p[0].flag==IO_waiting)
{

i=(*memory).p[0].ioNum;
if(--((*memory).p[0].ioClock[i-1])<=0)// one io tase is completed
{
(*memory).p[0].flag=computing;
(*memory).p[0].ioNum--;
change += exchange_IO_Compute;
waitio++;
}
else
{
waitio++;
}
}
else if((* memory).p[0].ioNum>0&&(* memory).p[0].flag==computing)
{
(* memory).p[0].flag=IO_waiting;
change += exchange_IO_Compute;
}
else if((* memory).p[0].computeNum>0&&(* memory).p[0].flag==IO_waiting)
{
(* memory).p[0].flag=computing;
change += exchange_IO_Compute;
}
if((* memory).p[0].computeNum<=0&&(* memory).p[0].ioNum<=0)  //一個作業已經完成
{
(*save) = (PCI*)malloc(sizeof(PCI));
(*save)->next = NULL;
(*save)->processID = (*memory).p[0].processID;
(*save)->comeingTime = (*memory).p[0].produceTime;
(*save)->askMemory = (*memory).p[0].askMemory;
(*save)->compute = compute;
(*save)->waitio = waitio;
(*save)->exchange = change;
(*save)->completedTime = systemClock;
(*save)->runningTime = (*memory).p[0].runningTime;
*spareMemory = MAX_MEMORY;      //*spareMemory + (*memory).p[0].askMemory;
compute=waitio=change=0;
computeLast=waitioLast=changeLast=0;
Priority_storage_to_memory(&(*storage),&(* memory),&(*spareMemory));
change += exchange_process;
return task_completed;
}
else
return task_not_completed;
}
int Priority_storage_to_memory(processPool *storage,processPool* memory,int *spareMemory)
{
int priorityNum[initProcessNum],i=0,j=0;
double temp=0;
for(i=0; i<(*storage).proNum; i++)    // setting priority to each process
{
temp = (*storage).p[i].computeTime/(*storage).p[i].ioTime;
if(temp > 0.8)
priorityNum[i] = 6;
else if(temp > 0.7)
priorityNum[i] = 5;
else if(temp > 0.6)
priorityNum[i] = 4;
else if(temp > 0.5)
priorityNum[i] = 3;
else if(temp > 0.4)
priorityNum[i] = 2;
else
priorityNum[i] = 1;
if(priorityNum[j] < priorityNum[i])
j = i;
}
if((*storage).p[j].askMemory < *spareMemory)
{
(*memory).p[0].askMemory = (*storage).p[j].askMemory;
(*memory).p[0].comeingTime = (*storage).p[j].comeingTime;
(*memory).p[0].computeNum = (*storage).p[j].computeNum;
(*memory).p[0].ioNum = (*storage).p[j].ioNum;
(*memory).p[0].processID = (*storage).p[j].processID;
(*memory).p[0].flag = (*storage).p[j].flag;
(*memory).p[0].ioTime = (*storage).p[j].ioTime;
(*memory).p[0].computeTime = (*storage).p[j].computeTime;
(*memory).p[0].runningTime = systemClock;
(*memory).p[0].produceTime = (*storage).p[j].produceTime;
for(i=0; i<(*memory).p[0].ioNum; i++)
(*memory).p[0].ioClock[i] = (*storage).p[j].ioClock[i];
for(i=0; i<(*memory).p[0].computeNum; i++)
(*memory).p[0].computeClock[i] = (*storage).p[j].computeClock[i];
(*memory).proNum=1;
*spareMemory = *spareMemory - (*memory).p[j].askMemory;
produce_one_task(&(*storage),j,ID_FIFO++);           //調入一個進程後,再在進程池中新生成一個進程
MAX_COMING_TIME = (*storage).p[j].comeingTime;
}
else
{
printf("A process ask for a memory overed the system capacity\n ");
exit(1);
}
return 0;
}
int ShortJobFirst(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw)
{
static int compute=0,waitio=0,change=0;
static int computeLast=0,waitioLast=0,changeLast=0;
int i=0;
if(draw==1)
{
//printf("%d %d %d\n",compute-computeLast,waitio-waitioLast,change-changeLast);
drawPerformance_SJF(0,325,MAX_MEMORY-(*spareMemory),compute-computeLast,waitio-waitioLast,change-changeLast);


computeLast=compute;
waitioLast=waitio;
changeLast=change;
}
if((* memory).p[0].flag==ready)
(* memory).p[0].flag=computing;
if((* memory).p[0].computeNum>0&&(* memory).p[0].flag==computing)
{
i=(*memory).p[0].computeNum;
if(--((*memory).p[0].computeClock[i-1])<=0)// one compute tase is completed
{
(*memory).p[0].flag=IO_waiting;
(*memory).p[0].computeNum--;
change += exchange_IO_Compute;
compute++;
}
else
{
compute++;
}
}
else if((* memory).p[0].ioNum>0&&(* memory).p[0].flag==IO_waiting)
{

i=(*memory).p[0].ioNum;
if(--((*memory).p[0].ioClock[i-1])<=0)// one io tase is completed
{
(*memory).p[0].flag=computing;
(*memory).p[0].ioNum--;
change += exchange_IO_Compute;
waitio++;
}
else
{
waitio++;
}
}
else if((* memory).p[0].ioNum>0&&(* memory).p[0].flag==computing)
{
(* memory).p[0].flag=IO_waiting;
change += exchange_IO_Compute;
}
else if((* memory).p[0].computeNum>0&&(* memory).p[0].flag==IO_waiting)
{
(* memory).p[0].flag=computing;
change += exchange_IO_Compute;
}
if((* memory).p[0].computeNum<=0&&(* memory).p[0].ioNum<=0)  //一個作業已經完成
{
(*save) = (PCI*)malloc(sizeof(PCI));
(*save)->next = NULL;
(*save)->processID = (*memory).p[0].processID;
(*save)->comeingTime = (*memory).p[0].produceTime;
(*save)->askMemory = (*memory).p[0].askMemory;
(*save)->compute = compute;
(*save)->waitio = waitio;
(*save)->exchange = change;
(*save)->completedTime = systemClock;
(*save)->runningTime = (*memory).p[0].runningTime;
*spareMemory = MAX_MEMORY;//*spareMemory + (*memory).p[0].askMemory;
compute=waitio=change=0;
computeLast=waitioLast=changeLast=0;
SJF_storage_to_memory(&(*storage),&(* memory),&(*spareMemory));
change += exchange_process;
return task_completed;
}
else
return task_not_completed;
}
int SJF_storage_to_memory(processPool *storage,processPool* memory,int *spareMemory)
{
int i=0,j=0;
for(i=1; i<(*storage).proNum; i++)
{
if(((*storage).p[i].computeTime+(*storage).p[i].ioTime)<((*storage).p[j].computeTime+(*storage).p[j].ioTime))
j=i;
}
if((*storage).p[j].askMemory > *spareMemory)
{
printf("The memory asked is overflowed than the system memory.\n");
exit(0);
}
else
{
(*memory).p[0].askMemory = (*storage).p[j].askMemory;
(*memory).p[0].comeingTime = (*storage).p[j].comeingTime;
(*memory).p[0].computeNum = (*storage).p[j].computeNum;
(*memory).p[0].ioNum = (*storage).p[j].ioNum;
(*memory).p[0].processID = (*storage).p[j].processID;
(*memory).p[0].flag = (*storage).p[j].flag;
(*memory).p[0].ioTime = (*storage).p[j].ioTime;
(*memory).p[0].computeTime = (*storage).p[j].computeTime;
(*memory).p[0].runningTime = systemClock;
(*memory).p[0].produceTime = (*storage).p[j].produceTime;
for(i=0; i<(*memory).p[0].ioNum; i++)
(*memory).p[0].ioClock[i] = (*storage).p[j].ioClock[i];
for(i=0; i<(*memory).p[0].computeNum; i++)
(*memory).p[0].computeClock[i] = (*storage).p[j].computeClock[i];
(*memory).proNum=1;
*spareMemory = *spareMemory - (*memory).p[j].askMemory;
produce_one_task(&(*storage),j,ID_FIFO++);           //調入一個進程後,再在進程池中新生成一個進程
MAX_COMING_TIME = (*storage).p[j].comeingTime;
}
return 0;
}
int TimeTurning(processPool *storage,processPool* memory,int *spareMemory,PCI ** save,int draw)
{
static int compute=0,waitio=0,change=0,clockSegment=100,poi=0;
static int computeLast=0,waitioLast=0,changeLast=0;
int i=0,j=0,k=0;
if((*memory).proNum<=0)
TimeTurining_storage_to_memory(&(*storage),&(*memory),&(*spareMemory),-1);
if(draw==1)
{
//printf("%d %d %d\n",compute-computeLast,waitio-waitioLast,change-changeLast);
drawPerformance_TT(400,0,MAX_MEMORY-(*spareMemory),compute-computeLast,waitio-waitioLast,change-changeLast);


computeLast=compute;
waitioLast=waitio;
changeLast=change;
}
if(poi>=(* memory).proNum)
poi = 0;
if((* memory).p[poi].flag==ready)
(* memory).p[poi].flag=computing;
if((* memory).p[poi].computeNum>0&&(* memory).p[poi].flag==computing)
{
i=(*memory).p[poi].computeNum;
if(--((*memory).p[poi].computeClock[i-1])<=0)// one compute tase is completed
{
(*memory).p[poi].flag=IO_waiting;
(*memory).p[poi].computeNum--;
change += exchange_IO_Compute;
compute++;
}
else
{
compute++;
}
}
else if((* memory).p[poi].ioNum>0&&(* memory).p[poi].flag==IO_waiting)
{

i=(*memory).p[poi].ioNum;
if(--((*memory).p[poi].ioClock[i-1])<=0)// one io tase is completed
{
(*memory).p[poi].flag=computing;
(*memory).p[poi].ioNum--;
change += exchange_IO_Compute;
waitio++;
}
else
{
waitio++;
}
}
else if((* memory).p[poi].ioNum>0&&(* memory).p[poi].flag==computing)
{
(* memory).p[poi].flag=IO_waiting;
change += exchange_IO_Compute;
}
else if((* memory).p[poi].computeNum>0&&(* memory).p[poi].flag==IO_waiting)
{
(* memory).p[poi].flag=computing;
change += exchange_IO_Compute;
}
for(i=0; i<(*memory).proNum; i++)  //其他等待io的進程可以並行推進
{
if(i!=poi && (*memory).p[i].flag==IO_waiting)
{
j=(*memory).p[i].ioNum;
if(j>0)
{
if(--((*memory).p[i].computeClock[j-1])<=0)// one compute tase is completed
{
(*memory).p[i].flag=computing;
(*memory).p[i].ioNum--;
change += exchange_IO_Compute;
}
}
}
}
for(i=0; i<(*memory).proNum; i++)  //處理已經運行完畢的進程
{
if((*memory).p[i].ioNum <= 0 && (*memory).p[i].computeNum <= 0) // a task is completed
{
(*save) = (PCI*)malloc(sizeof(PCI));
(*save)->next = NULL;
       (*save)->processID = (*memory).p[i].processID;
(*save)->comeingTime = (*memory).p[i].produceTime;
(*save)->askMemory = (*memory).p[i].askMemory;
(*save)->compute = compute;
(*save)->waitio = waitio;
(*save)->exchange = change;
(*save)->completedTime = systemClock-(*save)->comeingTime;
(*save)->runningTime = (*memory).p[i].runningTime;
(*save)->completedTime = systemClock;
*spareMemory = *spareMemory + (*memory).p[i].askMemory;
compute=waitio=change=0;
computeLast=waitioLast=changeLast=0;
if(TimeTurining_storage_to_memory(&(*storage),&(*memory),&(*spareMemory),i)==1) // 調入一個新進程進入
{
if(i==poi) //佔用CPU的進程已經運行完,將時間片分給其他進程
{
poi++;
   clockSegment=2000;
}
}
else  //沒有足夠內存調入新進程
{
if(i < (*memory).proNum-1)
{
for(j=i; j+1<(*memory).proNum; j++)
{
(*memory).p[j].askMemory = (*memory).p[j+1].askMemory;
(*memory).p[j].comeingTime = (*memory).p[j+1].comeingTime;
(*memory).p[j].computeNum = (*memory).p[j+1].computeNum;
(*memory).p[j].ioNum = (*memory).p[j+1].ioNum;
(*memory).p[j].processID = (*memory).p[j+1].processID;
(*memory).p[j].flag = (*memory).p[j+1].flag;
(*memory).p[j].ioTime = (*memory).p[j+1].ioTime;
(*memory).p[j].computeTime = (*memory).p[j+1].computeTime;
(*memory).p[j].runningTime = (*memory).p[j+1].runningTime;
(*memory).p[j].produceTime = (*memory).p[j+1].produceTime;
(*memory).p[j].completedTime = (*memory).p[j+1].completedTime;
for(k=0; k<(*memory).p[j].computeNum; k++)
(*memory).p[j].computeClock[k] = (*memory).p[j+1].computeClock[k];
for(k=0; k<(*memory).p[j].ioNum; k++)
(*memory).p[j].ioClock[k] = (*memory).p[j+1].ioClock[k];
}
if(i<poi)
poi--;
else if(i==poi)
clockSegment=2000;
}
i--;
(*memory).proNum--;
}
return task_completed;
}
}
--clockSegment;
if(clockSegment<=0)
{
poi=poi+1;
if(poi>=(*memory).proNum)
poi=0;
clockSegment=100;
}
return task_not_completed;
}
int TimeTurining_storage_to_memory(processPool *storage,processPool* memory,int *spareMemory,int pos)
{
int i=0,j=0,k=0,flag=0,translation=0;
for(i=0; i<(*storage).proNum; i++)
{
if((*storage).p[i].comeingTime>MAX_COMING_TIME)
MAX_COMING_TIME = (*storage).p[i].comeingTime;
}
if(pos>=0)
{
for(i=0; i<(*storage).proNum; i++)
{
if((*storage).p[i].askMemory <= *spareMemory)
{
j=pos;
(*memory).p[j].askMemory = (*storage).p[i].askMemory;
(*memory).p[j].comeingTime = (*storage).p[i].comeingTime;
(*memory).p[j].computeNum = (*storage).p[i].computeNum;
(*memory).p[j].ioNum = (*storage).p[i].ioNum;
(*memory).p[j].processID = (*storage).p[i].processID;
(*memory).p[j].flag = (*storage).p[i].flag;
(*memory).p[j].ioTime = (*storage).p[i].ioTime;
(*memory).p[j].computeTime = (*storage).p[i].computeTime;
(*memory).p[j].runningTime = systemClock;
(*memory).p[j].produceTime = (*storage).p[i].produceTime;
for(k=0; k<(*memory).p[j].ioNum; k++)
(*memory).p[j].ioClock[k] = (*storage).p[i].ioClock[k];
for(k=0; k<(*memory).p[j].computeNum; k++)
(*memory).p[j].computeClock[k] = (*storage).p[i].computeClock[k];
*spareMemory = *spareMemory - (*memory).p[j].askMemory;
produce_one_task(storage,i,ID_FIFO++);           //調入一個進程後,再在進程池中新生成一個進程
MAX_COMING_TIME = (*storage).p[i].comeingTime;
translation=1;
break;
}
}
}
else
{
while(1)
{
flag=0;
for(i=0; i<(*storage).proNum; i++)
{
if((*storage).p[i].askMemory <= *spareMemory)
{
j=(*memory).proNum;
(*memory).p[j].askMemory = (*storage).p[i].askMemory;
(*memory).p[j].comeingTime = (*storage).p[i].comeingTime;
(*memory).p[j].computeNum = (*storage).p[i].computeNum;
(*memory).p[j].ioNum = (*storage).p[i].ioNum;
(*memory).p[j].processID = (*storage).p[i].processID;
(*memory).p[j].flag = (*storage).p[i].flag;
(*memory).p[j].ioTime = (*storage).p[i].ioTime;
(*memory).p[j].computeTime = (*storage).p[i].computeTime;
(*memory).p[j].runningTime = systemClock;
(*memory).p[j].produceTime = (*storage).p[i].produceTime;
for(k=0; k<(*memory).p[j].ioNum; k++)
(*memory).p[j].ioClock[k] = (*storage).p[i].ioClock[k];
for(k=0; k<(*memory).p[j].computeNum; k++)
(*memory).p[j].computeClock[k] = (*storage).p[i].computeClock[k];
(*memory).proNum++;
*spareMemory = *spareMemory - (*memory).p[j].askMemory;
produce_one_task(&(*storage),i,ID_FIFO++);           //調入一個進程後,再在進程池中新生成一個進程
MAX_COMING_TIME = (*storage).p[i].comeingTime;
flag=1;
translation=1;
}
}
if(flag == 0)
break;
}
}
return translation;
}
int FIFO(processPool *storage,processPool* memory,int * spareMemory,PCI ** save,int draw)
{
static int compute=0,waitio=0,change=0,flag=0,flagiocpt=0;
static int computeLast=0,waitioLast=0,changeLast=0;
int i=0;
if(draw==1)
{
//printf("%d %d %d\n",compute-computeLast,waitio-waitioLast,change-changeLast);
drawPerformance_FIFO(0,0,MAX_MEMORY-(*spareMemory),compute-computeLast,waitio-waitioLast,change-changeLast);


computeLast=compute;
waitioLast=waitio;
changeLast=change;
}
if(flag==1)  
{
if((*memory).p[0].ioNum>0||(*memory).p[0].computeNum>0)  //task is not completed
{
if(flagiocpt==0)   // implement compute
{
i=(*memory).p[0].computeNum;
if(i>0 && (*memory).p[0].computeClock[i-1]>0)
{
if(--((*memory).p[0].computeClock[i-1])<=0)// ome compute tase is completed
{
flagiocpt = 1;
(*memory).p[0].computeNum--;
change += exchange_IO_Compute;
compute++;
}
else
compute++;

}
else
flagiocpt = 1;
}
else  //wait io
{
i=(*memory).p[0].ioNum;
if(i>0 && (*memory).p[0].ioClock[i-1]>0)
{
if(--((*memory).p[0].ioClock[i-1])<=0)// one io tase is completed
{
flagiocpt = 0;
(*memory).p[0].ioNum--;
change += exchange_IO_Compute;
waitio++;
}
else
waitio++;

}
else
flagiocpt = 0;
}
}
else  //task is completed
{
(*save) = (PCI*)malloc(sizeof(PCI));
(*save)->next = NULL;
       (*save)->processID = (*memory).p[0].processID;
(*save)->comeingTime = (*memory).p[0].produceTime;
(*save)->askMemory = (*memory).p[0].askMemory;
(*save)->compute = compute;
(*save)->waitio = waitio;
(*save)->exchange = change;
(*save)->completedTime = systemClock-(*save)->comeingTime;
(*save)->runningTime = (*memory).p[0].runningTime;
(*save)->completedTime = systemClock;
//*spareMemory = MAX_MEMORY;
free((*memory).p[0].ioClock);
free((*memory).p[0].computeClock);
free((*memory).p);
flag=0;
compute=waitio=change=0;
computeLast=waitioLast=changeLast=0;
return task_completed;
}
}
else         
{
FIFO_storage_to_memory(&(*storage),&(*memory),&(*spareMemory));
change += exchange_process;
//*spareMemory -= (*memory).p[0].askMemory;
flag=1;
//showProcessInf(*memory);
return task_not_completed;
}
return task_not_completed;
}
int FIFO_storage_to_memory(processPool *storage,processPool* memory,int * spareMemory)
{
int i=0,j=0,k=0;
MAX_COMING_TIME = (*storage).p[initProcessNum-1].comeingTime;
(*memory).p = (process*)malloc(initProcessNum*sizeof(process));
memory->proNum = 1;
for(i=0; i<initProcessNum; i++)
{
if((*storage).p[i].askMemory <= *spareMemory)
{
(*memory).p[0].askMemory = (*storage).p[i].askMemory;
(*memory).p[0].comeingTime = (*storage).p[i].comeingTime;
(*memory).p[0].computeNum = (*storage).p[i].computeNum;
(*memory).p[0].ioNum = (*storage).p[i].ioNum;
(*memory).p[0].processID = (*storage).p[i].processID;
(*memory).p[0].flag = (*storage).p[i].flag;
(*memory).p[0].ioTime = (*storage).p[i].ioTime;
(*memory).p[0].computeTime = (*storage).p[i].computeTime;
(*memory).p[0].produceTime = (*storage).p[i].produceTime;
(*memory).p[0].runningTime = systemClock;
(*memory).p[0].computeClock = (int*)malloc((*memory).p[0].computeNum*sizeof(int));
(*memory).p[0].ioClock = (int*)malloc((*memory).p[0].ioNum*sizeof(int));
for(k=0; k<(*memory).p[0].ioNum; k++)
(*memory).p[0].ioClock[k] = (*storage).p[i].ioClock[k];
for(k=0; k<(*memory).p[0].computeNum; k++)
(*memory).p[0].computeClock[k] = (*storage).p[i].computeClock[k];
break;
}
}
if(i<initProcessNum)  //調用一個作業進入內存後,並再生成一個作業等待調入
{
produce_one_task(storage,i,ID_FIFO++);
MAX_COMING_TIME = (*storage).p[i].comeingTime;
sort_by_comingtime(storage,i);
}
return 0;
}
int sort_by_comingtime(processPool * p,int pos)
{
int i=0,j=0;
process temp;
if(pos<0)
{
for(i=0; i<initProcessNum; i++)
for(j=0; j+1<initProcessNum-i; j++)
{
if((*p).p[j].comeingTime>(*p).p[j+1].comeingTime)
{
temp = (*p).p[j];
(*p).p[j] = (*p).p[j+1];
(*p).p[j+1] = temp;
}
}
}
else if(pos<initProcessNum)
{
for(i=pos-1; i>=0 && (*p).p[i].comeingTime > (*p).p[i+1].comeingTime; i--)
{
temp = (*p).p[i];
(*p).p[i] = (*p).p[i+1];
(*p).p[i+1] = temp;
}
for(i=pos+1; i<initProcessNum && (*p).p[i-1].comeingTime > (*p).p[i].comeingTime; i++)
{
temp = (*p).p[i-1];
(*p).p[i-1] = (*p).p[i];
(*p).p[i] = temp;
}
}
else
printf("position eror\n");
return 0;
}
int InitPool(processPool * p)
{
int i=0;
(*p).proNum = initProcessNum;
(*p).p = (process*)malloc(initProcessNum*sizeof(process));
for(i=0; i<initProcessNum; i++)                                        //init process information
{
(*p).p[i].computeClock = (int*)malloc(initclocknum*sizeof(int));
(*p).p[i].ioClock = (int*)malloc(initclocknum*sizeof(int));
produce_one_task(&(*p),i,ID_FIFO++);
}
return 0;
}
int produce_one_task(processPool * p,int i,int id)
{
int time=MAX_COMING_TIME,j=0,totallTime=0;
(*p).p[i].processID = initProcessID+id;           
(*p).p[i].comeingTime = time+rand()%(MAXProcessRunTime/5);
(*p).p[i].produceTime = systemClock;
(*p).p[i].ioNum = rand()%4+20;                                        //IO number setting to 2--5;
(*p).p[i].computeNum = rand()%4+30;                                   //computNum setting to 3--6;
totallTime = (*p).p[i].computeNum  + (*p).p[i].ioNum;
(*p).p[i].computeTime=0;
for(j=0; j<(*p).p[i].computeNum; j++)
{
(*p).p[i].computeClock[j]=rand()%(MAXProcessRunTime/totallTime)+1;
(*p).p[i].computeTime += (*p).p[i].computeClock[j];
}
(*p).p[i].ioTime=0;
for(j=0; j<(*p).p[i].ioNum; j++)
{
(*p).p[i].ioClock[j]=rand()%(MAXProcessRunTime/totallTime)+1;
(*p).p[i].ioTime += (*p).p[i].ioClock[j];
}
(*p).p[i].askMemory = rand()%(MAX_MEMORY/4);
(*p).p[i].flag = ready;
return 0;
}
int showProcessInf(processPool p)
{
int i=0,j=0;
for(i=0;i<p.proNum;i++)
{
printf("ID:%d,%d,%d,%d,%d,%d,%d\n",p.p[i].processID,p.p[i].comeingTime,p.p[i].ioNum,p.p[i].computeNum,p.p[i].ioTime,p.p[i].computeTime
,p.p[i].askMemory);
for(j=0;j<p.p[i].ioNum;j++)
{
printf("%d ",p.p[i].ioClock[j]);
}
printf("\n");
for( j=0;j<p.p[i].computeNum;j++)
{
printf("%d ",p.p[i].computeClock[j]);
}
printf("\n");
}
return 0;
}

發佈了28 篇原創文章 · 獲贊 35 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章