V. CHẠY THỬ CHƯƠNG TRÌNH a.Giao diện ban đầu
g. Thuật toán Round Robin (thời gian chờ trung bình cao nhất)
VII.TÀI LIỆU
[1]Vũ Lê Hùng, Giáo Trình Nguyên Lý Hệ Điều Hành, Đại học Bách Khoa Hồ Chí Minh
[2]Lê Trung Dũng, Giáo Trình Hệ Điều Hành, Nhà xuất bản Giáo Dục
[3]Trần Hồ Thủy Tiên.Giáo Tìình Nguyên Lý Hệ Điều Hành.Đại học Đà Nẵng, Trường đại học Bách Khoa, Khoa Công Nghệ Thông Tin 01/04/2010.
VIII. PHỤ LỤC
Code của file LAPLICH.cpp trong project LapLichCPU.dev (loại Project là WinBGIm )
#include <graphics.h> #include<stdio.h> #include<conio.h> #include <iostream.h> #include<stdlib.h> #include<alloc.h> #include<string.h> #include<windows.h> using namespace std;
void clrscr(void) // ham xoa man hinh { CONSOLE_SCREEN_BUFFER_INFO csbiInfo; HANDLE hConsoleOut; COORD Home = {0,0}; DWORD dummy; hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo); FillConsoleOutputCharacter(hConsoleOut,' ',csbiInfo.dwSize.X * csbiInfo.dwSize.Y,Home,&dummy); csbiInfo.dwCursorPosition.X = 0; csbiInfo.dwCursorPosition.Y = 0; SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPo sition); }
void gotoxy(short x,short y) // ham goto xy {
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = { x,y};
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hConsoleOutput ,
Cursor_an_Pos); }
struct sodo // cau truc du lieu bieu dien o cua so do Gantt
{ int *ten; // ten cua so do trung voi ten cua tien trinh
int *moc; // moc thoi gian cap cpu cho tien trinh int sl; // tong so luong cac o trong so do Gantt
};
struct process // cau truc du lieu cua tien trinh
t
int timexh; // thoi gian den cua tien trinh int timeth; // thoi gian xu ly cua tien trinh };
process* input(int &n) { int i,j;
process *a,tg;
a=new process[n]; // cap phat bo nho cho 1 danh sach don gom n tien trinh
gotoxy(8,1);cout<<"Ten TT"; gotoxy(18,1);cout<<"Time XH"; gotoxy(28,1); cout<<"Time XL"; for(i=0;i<n;i++) { gotoxy(10,i+2); cout<<"P"<<i; a[i].id=i+1; label: gotoxy(20,i+2);
cin>>a[i].timexh; // nhap thoi gian den cho tien tinh
if(a[i].timexh<0) {gotoxy(0,10);
printf("thoi gian den phai > hoac =0 , moi ban nhap lai ");
goto label; } label1:
gotoxy(30,i+2);
cin>>a[i].timeth; // nhap thoi gian xu ly cho tien trinh
if(a[i].timeth<=0) {gotoxy(0,11);
printf("thoi gian xu ly phai > 0 , moi ban nhap lai ");
goto label1; } }
for(i=0;i<n;i++) for(j=0;j<n-1;j++)
if( a[j].timexh>a[j+1].timexh) // sap xep cac tien trinh theo thoi
{ tg=a[j]; a[j]=a[j+1];a[j+1]=tg; }; // gian den tang dan
return a; // tra ve danh sach a gom n tien trinh
} // da duoc sap xep theo tg den
ban cua Dev c++ { int i=0,j=0;
for(i=0;i<=G.sl;i++)
{ cout<<G.moc[i]<<" "; // xuat moc thoi gian if(i!=G.sl)
if(G.ten[i]!=0)
cout<<"P"<<G.ten[i]<<" "; // xuat ten tien trinh j++;
if(j==9) { cout<<"\n"; j=0;} // xuong hang khi xuat duoc 9 o
}} }
void debai(process *A,int &n) // xuat de bai tren che do do hoa { gotoxy(57,1);cout<<"TenTT"; gotoxy(65,1);cout<<"TimeXH"; gotoxy(73,1); cout<<"TimeXL"; for(int i=0;i<n;i++) { gotoxy(57,i+2); cout<<" "; gotoxy(60,i+2); cout<<"P"<<A[i].id; gotoxy(68,i+2); cout<<A[i].timexh; gotoxy(76,i+2); cout<<A[i].timeth; } }
sodo FCFS(process *a,int &n) // thuat toan fcfs { sodo G; process *x; int i,j;
x=new process[n]; // cap phat danh sach x gom n process
for(i=0;i<n;i++)
x[i]=a[i]; // gan danh sach x bang danh sach a
G.sl=0; // so luong o trong so do Grantt ban dau la 0
G.ten=new int; G.moc=new int;
G.moc[0]=0; // khoi tao moc thoi gian ban dau la 0
j=0,i=0; // j dung dem so tien trinh i dung xac dinh moc dang xet
cout<<"Ket qua theo thuat toan FCFS\n\n";
while(j<n) // khi con tien trinh chua den hoac chua duoc xu ly
G.ten=(int*)realloc(G.ten,
(G.sl+1)*sizeof(int)); // cap phat lai bo nho
G.moc=(int*)realloc(G.moc,(G.sl+2)*sizeof(int)); if(G.moc[i]<x[j].timexh) // neu process chua den tra cpu ve cho hdh
{ G.moc[i+1]=x[j].timexh; // moc cap cpu tiep theo la khi process tiep theo den
G.ten[i]=0; // ten cua so do = 0 o trong sd Grantt la o trong
}
else // process da den {
G.ten[i]=x[j].id; // ten trong so do Gantt la ten cua process
G.moc[i+1]=G.moc[i]+x[j].timeth; // cap phat cpu va xac dinh moc cap cpu tiep theo
// = moc hien tai + thoi gian xu ly cua tt
j++; // danh dau tt da xu ly }
G.sl++; // tang so luong o trong so do Grantt
i++; }
return G; }
sodo SJF(process *a,int &n) // thuat toan SJF { sodo G; process *x,tg; int i,j,k,h;
x=new process[n]; for(i=0;i<n;i++) x[i]=a[i]; G.sl=0; G.ten=new int; G.moc=new int; G.moc[0]=0; j=0,i=0;
cout<<"Ket qua theo thuat toan FJS\n\n";
while(j<n) // con tien trinh chua den hoac chua duoc xu ly
{
for(h=0;h<n;h++) for(k=h+1;k<n-1;k++)
if(( x[k].timeth>x[k+1].timeth)&&( x[k+1].timexh<G.moc[i]) )
{ tg=x[k]; // neu tien trinh da den va co thoi gian xu ly nho hon
x[k+1]=tg; }
G.ten=(int*)realloc(G.ten,(G.sl+1)*sizeof(int)); G.moc=(int*)realloc(G.moc,(G.sl+2)*sizeof(int)); if(G.moc[i]<x[j].timexh) // tien trinh chua den
{ G.moc[i+1]=x[j].timexh; // moc tiep = tg den cua process
G.ten[i]=0; // o trong so do Gantt la o trong
} else else {
G.ten[i]=x[j].id; // ten trong so do Gantt la ten cua process
G.moc[i+1]=G.moc[i]+x[j].timeth; // cap cpu va xac dinh moc hien tai + tg thuc hien cua tt
j++; // danh dau them 1 process da duoc xu ly
}
G.sl++; // tang so o trong so do Gantt i++;
}
return G; }
sodo SRT(process *x,int &n) // thuat toan SRT { sodo G; int i,j,tg; process *a;
a=new process[n]; for(i=0;i<n;i++) a[i]=x[i];
int *vt; // vi process co the bi ngat tra cpu cho tien trinh khac
// nen ta dung them vt de danh dau cac vi tri cua tien trinh
int *th; // thoi gian xu ly cua tien trinh int *cl; // thoi gian xu ly con lai cua tien trinh
int sopt=0; // so process trong danh sach san sang
int slxh=0; // so luong tien trinh da den int *xh; // danh dau process da xu ly chua xh=new int[n]; vt=new int; th=new int; cl=new int; G.ten=new int; G.moc=new int;
G.sl=0; // so luong o trong so do Gantt ban dau =0
G.moc[0]=0; // moc cap cpu dau tien bang 0 for(i=0;i<n;i++) xh[i]=0; // cac tien trinh deu o trang thai chua duoc xu ly
cout<<"Ket qua theo thuat toan SRT\n\n";
while(sopt>0 || slxh<n) // danh sach san sang con process hoac con process chua den
{
G.ten=(int*)realloc(G.ten,(G.sl+1)*sizeof(int)); // cap phat bo nho lai
G.moc=(int*)realloc(G.moc,(G.sl+2)*sizeof(int)); for(i=0;i<n;i++)
if (a[i].timexh<=G.moc[G.sl] && xh[i]==0) // process da den va chua duoc xu ly
{ vt=(int*)realloc(vt,(sopt+1)*sizeof(int)); th=(int*)realloc(th,(sopt+1)*sizeof(int)); cl=(int*)realloc(cl,(sopt+1)*sizeof(int)); vt[sopt]=a[i].id; th[sopt]=a[i].timeth; cl[sopt]=a[i].timeth;
sopt++; // tang so tien trinh trong danh sach san sang
slxh++; // tang so tien trinh da den
xh[i]=1; // danh dau tien trinh duoc xu ly }
for(i=0;i<sopt;i++) for(j=0;j<sopt-1;j++)
if(th[j+1] >= th[j]) // sap xep lai cac tien trinh trong ds san sang
{ // theo thoi gian thuc hien ngan hon
tg=vt[j]; vt[j]=vt[j+1]; vt[j+1]=tg; tg=th[j]; th[j]=th[j+1]; th[j+1]=tg;
tg=cl[j]; cl[j]=cl[j+1]; cl[j+1]=tg; }
if(sopt==0) // danh sach san sang trong
{ G.ten[G.sl]=0; // tao o trong trong so do Gantt
G.moc[G.sl+1]=a[slxh].timexh; // moc tiep theo = thoi gian den cua tien trinh tiep theo
G.sl++ ; // tang so o cua so do Gantt len }
else // danh sach san sang co tien trinh { G.ten[G.sl]=vt[sopt-1]; // ten trong so do Gantt la ten cua tien trinh dau ds san sang
theo = moc hien tai + thoi gian con lai cua process G.sl++; // tang so luong o trong so do Grantt
for(i=0;i<n;i++) // neu co tien trinh den ma thoi gian thuc hien nho hon thoi gian
if (a[i].timexh < G.moc[G.sl] && xh[i]==0 && a[i].timeth< th[sopt-1]) // thuc hien con lai cua process
{ G.moc[G.sl]=a[i].timexh; //dinh lai moc thoi gian cap cpu = thoi gian den cua process do
cl[sopt-1]-=G.moc[G.sl]-G.moc[G.sl-1]; // dinh thoi gian con lai cho tien trinh vua bi xen ngang
break; //-= moc hien tai - moc truoc do
}
if(i==n) // neu tat ca cac process dc xu li
sopt--; // thi giam so tien trinh trong ready-list
} } }
return G; // tra ve so do Gantt }
sodo RR(process *x,int &n)
{ sodo G; process *a; int i,j,q; a=new process[n];
for(i=0;i<n;i++) a[i]=x[i];
int *xh,sopt=0,slxh=0,*vt,*cl;
xh=new int[n]; // danh dau process da xu ly chua
vt=new int; // dung vt[i] de thay cho ten cua tien trinh thu i
cl=new int; // dinh thoi gian xu ly con lai cua process
G.ten=new int; G.moc=new int;
G.sl=0; G.moc[0]=0; // khoi tao so luong o=0 trong so do Gantt va moc dau tien =0
for(i=0;i<n;i++) xh[i]=0; // cac tien trinh deu o trang thai chua duoc xu ly
cout<<"Luong tu thoi gian q= "; cin>>q; // nhap quantumn
cout<<"Ket qua theo thuat toan RR\n\n"; for(i=0;i<n;i++)
process co thoi gian den =0
{ vt=(int*)realloc(vt,(sopt+1)*sizeof(int)); cl=(int*)realloc(cl,(sopt+1)*sizeof(int));
vt[sopt]=a[i].id;
cl[sopt]=a[i].timeth; // dinh thoi gian con lai = thoi gian thuc hien
xh[i]=1; sopt++; slxh++; // danh dau process o trang thai da qua xu ly
}
while(sopt>0 || slxh<n ) // danh sach san sang con process hoac con process chua den
{ G.ten=(int*)realloc(G.ten,(G.sl+1)*sizeof(int)); G.moc=(int*)realloc(G.moc,(G.sl+2)*sizeof(int)); if(sopt>0) // danh sach san sang co tien trinh
{ if(cl[sopt-1]<=q) // neu thoi gian con lai cua tien trinh <= quantumn
{ G.moc[G.sl+1]=G.moc[G.sl]+cl[sopt-1]; // moc cap cpu tiep theo= moc hien tai
// + thoi gian con lai cua process + thoi gian con lai cua process
G.ten[G.sl]=vt[sopt-1]; // ten trong o cua so do Gantt la ten cua process
sopt--; // loai tien trinh ra khoi danh sach san sang
for(i=0;i<n;i++)
if(a[i].id==G.ten[G.sl])
{ j=i; // tim tien trinh j de danh dau break;
}
xh[j]=1; // danh dau tien trinh thu j da xuat hien
}
else // thoi gian con lai cua tien trinh lon hon quantumn
{ G.moc[G.sl+1]=G.moc[G.sl]+q; // moc cap cpu tiep theo = moc hien tai + quantumn
G.ten[G.sl]=vt[sopt-1]; // ten trong o cua so do Gantt la ten cua process
for(i=sopt-1;i>0;i--) // dua process ve cuoi ready_list
{ vt[i]=vt[i-1]; cl[i]=cl[i-1]; xh[i]=xh[i-1]; }
vt[0]=G.ten[G.sl]; // hoan thanh dua tien trinh ve cuoi ready_list
if(a[i].id==G.ten[G.sl])
{ j=i; break; } // xac dinh tien trinh j
a[j].timeth-=q; // dinh lai thoi gian xy ly cho process j
cl[0]=a[j].timeth; // dinh thoi gian con lai cho process j
xh[j]=1; // danh dau da qua xu ly }
G.sl++; // tang so luong o so do Gantt len
}
else // danh sach san sang rong { for(i=0;i<n;i++)
if(xh[i]==0) // neu cac process chua qua xu ly
{ j=i; break; }
vt=(int*)realloc(vt,(sopt+1)*sizeof(int)); cl=(int*)realloc(cl,(sopt+1)*sizeof(int)); vt[sopt]=a[j].id;
cl[sopt]=a[j].timeth; // dinh thoi gian con lai = thoi gian thuc hien
G.moc[G.sl+1]=a[j].timexh; // moc cap cpu tiep theo= thoi gian den cua Process dau readylist
G.ten[G.sl]=0; // o trong so do Gantt la o trong
slxh++; sopt++; // tang so process da den ,tang so process trong readylist
G.sl++; // tang so o trong so do Gantt continue;
}
for(i=0;i<n;i++)
if(a[i].timexh<=G.moc[G.sl] && xh[i]==0) // neu co process co thoi gian den < moc hien tai
{ vt=(int*)realloc(vt,(sopt+1)*sizeof(int)); cl=(int*)realloc(cl,(sopt+1)*sizeof(int));
vt[sopt]=a[i].id;
cl[sopt]=a[i].timeth; // dinh thoi gian con lai = thoi gian thuc hien
sopt++; slxh++; xh[i]=1; // tang so process da den ,tang so process trong readylist
} // danh dau process o trang thai da qua xu ly
}
return G; // tra ve so do Gantt }
float waittimeavg(sodo &g,process *x,int &n) // tinh thoi gian cho trung binh
float waittimeavg; // thoi gian cho trung binh int i=g.sl-1,j=0,kt,tg; // i= so luong o -1 ,j dem so luong o cua g
int totalwaittime=0; // tong thoi gian cho
list=new int[n+1]; //danh sach chua cac tien trinh da tim duoc thoi gian ket thuc
waittime=new int[n]; //danh sach thoi gian cho cua cac tien trinh
timefinish=new int[n]; //danh sach thoi gian hoan thanh cua cac tien trinh
timestay=new int[n]; //danh sach thoi gian luu lai cua cac tien trinh
while(j<=g.sl+1 && i>=0) // chua het o trong so do Grantt va so luong o chua thong tin ve tien trinh >=0
{ int k=0;
while(k<=j && g.ten[i]!=list[k]) k++; //kiem tra tien trinh da co trong danh sach tim duoc thoi gian hoan thanh chua
if(k<=j) kt=1; else kt=0;
if(kt==0) // neu chua co trong danh sach thi dua vao danh sach
{ timefinish[j]=g.moc[i+1]; // dinh thoi gian hoan thanh = moc sau no
j++;
list[j]=g.ten[i]; // danh dau phan tu thu j trong danh sach = ten cua o trong so do Grant
} i--; i--; }
for(i=0;i<n;i++) for(j=1;j<=n;j++)
if(list[j]==x[i].id) // danh dau phan tu thu j trong danh sach = ten cua process
{tg=timefinish[i]; timefinish[i]=timefinish[j-1]; timefinish[j-1]=tg;
tg=list[i+1]; list[i+1]=list[j]; list[j]=tg; } // sap xep thoi gian hoan thanh theo thu tu process vi du het p1->p2 for(i=0;i<n;i++) { waittime[i]=timefinish[i]-(x[i].timexh+x[i].timeth); totalwaittime+=waittime[i]; timestay[i]=timefinish[i]-x[i].timexh; } waittimeavg=(float)totalwaittime/n; char ab[30];
// ve chi tiet { char as[30]; setcolor(YELLOW); setbkcolor(0); settextstyle(0,0,2); settextjustify(0,2); rectangle(10+300,100,300+480,125); outtextxy(120+400,105,"CHI TIET"); rectangle(10+300,125,300+480,150+20*n);
sprintf(ab,"Thoi gian cho trung binh la: %f s",waittimeavg); outtextxy(100,170+20*n,ab); outtextxy(20+300,130,"TenTT"); outtextxy(100+310,130,"TimeHT"); outtextxy(200+330,130,"TimeCho"); outtextxy(350+300,130,"TimeLuu"); for(int i=0;i<n;i++) { sprintf(as,"P%d",x[i].id); outtextxy(250+100,150+20*i,as); sprintf(as,"%d",timefinish[i]); outtextxy(350+100,150+20*i,as); sprintf(as,"%d",waittime[i]); outtextxy(480+100,150+20*i,as); sprintf(as,"%d",timestay[i]); outtextxy(500+200,150+20*i,as); } }
//ve chi tiet xong return waittimeavg; }
void ve_debai(process *A,int &n) { char as[30]; setcolor(YELLOW); settextstyle(0,0,2); settextjustify(0,2); rectangle(10,100,300,125); outtextxy(120,105,"DE BAI"); rectangle(10,125,300,150+20*n); outtextxy(20,130,"TenTT"); outtextxy(100,130,"TimeXH"); outtextxy(200,130,"TimeXL"); for(int i=0;i<n;i++) { sprintf(as,"P%d",A[i].id);
outtextxy(70,150+20*i,as); sprintf(as,"%d",A[i].timexh); outtextxy(150,150+20*i,as); sprintf(as,"%d",A[i].timeth); outtextxy(250,150+20*i,as); } }
void giant_output(sodo &G) {
int ax,ay,bx,by,rong=40; //Toa do cac o
vuong can ve
int d=0,i=0,j=0,k=0,h=0; //d:So o
can ve; i:Ve o theo hang; j:Xuong dong moi khi het hang char as[30]; char bs[30]; settextjustify(1,1); setbkcolor(BLUE); settextstyle(0,0,1); line(10,430,700,430); line(700-5,430-3,700,430); line(700-5,430+3,700,430); outtextxy(700-4,440,"t"); for(i=0;i<=G.sl;i++) { settextjustify(1,2); settextstyle(0,0,2); setbkcolor(13); outtextxy(48,400,"CPU "); setbkcolor(5); settextjustify(1,2); settextstyle(0,0,2); if(i!=G.sl) { sprintf(bs,"%d",G.moc[i]); sprintf(as,"P%d ",G.ten[i]); setbkcolor(G.ten[i]); if(G.ten[i]!=0) outtextxy(107+60*h,400+60*k,as); else { settextjustify(1,2); settextstyle(0,0,2); setbkcolor(13); outtextxy(107+60*h,400+60*k,"CPU "); } settextjustify(1,1);
setbkcolor(BLUE); settextstyle(0,0,1);
outtextxy(82+60*h,420+60*k,bs); if(j%7==1&&j!=1&&i<G.sl-1){ k++;h=-1;j=0;
line(10,440+60*k,700,440+60*k);// ve mui ten cho so do giant line(700-5,440+60*k- 3,700,440+60*k); line(700- 5,440+60*k+3,700,440+60*k); outtextxy(700-4,450+60*k,"t"); } h++; delay(1000); } j++; if(i==G.sl) { settextjustify(1,2); settextstyle(0,0,2); setbkcolor(13); outtextxy(108+60*h,461+60*(k- 1),"CPU "); sprintf(bs,"%d",G.moc[i]); settextjustify(1,1); setbkcolor(BLUE); settextstyle(0,0,1); outtextxy(82+60*h,420+60*k,bs); } } } int main( ) {
initwindow( 800 , 600 , "LAP LICH CPU" ); settextstyle(1,0,1);
settextjustify(1,1);
outtextxy( 380 ,30 , "CHUONG TRINH LAP LICH CHO CPU!" );
outtextxy( 380 ,595 , "Svth: Nguyen Huu Thien" ); outstreamxy( 0 , 15 );
char k; int n; process *X; sodo G; do
{ do{ clrscr();
cout<<"\tSo luong tien trinh: "; cin>>n; }while(n<=0||n>15);
X=new process[n]; clrscr();
X=input(n); // Tien hanh nhap du lieu cho mang tien trinh label: clrscr(); debai(X,n); ve_debai(X,n); gotoxy(1,1);
cout<<"\n\tChon thuat toan"; cout<<"\n\t1. Thuat toan FCFS"; cout<<"\n\t2. Thuat toan SJF"; cout<<"\n\t3. Thuat toan SRT"; cout<<"\n\t4. Thuat toan RR"; cout<<"\n\t5. Nhap lai ";
cout<<"\n\t6. Thoat chuong trinh"; cout<<"\n\t Ban chon: ";
cin>>k; }while(k=='5'); if(k=='6') exit(0); if(k!=5) { float timeavg; if(k=='1') {G=FCFS(X,n) ; outtextxy( 310 ,60 ,
"ALGORITHM FIRST COME FIRST SERVICE" );
outtextxy( 350 ,75 , "( den truoc phuc vu truoc)" );}
else if(k=='2') {G=SJF(X,n);outtextxy( 310 ,60 , "ALGORITHM SHORTEST JOB FIRST" );
outtextxy( 350 ,75 , "( cong viec ngan nhat )" );}
else if(k=='3') {G=SRT(X,n);outtextxy( 310 ,60 , "ALGORITHM SHORTEST REMAINING TIME" );
outtextxy( 350 ,75 , "(theo do uu tien)" );}
else if(k=='4') {G=RR(X,n);outtextxy( 310 ,60 , "ALGORITHM ROUND ROBIN " );
outtextxy( 350 ,75 , "( xoay vong)" );} else { goto label; } output(G); giant_output(G);
timeavg=waittimeavg(G,X,n); //Tinh thoi gian cho doi trung binh cua thuat toan
cout<<"\n\nThoi gian cho doi trung binh= "<<timeavg; getch();
goto label; } while( !kbhit() ); closegraph( ); return( 0 ); }