Thuật toán điều phối tiến trình--> nguyên lý hệ điều hành
Trang 1 trong tổng số 1 trang
Thuật toán điều phối tiến trình--> nguyên lý hệ điều hành
#include
#include
#include
#include
#include
struct process{
char ten[12];
int tvao;
int txly;
int tcho;
int tluu;
int stt;
int dut;
} P[20];
int n, j, ok[20];
int khoitaomenu();
void nhapDL(process P[20]);
void FIFO(void);
void DUT(void);
void SJF(void);
void create_graph(process P[20]);
void main(){
clrscr();
int choice;
printf("\n Nhap so tien trinh yeu cau CPU: n = "); scanf("%d",&n);
nhapDL(P);
int mh=0, mode=0;
initgraph(&mh, &mode, "c:\\Tc\\BGI");
create_graph(P);
getch();
do{
choice=khoitaomenu();
switch(choice) {
case 1:{
FIFO();
break;
};
case 2:{
SJF();
break;
}
/* case 3:{
RR();
break;
}*/
case 4:{
DUT();
break;
}
case 0:{
break;
}
default:{
gotoxy(18,20);
printf("\n\t\tLoi! Hay nhap so [0-4] ");
getch();
break;
}
}
}while(choice != 0);
}
int khoitaomenu(){
int choice = -1;
clrscr();
gotoxy(22, ;
printf("***** Menu *****");
gotoxy(18, 10);
printf("1. Chiem luoc FIFO");
gotoxy(18, 12);
printf("2. Chien luoc SJF (do quyen)");
gotoxy(18, 14);
printf("3. Chien luoc Round Robin");
gotoxy(18, 16);
printf("4. Chien luoc Do uu tien");
gotoxy(18, 18);
printf("0. Thoat chuong trinh");
gotoxy(18, 20);
printf("Nhap vao lua chon cua ban:");
scanf("%d", &choice);
return choice;
}
void create_graph(process P[20])
{
int mh=0, mode=0;
initgraph(&mh, &mode, "c:\\Tc\\BGI");
if (graphresult() != grOk)
{printf("\nLoi do hoa!");
getch();
exit(1);}
setcolor(RED);
moveto(50,30);
outtext("******************* MO TA DO HOA *******************");
moveto(70,60);
outtext("******* Thuat toan dieu phoi tien trinh *******");
moveto(20,100);
outtext("Day cac tien trinh di vao theo thu tu da nhap:");
setcolor(1);
int y1=150, y2=y1+30, x1=100, x2=x1+15*P[1].txly;
rectangle(x1,y1,x2,y2);
outtextxy((x2+x1)/2, (y1+y2)/2, P[1].ten);
for (int i=2; i<=n; i++)
{
x1=100+15*P[i].tvao; x2=x1+10*P[i].txly;
y1=y2+8; y2=y1+30;
setcolor(i+1);
rectangle(x1,y1,x2,y2);
moveto((x1+x2)/2, (y1+y2)/2);
outtext(P[i].ten);
}
closegraph();
}
void nhapDL(process P[20]){
int i=1;
while (i<=n){
//clrscr();
fflush(stdin);
printf("\n\tTen tien trinh : "); gets(P[i].ten);
printf("\tThoi diem vao : "); scanf("%d",&P[i].tvao);
printf("\tThoi gian xu ly: "); scanf("%d",&P[i].txly);
printf("\tDo uu tien: "); scanf("%d",&P[i].dut);
i++;
// create_graph(P);
}
}
void FIFO(){
int i, t=0, s1=0, s2=0;
process *Q=P;
//sap xep theo thoi gian vao tang dan
for(i=1; i if (Q[i].tvao>Q[j].tvao){
process tg =Q[i];
Q[i]=Q[j];
Q[j]=tg;
}
Q[1].stt=1; // tien trinh dau tien duoc xu ly
Q[1].tcho=0;
t=Q[1].tvao + Q[1].txly;
Q[1].tluu=Q[1].txly;
j=1;
for (i=2; i<=n; i++)
if (Q[i].tvao<=t){
j++;
Q[i].stt=j;
Q[i].tcho=t-Q[i].tvao;
t=t+Q[i].txly;
Q[i].tluu=t-Q[i].tvao;
}
else{ // neu chua co tien trinh nao cho xu ly
j++;
Q[i].stt=j;
t=Q[i].tvao;
Q[i].tcho=0;
t=t+Q[i].txly;
Q[i].tluu=Q[i].tcho+Q[i].txly;
}
// sap xep theo thu tu cap phat
for(i=1; i if (Q[i].stt>Q[j].stt){
process tg =Q[i];
Q[i]=Q[j];
Q[j]=tg;
}
//out ra danh sach
clrscr();
printf("\n\t__________ Ket qua dieu phoi theo FIFO__________\n");
printf("\n\t|TTCP| Ten TT | Tvao | SD CPU | TLuu | TCho |\n");
printf("\t======================================== =====");
for (i=1; i<=n; i++){
s1=s1+Q[i].tcho;
s2=s2+Q[i].tluu;
printf("\n\t| %2d | %6s | %4d | %6d | %4d | %4d |",
i,Q[i].ten,Q[i].tvao,Q[i].txly,Q[i].tluu,Q[i].tcho);
}
printf("\n\t====================================== =======");
printf("\n\t\t\t\t\t%4d%8d",s2,s1);
printf("\n\n\tThoi gian cho TB %10.3f",(float)s1/n);
printf("\n\tThoi gian luu TB %10.3f",(float)s2/n);
printf("\n An bat ki Qhim nao de quay lai menu.");
getch();
}
void SJF(void){
int i,t,d,id,s1=0,s2=0;
process *R=P;
for (i=0; i<=n;i++) ok[i]=0;
ok[1]=-1;
t=R[1].txly; R[1].tcho=0;
R[1].stt=1; R[1].tluu=t;
j=2;
while(j<=n){
d=0; int tmin=99;
for(i=1; i<=n; i++) if(ok[i]!=-1 &&R[i].tvao<=t){
d++; ok[i]=1;}
if (d!=0){
d--;
for (i=1; i<=n; i++)
if (ok[i]==1 && R[i].txly tmin=R[i].txly;id=i;
}
ok[id]=-1; R[id].stt=j;
R[id].tcho=t-R[id].tvao; t=t+R[id].txly;
R[id].tluu=R[id].tcho + R[id].txly;
j++;
}
else{
for (i=1; i<=n; i++)
if (ok[i]==0 && R[i].txly tmin=R[i].txly;id=i;
}
ok[id]=-1; R[id].stt=j;
R[id].tcho=0; t=R[id].tvao+R[id].txly;
R[id].tluu=R[id].tcho + R[id].txly;
j++;
}
};
// sap xep theo thu tu cap phat
for(i=1; i if (R[i].stt>R[j].stt){
process tg =R[i];
R[i]=R[j];
R[j]=tg;
}
clrscr();
printf("\n\t__________ Ket qua dieu phoi SJF___________________\n");
printf("\n\t|TTCP| Ten TT | Tvao | SD CPU | TLuu | TCho |\n");
printf("\t======================================== =====");
for (i=1; i<=n; i++){
s1=s1+R[i].tcho;
s2=s2+R[i].tluu;
printf("\n\t| %2d | %6s | %4d | %6d | %4d | %4d |",
i,R[i].ten,R[i].tvao,R[i].txly,R[i].tluu,R[i].tcho);
}
printf("\n\t====================================== =======");
printf("\n\t\t\t\t\t%4d%8d",s2,s1);
printf("\n\n\tThoi gian cho TB %10.3f",(float)s1/n);
printf("\n\tThoi gian luu TB %10.3f",(float)s2/n);
getch();
printf("\n An bat ki phim nao de quay lai menu.");
}
void DUT(){
int t,d,i,id,s1=0,s2=0;
process *R=P;
for(i=1;i<=n;i++) ok[i]=0;
ok[1]=-1;
t=R[1].txly; R[1].tcho=R[1].tluu=t;
R[1].stt=1;
j=2;
d=0;
while(j<=n){
for(i=1; i<=n; i++) if(ok[i]!=-1 && R[i].tvao<=t && ok[i]==0){
d++; ok[i]=1;}
if (d!=0){
d--;
int Qmin=99;
for (i=1; i<=n; i++)
if (ok[i]==1 && R[i].dut Qmin=R[i].dut;
id=i;
}
ok[id]=-1;
R[id].stt=j;
j++;
R[id].tcho=t-R[id].tvao;
t=t+R[id].txly;
R[id].tluu=R[id].tcho+R[id].txly;
}
else{
for (i=1;i<=n;i++)
if (ok[i]!=-1){
id=i;
break;
}
ok[id]=-1;
R[id].stt=j;
j++;
R[id].tcho=0;
t=R[id].tvao+R[id].txly;
R[id].tluu=R[id].tcho+R[id].txly;
}
};
for(i=1; i if (R[i].stt>R[j].stt){
process tg =R[i];
R[i]=R[j];
R[j]=tg;
}
//out ra danh sach
clrscr();
printf("\n\t__________ Ket qua dieu phoi theo do uu tien__________\n");
printf("\n\t|TTCP| Ten TT | Tvao | SD CPU | DUT | Tcho | Tluu |\n");
printf("\t======================================== ===========");
for (i=1; i<=n; i++){
s1=s1+R[i].tcho;
s2=s2+R[i].tluu;
printf("\n\t| %2d | %6s | %4d | %6d | %3d | %4d | %4d |",
R[i].stt,R[i].ten,R[i].tvao,R[i].txly,R[i].dut,R[i].tcho,R[i].tluu);
}
printf("\n\t====================================== =============");
printf("\n\t\t\t\t\t %4d %4d",s1,s2);
printf("\n\t\tThoi gian cho TB =%5.3f",(float)s1/n);
printf("\n\t\tThoi gian luu TB =%5.3f",(float)s2/n);
getch();
printf("\n An bat ki phim nao de quay lai menu.");
}
#include
#include
#include
#include
struct process{
char ten[12];
int tvao;
int txly;
int tcho;
int tluu;
int stt;
int dut;
} P[20];
int n, j, ok[20];
int khoitaomenu();
void nhapDL(process P[20]);
void FIFO(void);
void DUT(void);
void SJF(void);
void create_graph(process P[20]);
void main(){
clrscr();
int choice;
printf("\n Nhap so tien trinh yeu cau CPU: n = "); scanf("%d",&n);
nhapDL(P);
int mh=0, mode=0;
initgraph(&mh, &mode, "c:\\Tc\\BGI");
create_graph(P);
getch();
do{
choice=khoitaomenu();
switch(choice) {
case 1:{
FIFO();
break;
};
case 2:{
SJF();
break;
}
/* case 3:{
RR();
break;
}*/
case 4:{
DUT();
break;
}
case 0:{
break;
}
default:{
gotoxy(18,20);
printf("\n\t\tLoi! Hay nhap so [0-4] ");
getch();
break;
}
}
}while(choice != 0);
}
int khoitaomenu(){
int choice = -1;
clrscr();
gotoxy(22, ;
printf("***** Menu *****");
gotoxy(18, 10);
printf("1. Chiem luoc FIFO");
gotoxy(18, 12);
printf("2. Chien luoc SJF (do quyen)");
gotoxy(18, 14);
printf("3. Chien luoc Round Robin");
gotoxy(18, 16);
printf("4. Chien luoc Do uu tien");
gotoxy(18, 18);
printf("0. Thoat chuong trinh");
gotoxy(18, 20);
printf("Nhap vao lua chon cua ban:");
scanf("%d", &choice);
return choice;
}
void create_graph(process P[20])
{
int mh=0, mode=0;
initgraph(&mh, &mode, "c:\\Tc\\BGI");
if (graphresult() != grOk)
{printf("\nLoi do hoa!");
getch();
exit(1);}
setcolor(RED);
moveto(50,30);
outtext("******************* MO TA DO HOA *******************");
moveto(70,60);
outtext("******* Thuat toan dieu phoi tien trinh *******");
moveto(20,100);
outtext("Day cac tien trinh di vao theo thu tu da nhap:");
setcolor(1);
int y1=150, y2=y1+30, x1=100, x2=x1+15*P[1].txly;
rectangle(x1,y1,x2,y2);
outtextxy((x2+x1)/2, (y1+y2)/2, P[1].ten);
for (int i=2; i<=n; i++)
{
x1=100+15*P[i].tvao; x2=x1+10*P[i].txly;
y1=y2+8; y2=y1+30;
setcolor(i+1);
rectangle(x1,y1,x2,y2);
moveto((x1+x2)/2, (y1+y2)/2);
outtext(P[i].ten);
}
closegraph();
}
void nhapDL(process P[20]){
int i=1;
while (i<=n){
//clrscr();
fflush(stdin);
printf("\n\tTen tien trinh : "); gets(P[i].ten);
printf("\tThoi diem vao : "); scanf("%d",&P[i].tvao);
printf("\tThoi gian xu ly: "); scanf("%d",&P[i].txly);
printf("\tDo uu tien: "); scanf("%d",&P[i].dut);
i++;
// create_graph(P);
}
}
void FIFO(){
int i, t=0, s1=0, s2=0;
process *Q=P;
//sap xep theo thoi gian vao tang dan
for(i=1; i
process tg =Q[i];
Q[i]=Q[j];
Q[j]=tg;
}
Q[1].stt=1; // tien trinh dau tien duoc xu ly
Q[1].tcho=0;
t=Q[1].tvao + Q[1].txly;
Q[1].tluu=Q[1].txly;
j=1;
for (i=2; i<=n; i++)
if (Q[i].tvao<=t){
j++;
Q[i].stt=j;
Q[i].tcho=t-Q[i].tvao;
t=t+Q[i].txly;
Q[i].tluu=t-Q[i].tvao;
}
else{ // neu chua co tien trinh nao cho xu ly
j++;
Q[i].stt=j;
t=Q[i].tvao;
Q[i].tcho=0;
t=t+Q[i].txly;
Q[i].tluu=Q[i].tcho+Q[i].txly;
}
// sap xep theo thu tu cap phat
for(i=1; i
process tg =Q[i];
Q[i]=Q[j];
Q[j]=tg;
}
//out ra danh sach
clrscr();
printf("\n\t__________ Ket qua dieu phoi theo FIFO__________\n");
printf("\n\t|TTCP| Ten TT | Tvao | SD CPU | TLuu | TCho |\n");
printf("\t======================================== =====");
for (i=1; i<=n; i++){
s1=s1+Q[i].tcho;
s2=s2+Q[i].tluu;
printf("\n\t| %2d | %6s | %4d | %6d | %4d | %4d |",
i,Q[i].ten,Q[i].tvao,Q[i].txly,Q[i].tluu,Q[i].tcho);
}
printf("\n\t====================================== =======");
printf("\n\t\t\t\t\t%4d%8d",s2,s1);
printf("\n\n\tThoi gian cho TB %10.3f",(float)s1/n);
printf("\n\tThoi gian luu TB %10.3f",(float)s2/n);
printf("\n An bat ki Qhim nao de quay lai menu.");
getch();
}
void SJF(void){
int i,t,d,id,s1=0,s2=0;
process *R=P;
for (i=0; i<=n;i++) ok[i]=0;
ok[1]=-1;
t=R[1].txly; R[1].tcho=0;
R[1].stt=1; R[1].tluu=t;
j=2;
while(j<=n){
d=0; int tmin=99;
for(i=1; i<=n; i++) if(ok[i]!=-1 &&R[i].tvao<=t){
d++; ok[i]=1;}
if (d!=0){
d--;
for (i=1; i<=n; i++)
if (ok[i]==1 && R[i].txly
}
ok[id]=-1; R[id].stt=j;
R[id].tcho=t-R[id].tvao; t=t+R[id].txly;
R[id].tluu=R[id].tcho + R[id].txly;
j++;
}
else{
for (i=1; i<=n; i++)
if (ok[i]==0 && R[i].txly
}
ok[id]=-1; R[id].stt=j;
R[id].tcho=0; t=R[id].tvao+R[id].txly;
R[id].tluu=R[id].tcho + R[id].txly;
j++;
}
};
// sap xep theo thu tu cap phat
for(i=1; i
process tg =R[i];
R[i]=R[j];
R[j]=tg;
}
clrscr();
printf("\n\t__________ Ket qua dieu phoi SJF___________________\n");
printf("\n\t|TTCP| Ten TT | Tvao | SD CPU | TLuu | TCho |\n");
printf("\t======================================== =====");
for (i=1; i<=n; i++){
s1=s1+R[i].tcho;
s2=s2+R[i].tluu;
printf("\n\t| %2d | %6s | %4d | %6d | %4d | %4d |",
i,R[i].ten,R[i].tvao,R[i].txly,R[i].tluu,R[i].tcho);
}
printf("\n\t====================================== =======");
printf("\n\t\t\t\t\t%4d%8d",s2,s1);
printf("\n\n\tThoi gian cho TB %10.3f",(float)s1/n);
printf("\n\tThoi gian luu TB %10.3f",(float)s2/n);
getch();
printf("\n An bat ki phim nao de quay lai menu.");
}
void DUT(){
int t,d,i,id,s1=0,s2=0;
process *R=P;
for(i=1;i<=n;i++) ok[i]=0;
ok[1]=-1;
t=R[1].txly; R[1].tcho=R[1].tluu=t;
R[1].stt=1;
j=2;
d=0;
while(j<=n){
for(i=1; i<=n; i++) if(ok[i]!=-1 && R[i].tvao<=t && ok[i]==0){
d++; ok[i]=1;}
if (d!=0){
d--;
int Qmin=99;
for (i=1; i<=n; i++)
if (ok[i]==1 && R[i].dut
id=i;
}
ok[id]=-1;
R[id].stt=j;
j++;
R[id].tcho=t-R[id].tvao;
t=t+R[id].txly;
R[id].tluu=R[id].tcho+R[id].txly;
}
else{
for (i=1;i<=n;i++)
if (ok[i]!=-1){
id=i;
break;
}
ok[id]=-1;
R[id].stt=j;
j++;
R[id].tcho=0;
t=R[id].tvao+R[id].txly;
R[id].tluu=R[id].tcho+R[id].txly;
}
};
for(i=1; i
process tg =R[i];
R[i]=R[j];
R[j]=tg;
}
//out ra danh sach
clrscr();
printf("\n\t__________ Ket qua dieu phoi theo do uu tien__________\n");
printf("\n\t|TTCP| Ten TT | Tvao | SD CPU | DUT | Tcho | Tluu |\n");
printf("\t======================================== ===========");
for (i=1; i<=n; i++){
s1=s1+R[i].tcho;
s2=s2+R[i].tluu;
printf("\n\t| %2d | %6s | %4d | %6d | %3d | %4d | %4d |",
R[i].stt,R[i].ten,R[i].tvao,R[i].txly,R[i].dut,R[i].tcho,R[i].tluu);
}
printf("\n\t====================================== =============");
printf("\n\t\t\t\t\t %4d %4d",s1,s2);
printf("\n\t\tThoi gian cho TB =%5.3f",(float)s1/n);
printf("\n\t\tThoi gian luu TB =%5.3f",(float)s2/n);
getch();
printf("\n An bat ki phim nao de quay lai menu.");
}
nguyen_tuan_phat_I12A- Tổng số bài gửi : 15
Join date : 17/02/2012
Similar topics
» Thảo luận Bài 6
» Thảo luận Bài 4
» câu 2 : Trình bày sơ đồ luân chuyển CPU giữa hai tiến trình dưới sự điều phối của hệ điều hành
» Các thuật giải điều phối tiến trình
» Các thuật giải điều phối tiến trình
» Thảo luận Bài 4
» câu 2 : Trình bày sơ đồ luân chuyển CPU giữa hai tiến trình dưới sự điều phối của hệ điều hành
» Các thuật giải điều phối tiến trình
» Các thuật giải điều phối tiến trình
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết