Professional Documents
Culture Documents
NHOM 10
Trang 1
AN NGUYN LY H IU HANH
NHOM 10
Trang 2
MC LC
TM HIU TI.........................................................................................................3
1.1 Bi cnh ti:........................................................................................................3
1.2 Mc tiu ti.........................................................................................................4
1.3 Hng gii quyt.....................................................................................................4
CHNG 2. C S L THUYT................................................................................4
2.1 Tm hiu v h iu hnh........................................................................................4
2.2 Gii thiu v tin trnh.............................................................................................6
CHNG 3. THIT K V XY DNG CHNG TRNH....................................21
3.1 Phn tch yu cu...................................................................................................21
3.2 Cc chc nng.......................................................................................................22
CHNG 4. TRIN KHAI V NH GI KT QU..............................................52
4.1 Mi trng trin khai:...........................................................................................52
4.2 Kt qu v chc nng ca chng trnh.................................................................53
4.3 nh gi kt qu....................................................................................................56
DANH SCH HNH NH
LI NI U
AN NGUYN LY H IU HANH
NHOM 10
Trang 3
TM HIU TI
1.1 Bi cnh ti:
Trc y tu tng thi im, my tnh c xc nh mt nhim v chnh; tt c
cc chng trnh c b li thnh gi (paket) v c gi i lin tc. iu c gi
l x l ng gi (pile processing) hay qun l l (batch manager). Ngy nay, khng ch
AN NGUYN LY H IU HANH
NHOM 10
Trang 4
c mt chng trnh chy trn my tnh, m nhiu chng trnh cng thc hin (multitasking). Cng nh th, khng ch c mt ngi s dng lm vic, m nhiu ngi s
dng cng lm vic (multi- user). hn ch s tranh chp gia chng vic dng my
tnh, do s phn b cc phng tin iu hnh phi c iu chnh trn chng
trnh.
Thm vo , chng trnh phi c lu tr khi cn thit s dng phng tin iu
hnh no: khng gian nh, th h CPU, dng lng CPU T ta hiu, tin trnh l
thng tin trng thi ca cc phng tin iu hnh i vi mt chng trnh (thng gi
l mt Job).
Mt tin trnh ny c th sinh ra mt tin trnh khc, khi ngi ta gi tin trnh u
l tin trnh cha, cn tin trnh c sinh ra l tin trnh con.
Cng mt lc c th c nhiu tin trnh c nhp vo h thng,lc ny cn phi c c
ch cac tin trnh thc hin theo th t.
Mt h thng a chng trnh (multi-programming system) cho php thc hin ng
thi nhiu chng trnh v nhiu tin trnh. Mt chng trnh (gi l mt job ) cng c
th t pht sinh ra nhiu tin trnh.
1.2 Mc tiu ti
Hiu r v tin trnh c bit chin lc mt hng i nhiu tin trnh ch phn
phi x l.Xem c ch thuc loi tin trnh no v tm hiu v trng thi ca cc c ch
1 hng i v c ch iu phi.Cch qun l tin trnh mt hng i.
1.3 Hng gii quyt
Tm hiu r v tin trnh:Tin trnh cc loi tin trnhm hnh tin
trnhcch thc hin m hnhqun l tin trnhc ch iu phi mt hng i cc
tin trnh.
Ngn ng s dng:
S dng ngn ng lp trnh C++
CHNG 2.
C S L THUYT
AN NGUYN LY H IU HANH
NHOM 10
Trang 5
AN NGUYN LY H IU HANH
NHOM 10
Trang 6
u im :
a ra c cch x l ca cc tin trnh .
Chng trnh n gin.
Khuyt im :
Cha nu r vic tp hp iu phi gia cc tin trnh.
Hng nghin cu gii thiu.
2.2 Gii thiu v tin trnh
2.2.1 Tin trnh l g?
Tt c cc my tnh hin i u c th thc hin nhiu vic cng mt lc. Trong
khi thc hin chng trnh ca ngi s dng, my tnh c th c d liu t a v a
ra mn hnh hoc my in. Trong mi trng a chng trnh (multiprogramming
system), mt CPU c th chuyn t chng trnh ny sang chng trnh khc, thc hin
mi chng trnh trong khong 1% hoc 1/10 mili giy. Nu ni chnh xc, th ti mt
thi im, CPU ch thc hin c mt chng trnh. Nhng nu xt trong khong thi
gian phn trm giy th CPU c th thc hin nhiu cng vic.
h tr hot ng a nhim,h thng my tnh cn phi c kh nng thc hin
nhiu tc v x l ng thi nhng vic iu khin hot ng song hnh cp phn
cng l rt kh khn.V vy,cc nh thit k h iu hnh xut mt m hnh song
hnh gi lp bng cch chuyn i b x l qua li gia cc chng trnh duy tr hot
ng ca nhiu chng trinh phai cng mt thi im.Trong m hnh ny cc chng
trnh ca h thng c t chc thnh cc tin trnh(process).
Nh vy c th coi tin trnh l mt chng trnh ang x l,n s dng mt con
tr lnh,tp cc thanh ghi v cc bin. hon thnh nhim v ca mnh,cc tin trnh
cn c th yu cu mt s ti nguyn h thng nh: CPU,b nh v cc thit b.
Chng ta cn phn bit r tin trnh v chng trnh.Chng trnh l mt thc th
th ng cha cc ch th iu khin my tinh thi hnh mt tc v c th no .Khi
thc hin cc ch th ny ,chng trnh c chuyn thnh cc tin trnh l mt thc th
hot ng,vi con tr lnh xc nh km them ti nguyn phc v cho hot ng.
Vy tm li ,tin trnh l mt b phn ca mt chng trnh ang thc hin, n v
thc hin tin trnh l processer.
Hay c th ni cch khc :
Saltzer: Tin trnh l mt chng trnh do mt processor logic thc hin.
Horning & Rendell: Tin trnh l mt qu trnh chuyn t trng thi ny sang trng thi
khc di tc ng ca hm hnh ng, xut pht t mt trng thi ban u no .
AN NGUYN LY H IU HANH
NHOM 10
Trang 7
AN NGUYN LY H IU HANH
NHOM 10
Trang 8
AN NGUYN LY H IU HANH
NHOM 10
Trang 9
L cc tin trnh hot ng song song s dng chung ti nguyn theo nguyn tc ln
lt, mi tin trnh sau mt khong thi gian chim gi ti nguyn phi t ng tr li
ti nguyn cho tin trnh kia.
AN NGUYN LY H IU HANH
NHOM 10
Trang 10
Trong qua trinh sng mt tin trinh thay i trang thai do nhiu nguyn nhn
nh: phai ch mt s kin nao o xay ra,hay i mt thao ta nhp xut hoan tt,buc
phai dng hoat ng do ht thi gian x ly
Tai mt thi im mt tin trinh co th nhn mt trong cac trang thai sau y:
Mi tao(new): tin trinh ang c tao lp
Running: cac chi thi cua tin trinh ang c x ly.
Blocked: tin trinh ch c cp phat tai nguyn,hay ch mt s kin xay
ra.
Ready(ready): tin trinh ch c cp phat CPU x ly.
Waiting (i):tin trnh phi dng v thiu ti nguyn hoc ch mt s kin
no .
Kt thuc(halt) : tin trinh hoan tt x ly.
Cc trng thi ca tin trnh c biu din qua s sau:
Tai mt thi im chi co mt tin trinh co th nhn trang thai running trn mt b
x ly bt ky.Trong khi o,nhiu tin trinh co th trang thai blocked hay ready.
Cac cung chuyn tip trong s trang thai biu din 6 s chuyn trang thai co th
xay ra trong cac iu kin sau:
Tin trinh mi tao c a vao h thng
B iu phi cp phat cho tin trinh mt khoang thi gian s dung CPU.
Tin trinh kt thuc
Tin trinh yu cu mt tai nguyn cha c ap ng vi tai nguyn cha
c sn sang cp phat tai thi im o,hoc tin trinh phai ch mt s
kin hay thao tac nhp xut.
B iu phi chon mt tin trinh khac ch x ly.
Tai nguyn ma tin trinh yu cu tr nn sn sang cp phat,hay s kin
thao tac nhp xut tin trinh ang i hoan tt.
Theo thi gian hoat ng tin trinh se thay i trang thai.Co 2 cp trang thai la:
Trang thai vi m: do h iu hanh t ra quan ly process.
AN NGUYN LY H IU HANH
NHOM 10
Trang 11
Trang thai vi m:trang thai chi tit sau tng lnh may c thc thi.
Trang thai vi m bao gm: running (ang chim CPU va chay),ready ( ang
ch CPU ),blocked( bi giam vi ch CPU).
S trang thai :
AN NGUYN LY H IU HANH
NHOM 10
Trang 12
AN NGUYN LY H IU HANH
NHOM 10
Trang 13
AN NGUYN LY H IU HANH
NHOM 10
Trang 14
Cc c im ca tin trnh:
iu phi hot ng ca cc tin trnh l mt vn rt phc tp,i hi h iu
hnh khi gii quyt phi xem xt cc yu t khc nhau c th t c mc tiu
ra.Mt s c tnh cn c quan tm nh tiu chun iu phi:
Tnh hng nhp xut ca tin trnh:khi mt tin trnh nhn c CPU,ch yu n
ch s dng CPU cho n khi pht sinh mt yu cu nhp xut.Hot ng ca cc tin
trnh nh th thng bao gm nhiu lt s dng CPU,mi lt trong mt thi gian kh
ngn.
Tnh hng x l ca tin trnh: khi mt tin trnh nhn dc CPU,n c khuynh
hng s dng CPU n khi ht thi gian dnh cho n.Hot ng ca tin trnh nh th
thng bao gm mt s t lt s dng CPU,nhng mi lt trong mt thi gian di.
Tin trnh tng tc hay x l theo l: ngi s dungjtheo kiu tng tc thng
yu cu c hi p tc thi i vi cc yu cu ca h,trong khi cc tiesn trnh ca
tc v c x l theo l ni chungcos th tr hon trong mt thi gian chp nhn c.
u tin ca tin trnh: Cc tin trnh c th c phn cp theo mt nh gi no
,mt cch hp l,cc tin trnh quan trng hn(c u tin hn) cn c u tin
hn.
Thi gian s dng CPU ca tin trnh:Mt s quan im u tin chn nhng tin
trnh s dng CPU nhiu thi gian nht v hy vng chng s cn t thi gian nht
hon tt v ri khi h thng.Tuy nhin cng c quan im cho rng cc tin trnh nhn
c CPU trong t thi gian l nhng tin trnh phi ch lu nht,do vy u tin chn
chng.
Thi gian cn li tin trnh cn hon tt: c th gim thiu thi gian ch i trung
bnh ca cc tin trnh bng cch cho cc tin trnh cn t thi gian nht hon tt c
thc hin trc.Tuy nhin ng tic l rt him khi bit c tin trnh cn bao nhiu
thi gian na kt thc x l.
2.2.6.2 C ch iu phi c quyn v khng c quyn
C ch iu phi c quyn
Nguyn l: c ch ny cho php mt tin trnh khi nhn c CPU s c quyn c
chim CPU n khi hon tt x l hoc t gii phng CPU.Khi quyt nh iu phi
s xy ra cc trng hp sau.
Tin trnh chuyn t trng thi running blockit
Kt thc tin trnh.
Cc gii thut c quyn d ci t thut ton.Tuy nhin li khng thch hp cho h
thng tng qut nhiu ngi dng.V nu cho tin trnh thi gian chim gi CPU ty
th s ngn cn qu trnh x l ca cc tin trnh cn li.
C ch iu phi khng c quyn.
Ngc vi nguyn l c quyn ,iu phi theo nguyn l khng c quyn cho php
tm dng hot ng ca tin trnh ang sn sang x l.Khi mt tin trnh nhn CPU n
AN NGUYN LY H IU HANH
NHOM 10
Trang 15
vn c s dng CPU cho n khi hon tt hoc t nguyn gii phng CPU,nhng mt
tin trnh khc c u tin cao hn c th dnh quyn s dng CPU ca tin trnh ban
u.Nh vy tin trnh c th dng hot ng bt c lc no m khng c bo trc
tin trnh khc x l.Cc quyt nh iu phi xy ra khi:
Tin trnh chuyn t trng thi running blocked.
Mt tin trnh ch trng thi x l.
Chuyn t trng thi ch(blocked) ready.
Khi mt tin trnh kt thc.
Cc thut ton iu phi theo nguyn tc khng c quyn ngn cn tnh trng mt
tin trnh c chim CPU.
2.2.6.3 T chc iu phi
Cc loi danh sch s dng iu phi tin trnh
H iu hnh s dng hai loi danh sch thc hin iu phi cc tin trnh l
danh sch sn sng(ready list) v danh sch ch i(waiting list).
Khi mt tin trnh bt u i vo h thng,n c chn vo danh sch tc v(job
list).Danh sch ny bao gm tt c cc tin trnh ca h thng.Nhng ch cc tin trnh
ang thng tr trong b nh chnh v trng thi sn sng tip nhn CPU hot
ng mi c a vo danh sch sn sng.
B iu phi s chn mt tin trnh trong danh sch sn sng v cp CPU cho tin
trnh .Tin trnh c cp CPU s thc hin x l,v c th chuyn sang trng thi
ch khi xy ra cc s kin nh i mt thao tc nhp/xut hon tt,yu cu ti nguyn
cha c tha mn,yu cu tm dngKhi tin trnh s c chuyn sang mt
danh sch ch i.
H iu hnh ch s dng mt danh sch sn sng cho ton h thng,nhng mi mt
ti nguyn(thit b ngoi vi) c mt danh sch ch i ring bao gm cc tin trnh ch
c cp pht ti nguyn .
AN NGUYN LY H IU HANH
NHOM 10
Trang 16
Qu trnh x l ca mt tin trnh tri qua nhng chu k chuyn i qua li gia
nhng danh sch sn sng v danh sch ch i.S di y m t s iu phi cc
tin trnh da trn cc danh sch ca h thng
Thot u tin trnh mi c t trong danh sch cc tin trnh sn sng(ready
list),n s i trong danh sch ny cho n khi c chn cp pht CPU v bt x
l.Sau c th xy ra mt trong cc tnh hung sau:
Tin trnh pht sinh mt yu cu mt ti nguyn m h thng cha th p ng,khi
tin trnh s c chuyn sang danh sch cc tin trnh ang ch ti nguyn tng
ng.
Tin trnh c th b bt buc tm dng x l do mt l do ngt xy ra,khi tin
trnh c a tr li vo danh sch sn sng ch c cp CPU cho lt tip theo.
AN NGUYN LY H IU HANH
NHOM 10
Trang 17
Trng hp u tin,tin trnh cui cng s chuyn t trng thi blocket sang trng
thasiready v li c a vo danh sch sn sng.Tin trnh lp li cho n khi hon tt
tc v th c h thng hy b khi mi danh sch iu phi.
Cc cp iu phi.
Thc ra cng vic iu phi c h iu hnh thc hin hai mc : iu phi
tc v v iu phi tin trnh.
iu phi tc v
Quyt inh la chn tc v no c a vo h thng,v np nhng tin trnh ca
tc v vo b nh chnh thc hin.Chc nng iu phi tc v quyt nh mc
a chng ca h thng(s lng tin trnh trong b nh chnh).Khi h thng to lp
tin trinh,hay c mt tin trnh kt thc x l chc nng v iu phi tc v c tn sut
hot ng thp.
h thng hot ng tt,b iu phi tc v cn bit tnh cht ca tin trnh l
hng nhp xut hay hng x l.Mt tin trnh c gi l hng nhp xut nu n
ch yu ch s dng CPU thc hin thao tc nhp xut.Ngc li mt tin trnh c
gi l hng x l nu n ch yu ch s dng CPU thc hin cc thao tc tnh
ton. cn bng hot ng ca CPU v cc thit b ngoi vi,b iu phi tc v nn
la chn cc tin trnh np vo b nh sao cho h thng l s pha trn hp l gia
cc tin trnh hng nhp xut v cc tin trnh hng x l.
AN NGUYN LY H IU HANH
NHOM 10
Trang 18
Cc chin lc iu phi
Phn tch cc iu phi s dng trong c ch mt hng i cc tin trnh:
Chin lc FIFO
Nguyn tc: CPU c cp pht cho tin trnh u tin trong danh sch sn sng c
yu cu, l tin trnh c a vo h thng sm nht. y l mt thut ton iu phi
theo nguyn tc c quyn. Mt khi CPU c cp pht cho tin trnh, CPU ch c
tin trnh t nguyn gii phng khi kt thc x l hay khi c mt yu cu xut/ nhp.
Chin lc ny th thi gian ch trung bnh khng t cc tiu v bin i ng k i
vi cc gi tr v thi gian yu cu x l v th t khc nhau ca ccd tin trnh trong
danh sch sn sng. C th xy ra hin tng tch lu thi gian ch, khi tt c cc tin
trnh phI ch i mt tin trnh c yu cc thi gian di kt thc x l .
Gii thut ny c bit khng ph hp vi cc h phn chia thi gian, trong cc h ny,
cn cho php mi tin trnh c cp pht CPU u n trong tng khong thi gian.
Vid d:
Tin trnh
P1
t/g x l
24
3
3
P2
P3
AN NGUYN LY H IU HANH
NHOM 10
Thi im
24
Trang 19
27| 30
Tin trnh P1
Thi im 0
P2
4
P3
7
P1
10
P1
14
P1
18
P1
22
P1
26
AN NGUYN LY H IU HANH
NHOM 10
Trang 20
Khi h thng pht sinh mt tin trnh ready mi, th b phn iu phi s so snh
u tin ca tin trnh mi pht sinh vi u tin ca tin trnh ang s hu processor
(tm gi l tin trnh hin ti). Nu tin trnh mi c u tin thp hn tin trnh hin
ti th b phn iu phi s chn n vo ready list ti v tr thch hp. Nu tin trnh mi
c u tin cao hn tin trnh hin ti th b iu phi s thu hi processor t tin
trnh hin ti cp cho tin trnh mi yu cu, nu l iu phi khng c quyn, hoc
chn tin trnh mi vo ready list ti v tr thch hp, nu l iu phi c quyn.
Chin lc ny cng phi s dng ready list, v ready list lun c xp theo
th t gim dn ca u tin k t u danh sch. iu ny c ngha l tin trnh
c chn cp processor l tin trnh u ready list.
V d: Nu h iu hnh cn cp processor cho 3 tin trnh P1, P2, P3 vi u tin v
khong thi gian mi tin trnh cn processor c m t trong bng sau:
Tin trnh u tin thi gian x l
P1
3
24
P2
1
3
P3
2
3
Th th t cp processor (theo nguyn tc c quyn) cho cc tin trnh ln
lt l:
Tin trnh P1
Thi im 0
P2
1
P3
4
P1
7
AN NGUYN LY H IU HANH
NHOM 10
Trang 21
CHNG 3.
AN NGUYN LY H IU HANH
NHOM 10
Trang 22
Ci t thut ton
3.2.1.1 ti :
M phng chin lc mt hng i nhiu tin trnh ch phn phi x l.
3.2.2 Phn tch :
Nhp vo tn cc tin trnh, thi gian n v thi gian x l. Phn tch u tin
ca cc tin trnh v th t c a vo danh sch sn sng.Tin trnh no c u
tin s thc hin trc ,nu mt tin trnh ang x l m c mt tin trnh khc c
np vo danh sch sn sng,nu tin trnh mi c u tin th s c cung cp CPU
v tin trnh ang x l c xp vo hng i.Sau in ra th t thc hin ca cc tin
trnh.
S dng cc thut ton FIFO.SJF, u tin,RR.
3.2.3 M t bi ton.
Chng ta se xy dng chng trnh n gin bng C.M phng kt qu x l ca cc
tin trnh thng qua thut ton FIFO,SJF,U TIN,RR.
u vo: Cc tin trnh p0,p1,p2,p3,.
Thi gian n ca mi tin trnh.
Thi gian x l ca tin trnh.
u ra: Thi gian mi tin trnh s dng CPU, thi gian ch ca mi tin trnh
trong CPU,v thi gian ch trung bnh ca c qu trnh x l.
-Khi to mt i tng l mt tin trnh cha cc thuc tnh:
tgvao,tgxuly,tentt,tgcho,stt.
Vi thut ton RR : cn c thi gian quan tum.
FIFO:
#include <stdio.h>
#include <conio.h>
typedef struct PR{ char ten[12];
int txly;
int tcho;
int tluu;
int stt;
} PROCESS;
PROCESS a[20];
int n, j, ok[20];
float tb;
void In(PROCESS a[20]);
void FIFO();
AN NGUYN LY H IU HANH
NHOM 10
Trang 23
void Fuctions1();
//Main()
main()
{
Fuctions1();
}
//Fuctions()
void Fuctions1()
{
do{
printf("\n\t----------------GIAI THUAT DINH THOI FIFO---------------\n\n");
printf("\n So luong tien trinh: n = ");
scanf("%d",&n);
}while((n<=0)||(n>20));
In(a);
FIFO();
getch();
}
//In()
void In(PROCESS a[20])
{
int i=1;
while (i<=n)
{
fflush(stdin);
printf("\n\tTen tien trinh : ");
gets(a[i].ten);
printf("\tThoi gian xu ly: ");
scanf("%d",&a[i].txly);
i++;
}
}
//FIFO
void FIFO()
{
printf("\n\nthuat toan FIFO");
int i,t,d,id,tt=0;
PROCESS *k=a;
for (i=0; i<=n;i++) ok[i]=0;
ok[0]=-1;
AN NGUYN LY H IU HANH
NHOM 10
Trang 24
t=k[0].txly;
k[0].tcho=0;
k[0].stt=1;
k[0].tluu=t;
j=1;
while(j<=n)
{
d=0;
int tmin=99;
for(i=1; i<=n; i++)//dua tien trinh vao cpu
if(ok[i]!=-1)
{
d++;
ok[i]=1;
}
if (d!=0)
{
d--;
id=j;
}
ok[id]=-1;
k[id].stt=j;
k[id].tcho=t;
t=t+k[id].txly;
k[id].tluu=k[id].tcho + k[id].txly;
j++;
};
printf("\n\t----------------GIAI THUAT DINH THOI FIFO---------------\n\n");
printf("\n\t\t|TT | Ten TT | SD CPU | Tgluu| TgCho|\n");
printf("\t\t----------------------------------------");
for (i=1; i<=n; i++)
{
printf("\n\t\t| %2d | %6s | %6d | %4d | %4d |",
i,k[i].ten,k[i].txly,k[i].tluu,k[i].tcho);
tt=tt+k[i].tcho;
}
tb=(float)tt/n;
printf("\nthoi gian cho trung binh la: %.2f",tb);
AN NGUYN LY H IU HANH
NHOM 10
Trang 25
getch();
}
// Vi thut ton ny tin no vo trc s c x l trc.Tin trnh no vo s c
gn vi ch s id=j; v x l.
Vi 3 thut ton cn li do phi s dng hng i,nn thut ton c s dng danh
sch lin kt.Trc ht thun tin vic thao tc trn danh sch lin kt,cn to mt
lp cc hm thao tc trn danh sch lin kt.
#include<conio.h>
#include<iostream>
#include<stdlib.h>
typedef struct PR{
char ten[10]; //<<<--int tvao;
int txuly;
int uutien;
int tcho;
int tluu;
int stt;
} PROCESS;
struct process_element{
int stt;
int tvao;
int txuly;
int ttong;
process_element *next;
};
typedef process_element *process_list;
//danh sch hang doi cac tien trinh
struct gantt_element{
int stt;
int tgian;
int tbatdau;
gantt_element *next;
};
typedef gantt_element *table_gantt;
//tao bang gantt
void read_data_from_file(char fileName[], PROCESS a[], int *n, int *quantum)
{
FILE *file;
if( (file=fopen(fileName,"r")) == NULL )
AN NGUYN LY H IU HANH
NHOM 10
Trang 26
{
printf("File not exist...");
getch();
exit(1);
}
char *s;
s=(char *)calloc(10,sizeof(char)); // :::|||
int num;
fscanf(file,"%d",n);
for(int i=0;i<*n;i++) a[i].stt=i;
for(int i=0;i<*n;i++)
{
fscanf(file,"%s",s);
strcpy(a[i].ten,s);
}
for(int i=0;i<*n;i++)
{
fscanf(file,"%d",&num);
a[i].tvao=num;
}
for(int i=0;i<*n;i++)
{
fscanf(file,"%d",&num);
a[i].txuly=num;
}
for(int i=0;i<*n;i++)
{
fscanf(file,"%d",&num);
a[i].uutien=num;
}
fscanf(file,"%d",&num);
*quantum=num;
fclose(file);
}
//overloading function
void read_data_from_file(char fileName[], PROCESS a[], int *n)
{
FILE *file;
if( (file=fopen(fileName,"r")) == NULL )
AN NGUYN LY H IU HANH
NHOM 10
Trang 27
{
printf("File not exist...");
getch();
exit(1);
}
char *s;
s=(char *)calloc(10,sizeof(char)); // :::|||
int num;
fscanf(file,"%d",n);
for(int i=0;i<*n;i++) a[i].stt=i;
for(int i=0;i<*n;i++)
{
fscanf(file,"%s",s);
strcpy(a[i].ten,s);
}
for(int i=0;i<*n;i++)
{
fscanf(file,"%d",&num);
a[i].tvao=num;
}
for(int i=0;i<*n;i++)
{
fscanf(file,"%d",&num);
a[i].txuly=num;
}
for(int i=0;i<*n;i++)
{
fscanf(file,"%d",&num);
a[i].uutien=num;
}
fclose(file);
}
void show_process_all(PROCESS a[], int n)
{
printf("\n");
printf("n=%d\n",n);
printf("Ten tien trinh: ");
for(int i=0;i<n;i++) printf("%6s",a[i].ten);
printf("\nThoi gian vao: ");
AN NGUYN LY H IU HANH
NHOM 10
Trang 28
AN NGUYN LY H IU HANH
NHOM 10
Trang 29
AN NGUYN LY H IU HANH
NHOM 10
Trang 30
}
//xoa tien trinh dau danh sach
void delete_element_in_head_process_list(process_list &list)
{
list=(*list).next;
}
//xoa tien trinh tail(o cuoi danh sach :D)
void delete_element_in_tail_process_list(process_list &list)
{
process_list tem=list;
while( (*tem).next!= NULL ) tem=(*tem).next;
(*tem).next=NULL;
}
//xoa mot tien trinh truoc mot tien trinh khac(khong phai dau danh sach)
void delete_element_before_node_process_list(process_list &list, process_list &trace)
{
if( list==trace ) list=NULL;
if( (*list).next==trace ) list=trace;
else
{
process_list tem=list;
while( (*(*tem).next).next!=trace) tem=(*tem).next;
(*tem).next=trace;
}
}
void delete_element_after_node_process_list(process_list &list, process_list &trace)
{
if( trace!=NULL ) (*trace).next=(*(*trace).next).next;
}
void swap_node_process_list(process_list &l1, process_list &l2)
{
int stt=(*l2).stt;
int tvao=(*l2).tvao;
int txuly=(*l2).txuly;
int ttong=(*l2).ttong;
(*l2).stt=(*l1).stt;
(*l2).tvao=(*l1).tvao;
(*l2).txuly=(*l1).txuly;
(*l2).ttong=(*l1).ttong;
AN NGUYN LY H IU HANH
NHOM 10
Trang 31
(*l1).stt=stt;
(*l1).tvao=tvao;
(*l1).txuly=txuly;
(*l1).ttong=ttong;
}
//
//circle list
//
void make_circle_process_list(process_list &list)
{
process_list tem=list;
while( (*tem).next!=NULL) tem=(*tem).next;
(*tem).next=list;
}
void delete_element_before_node_circle_process_list(process_list &list)
{
process_list tem=list;
while( (*(*tem).next).next != list ) tem=(*tem).next;
(*tem).next=list;
}
void add_element_after_node_circle_process_list(process_list &list,int stt, int tvao, int
txuly, int ttong)
{
process_list node=new process_element;
make_node_process(node,stt,tvao,txuly,ttong);
(*node).next=(*list).next;
(*list).next=node;
}
// cac ham lam viec voi danh sach lien ket
// table_gantt
//
//
void show_table_gantt(table_gantt g)
{
table_gantt tem=g;
printf("\n");
printf("\tstt\ttbatdau\t tgian\n");
while(tem!=NULL)
{
AN NGUYN LY H IU HANH
NHOM 10
Trang 32
printf("%10d%10d%10d\n",(*tem).stt,(*tem).tbatdau,(*tem).tgian);
tem=(*tem).next;
}
printf("\n");
}
void make_node_gantt(table_gantt &node,int stt, int tbatdau, int tgian)
{
(*node).stt=stt;
(*node).tgian=tgian;
(*node).tbatdau=tbatdau;
(*node).next=NULL;
}
void add_element_to_head_table_gantt(table_gantt &gantt, int stt, int tbatdau, int tgian)
{
table_gantt node=new gantt_element;
make_node_gantt(node,stt,tbatdau,tgian);
(*node).next = gantt;
gantt=node;
}
void add_element_to_tail_table_gantt(table_gantt &gantt,int stt, int tbatdau, int tgian)
{
table_gantt node=new gantt_element;
make_node_gantt(node,stt,tbatdau,tgian);
if(gantt==NULL) gantt=node;
else
{
table_gantt tail=gantt;
while( (*tail).next != NULL) tail=(*tail).next;
(*tail).next=node;
}
}
void add_element_before_node_table_gantt(table_gantt &gantt, table_gantt &trace, int
stt, int tbatdau, int tgian)
{
if(gantt==trace) add_element_to_head_table_gantt(gantt,stt,tbatdau,tgian);
else
{
table_gantt tem=gantt;
while( (*tem).next != trace ) tem=(*tem).next;
table_gantt node=new gantt_element;
make_node_gantt(node,stt,tbatdau,tgian);
AN NGUYN LY H IU HANH
NHOM 10
Trang 33
(*node).next=trace;
(*tem).next=node;
}
}
void add_element_after_node_table_gantt(table_gantt &gantt, table_gantt &trace, int
stt, int tbatdau, int tgian)
{
if( (*trace).next==NULL ) add_element_to_tail_table_gantt(gantt,stt,tbatdau,tgian);
else
{
table_gantt node=new gantt_element;
make_node_gantt(node,stt,tbatdau,tgian);
(*node).next=(*trace).next;
(*trace).next=node;
}
}
void delete_element_in_head_table_gantt(table_gantt &gantt)
{
gantt=(*gantt).next;
}
void delete_element_in_tail_table_gantt(table_gantt &gantt)
{
table_gantt tem=gantt;
while( (*tem).next!= NULL ) tem=(*tem).next;
(*tem).next=NULL;
}
void delete_element_before_node_table_gantt(table_gantt &gantt, table_gantt &trace)
{
if( gantt==trace ) gantt=NULL;
if( (*gantt).next==trace ) gantt=trace;
else
{
table_gantt tem=gantt;
while( (*(*tem).next).next!=trace) tem=(*tem).next;
(*tem).next=trace;
}
}
AN NGUYN LY H IU HANH
NHOM 10
Trang 34
AN NGUYN LY H IU HANH
NHOM 10
Trang 35
}
printf("\n");
}
//in ra thoi gian cho, thoi gian luu
void show_count_time(PROCESS a[], int n)
{
int sum=0;
for(int i=0;i<n;i++) sum+=a[i].tcho;
printf("|Ten tien trinh | Thoi gian cho | Thoi gian luu | tg su dung CPU| \n");
printf("|-----------------------------------------------------------------------|\n");
for(int i=0;i<n;i++) printf("|\t%s\t|\t%4d\t
|\t%4d\t\t|
%4d\t|\n",a[i].ten,a[i].tcho,a[i].tluu,a[i].txuly);
printf("|-----------------------------------------------------------------------|\n");
printf("\nThoi gian cho trung binh = %d/%d = %d\n",sum,n,sum/n);
}
void tinh_tcho_tluu(table_gantt gantt, PROCESS a[], int n)
{
table_gantt tem=gantt;
int time=0;
int mark_last_time_excute[n];
int i;
for(i=0;i<n;i++) mark_last_time_excute[i]=a[i].tvao;
for(i=0;i<n;i++) a[i].tcho=0;
while( tem!=NULL)
{
i=(*tem).stt;
if(i!=-1)
{
a[i].tcho+=time-mark_last_time_excute[i];//tinh thoi gian cho
time+=(*tem).tgian;
a[i].tluu=time;
//tim thoi gian luu lai he thong
mark_last_time_excute[i]=time;
}
else time+=(*tem).tgian;
tem=(*tem).next;
}
for(i=0;i<n;i++) a[i].tluu-=a[i].tvao;
}
RR
#include"pre.cpp"
\t
AN NGUYN LY H IU HANH
NHOM 10
Trang 36
#include<conio.h>
#include<iostream>
#include<stdlib.h>
PROCESS a[20];
int n; //number of processes
int quantum;
process_list queue;
table_gantt gantt;
void round_robin()
{
int i;
int time=0;
process_list buffer;
//tao buffer
//dua cac process tu a[] vao buffer
buffer=NULL;
for(i=0;i<n;i++)
{
add_element_to_tail_process_list(buffer,a[i].stt,a[i].tvao,a[i].txuly,a[i].tvao+a[i].
txuly);
}
//sap xep tang gian buffer theo tvao
process_list tem_p1=buffer, tem_p2;
while(tem_p1!=NULL)
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao<(*tem_p1).tvao )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
gantt=NULL;
queue=NULL;
AN NGUYN LY H IU HANH
NHOM 10
Trang 37
add_element_to_tail_process_list(queue,-1,0,0,0);
make_circle_process_list(queue);
//tao bieu do gantt
//loai node co tconlai=0
//table_gantt node_g, tem_g;
//tem_g=new gantt_element;
//gantt=tem_g;
do
{
//kiem tra trong buffer process nao co tvao <= time thi cho vao hang doi
while( buffer!=NULL && (*buffer).tvao <= time )
{
if( (*queue).txuly==0 ) //(truong hop queue rong) tao queue moi gom 1
phan tu
{
queue=NULL;
add_element_to_tail_process_list( queue, (*buffer).stt,(*buffer).tvao,
(*buffer).txuly, (*buffer).ttong);
make_circle_process_list(queue);
buffer=(*buffer).next;
}
else
{
//dua process vao queue
//dua vao ngay sau queue (de xu ly ngay lan tiep theo)
//neu (*queue).next chua xu ly lan nao(co txuly nhu cu) thi dua vao sau
(*queue).next
process_list tem=queue;
while( (*(*tem).next).txuly == a[(*(*tem).next).stt].txuly && (*tem).next
!= queue )
tem=(*tem).next;
add_element_after_node_circle_process_list(tem,(*buffer).stt,
(*buffer).tvao,(*buffer).txuly,(*buffer).ttong);
buffer=(*buffer).next;
//neu queue da xu ly va queue.next chua xu ly thi nhuong cho queue.next
//biet duoc bang cach so sanh txuly
int id1=(*queue).stt, id2=(*(*queue).next).stt, txl1=(*queue).txuly,
txl2=(*(*queue).next).txuly;
if( txl1 < a[id1].txuly && txl2 == a[id2].txuly ) queue=(*queue).next;
AN NGUYN LY H IU HANH
NHOM 10
Trang 38
}
}
//neu queue rong va buffer.tvao > time
//cho cpu "nghi viec" trong khoang thoi gian (tvao-time)
//dua process tiep theo gan nhat vao
if( (*queue).txuly==0 && buffer!=NULL)
{
add_element_to_tail_table_gantt(gantt,-1,time,(*buffer).tvao-time);
time=(*buffer).tvao;
queue=NULL;
add_element_to_tail_process_list(queue,(*buffer).stt,(*buffer).tvao,
(*buffer).txuly,(*buffer).ttong);
make_circle_process_list(queue);
buffer=(*buffer).next;
}
//xu ly process o hang doi queue
//dua them node vao bang gantt
//(doan chuong trinh chinh xu ly cac process)
if( (*queue).tvao <= time )
{
if( quantum>=(*queue).txuly )
{
add_element_to_tail_table_gantt(gantt,(*queue).stt,time,
(*queue).txuly);
time+=(*queue).txuly;
(*queue).txuly=0;
queue=(*queue).next;
delete_element_before_node_circle_process_list(queue);
}
else
{
add_element_to_tail_table_gantt(gantt,(*queue).stt,time,quantum);
(*queue).txuly-=quantum;
queue=(*queue).next;
time+=quantum;
}
}
} while( (*queue).txuly!=0 || buffer !=NULL ); //khi tconlai=0 tuc la queue rong (do
tinh chat danh sach lien ket vong phai co >=1 phan tu)
AN NGUYN LY H IU HANH
NHOM 10
Trang 39
}
main()
{
read_data_from_file("input2.txt",a,&n,&quantum);
show_process_all(a,n);
printf("\nquantum=%d\n",quantum);
printf("\n");
round_robin();
//printf("\ngantt:\n");
//show_table_gantt(gantt);
printf("\nbang thoi gian cap phat cpu:\n");
show_CPU_time_table(gantt,a);
tinh_tcho_tluu(gantt, a ,n);
printf("\n");
show_count_time(a, n);
getch();
}
Thut ton u tin khng c quyn
//cai nao uu tien cao hon giai quyet truoc ngay khi co the
#include"pre.cpp"
#include<conio.h>
#include<iostream>
#include<stdlib.h>
PROCESS a[20];
int n;
process_list list;
table_gantt gantt;
//ttong la do uu tien :">
//dua cac process vao buffer
//sap xep buffer
//tang dan theo tvao, cung tvao thi tang dan theo do uu tien
//
//tim tiep uutien lon hon gan nhat xu ly truoc
//phan chua xu ly dua vao stack
void UuTien_KhongDocQuyen()
{
AN NGUYN LY H IU HANH
NHOM 10
Trang 40
int i;
int time;
process_list buffer=NULL, stack=NULL;
gantt=NULL;
//dua process tu a[] vao buffer
for(i=0;i<n;i++)
add_element_to_tail_process_list(buffer, a[i].stt, a[i].tvao, a[i].txuly,
a[i].uutien);
//sap xep tang dan theo tvao
process_list tem_p1=buffer, tem_p2;
while( tem_p1!=NULL )
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao < (*tem_p1).tvao )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
//neu tvao bang nhau sap xep tang dan theo uu tien
tem_p1=buffer;
while( tem_p1!=NULL )
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao == (*tem_p1).tvao && (*tem_p2).ttong <
(*tem_p1).ttong )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
//printf("\nbuffer:\n");
//show_process_list(buffer);
//getch();
AN NGUYN LY H IU HANH
NHOM 10
Trang 41
time = (*buffer).tvao;
if(time>0) add_element_to_tail_table_gantt( gantt, -1, 0, time);
while( buffer !=NULL || stack !=NULL )
{
if( stack==NULL && buffer!=NULL )
{
//dua process tu buffer vao stack neu stack rong
add_element_to_head_process_list(stack, (*buffer).stt, (*buffer).tvao,
(*buffer).txuly, (*buffer).ttong );
buffer=(*buffer).next;
}
//tim process trong buffer co uu tien lon hon process tai stack
process_list tem=buffer;
while( tem!=NULL && (*stack).ttong<=(*tem).ttong )
{
tem=(*tem).next;
}
if( tem!=NULL) //ton tai process co uu tien lon hon
{
if( time + (*stack).txuly < (*tem).tvao )
{
add_element_to_tail_table_gantt(gantt, (*stack).stt, time, (*stack).txuly);
time += (*stack).txuly;
delete_element_in_head_process_list(stack);
//dua process o buffer vao stack
if( stack==NULL )
{
add_element_to_head_process_list(stack, (*buffer).stt, (*buffer).tvao,
(*buffer).txuly, (*buffer).ttong);
buffer=(*buffer).next;
}
}
else
{
add_element_to_tail_table_gantt(gantt, (*stack).stt, time, (*tem).tvaotime);
int txuly=time+(*stack).txuly-(*tem).tvao;
int stt=(*stack).stt;
int tvao=(*stack).tvao;
int ttong=(*stack).ttong;
AN NGUYN LY H IU HANH
NHOM 10
Trang 42
AN NGUYN LY H IU HANH
NHOM 10
Trang 43
tinh_tcho_tluu(gantt, a, n);
show_count_time(a, n);
getch();
}
Thut ton u tin c quyn
//Uu tien doc quyen
//cai nao da giai quyet la giai quyet den khi hoan thanh
#include"pre.cpp"
#include<conio.h>
#include<iostream>
#include<stdlib.h>
PROCESS a[20];
int n;
process_list buffer;
table_gantt gantt;
//process vao truoc se thuc hien cho xen khi hoan thanh
//khong nhuong CPU cho process vao sau du uu tien cao hon
//(doc quyen CPU)
void uu_tien_doc_quyen()
{
int i;
int time=0;
buffer = NULL;
gantt = NULL;
//dua process tu a[] vao buffer
for(i=0;i<n;i++)
add_element_to_tail_process_list(buffer, a[i].stt, a[i].tvao, a[i].txuly, a[i].uutien);
//sap xep tang dan theo tvao
process_list tem_p1=buffer, tem_p2;
while( tem_p1!=NULL )
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao < (*tem_p1).tvao )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
AN NGUYN LY H IU HANH
NHOM 10
Trang 44
}
tem_p1=(*tem_p1).next;
}
//neu tvao bang nhau sap xep tang dan theo uu tien
tem_p1=buffer;
while( tem_p1!=NULL )
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao == (*tem_p1).tvao && (*tem_p2).ttong <
(*tem_p1).ttong )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
//printf("\nbuffer:\n");
//show_process_list(buffer);
time = 0;
while( buffer !=NULL )
{
if( time < (*buffer).tvao)
{
add_element_to_tail_table_gantt(gantt, -1, time, (*buffer).tvao-time );
time = (*buffer).tvao;
}
else
{
process_list tem=buffer;
process_list max_priority=buffer;
//tim process trong buffer xu ly process co uu tien cao nhat
//co tvao <= time
//cho process tim duoc vao gantt va xoa khoi buffer
while( tem!=NULL && (*tem).tvao <= time )
{
if( (*max_priority).ttong > (*tem).ttong )
max_priority = tem;
AN NGUYN LY H IU HANH
NHOM 10
Trang 45
tem=(*tem).next;
}
//xu ly process tim duoc ( max_priority dang tro toi)
add_element_to_tail_table_gantt( gantt, (*max_priority).stt, time,
(*max_priority).txuly );
time+=(*max_priority).txuly;
//xoa process max_priority dang tro toi
max_priority=(*max_priority).next;
delete_element_before_node_process_list( buffer, max_priority);
}
}
}
main()
{
read_data_from_file("input4.txt",a,&n);
show_process_all(a,n);
//getch();
uu_tien_doc_quyen();
//printf("\ngantt:\n");
//show_table_gantt(gantt);
//getch();
printf("\nbang thoi gian cap phat cpu:\n");
show_CPU_time_table(gantt, a);
//getch();
tinh_tcho_tluu(gantt, a, n);
show_count_time(a, n);
getch();
}
Thut ton SJF khng c quyn
//SJF ko doc quyen
//cai nao co the xong truoc giai quyet truoc ngay khi co the
#include"pre.cpp"
#include<conio.h>
#include<iostream>
#include<stdlib.h>
PROCESS a[20];
int n;
AN NGUYN LY H IU HANH
NHOM 10
Trang 46
process_list list;
table_gantt gantt;
//dua cac process vao buffer
//sap xep buffer tang dan theo tvao, cung tvao thi tang dan theo do uu tien
//
//tim tiep uutien lon hon gan nhat xu ly truoc
//phan chua xu ly dua vao stack
void SJF_KhongDocQuyen()
{
int i;
int time;
process_list buffer=NULL, stack=NULL;
gantt=NULL;
//dua process tu a[] vao buffer
//SJF giong phuong phap lap lich uu tien
//do uu tien la thoi gian ket thuc tien trinh = tvao +txuly
for(i=0;i<n;i++)
add_element_to_tail_process_list(buffer, a[i].stt, a[i].tvao, a[i].txuly, a[i].tvao +
a[i].txuly);
//sap xep tang dan theo tvao
process_list tem_p1=buffer, tem_p2;
while( tem_p1!=NULL )
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao < (*tem_p1).tvao )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
//neu tvao bang nhau sap xep tang dan theo uu tien
tem_p1=buffer;
while( tem_p1!=NULL )
AN NGUYN LY H IU HANH
NHOM 10
Trang 47
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao == (*tem_p1).tvao && (*tem_p2).ttong <
(*tem_p1).ttong )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
//printf("\nbuffer:\n");
//show_process_list(buffer);
//neu thoi gian vao lon hon 0 > CPU ranh
time = (*buffer).tvao;
if(time>0) add_element_to_tail_table_gantt( gantt, -1, 0, time);
while( buffer !=NULL || stack !=NULL )
{
//dua process tu buffer vao stack
if( stack==NULL && buffer!=NULL )
{
add_element_to_head_process_list(stack, (*buffer).stt, (*buffer).tvao,
(*buffer).txuly, (*buffer).ttong );
buffer=(*buffer).next;
}
//tim process tiep theo co uu tien nho hon
//uu tien dc tinh bang thoi gian ket thuc neu duoc xu ly ngay
process_list tem=buffer;
while( tem!=NULL )
{
int stack_ttong = time + (*stack).txuly;
int tem_ttong = (*tem).tvao > time ? (*tem).ttong : time + (*tem).txuly;
if( tem_ttong < stack_ttong ) break;
tem=(*tem).next;
}
if( tem!=NULL) //tim duoc process uu tien hon process tai stack, dua process
nay vao stack
AN NGUYN LY H IU HANH
NHOM 10
Trang 48
{
if( time + (*stack).txuly < (*tem).tvao )
{
add_element_to_tail_table_gantt(gantt, (*stack).stt, time, (*stack).txuly);
time += (*stack).txuly; //time
delete_element_in_head_process_list(stack);
//dua process o buffer vao stack neu stack rong
if( stack==NULL )
{
add_element_to_head_process_list(stack, (*buffer).stt, (*buffer).tvao,
(*buffer).txuly, (*buffer).ttong);
buffer=(*buffer).next;
}
}
else
{
if( (*tem).tvao - time > 0 )
{
add_element_to_tail_table_gantt(gantt, (*stack).stt, time, (*tem).tvaotime);
int txuly=time+(*stack).txuly-(*tem).tvao;
int stt=(*stack).stt;
int tvao=(*stack).tvao;
//
int ttong=(*stack).ttong; // dieu chinh lai ttong (tuc la dieu chinh lai do
uu tien)
//
delete_element_in_head_process_list(stack);
if( txuly>0 )
add_element_to_head_process_list(stack, stt, tvao, txuly, ttong );
time = (*tem).tvao; //cap nhat tine
}
//dua process *tem dang tro toi vao *stack va xoa process nay
add_element_to_head_process_list( stack, (*tem).stt, (*tem).tvao,
(*tem).txuly, (*tem).ttong );
tem=(*tem).next;
delete_element_before_node_process_list(buffer,tem);
}
AN NGUYN LY H IU HANH
NHOM 10
Trang 49
}
else //xu ly process tai stack
{
if( time < (*stack).tvao )
{
add_element_to_tail_table_gantt(gantt, -1, time, (*stack).tvao-time);
time=(*stack).tvao;
}
add_element_to_tail_table_gantt(gantt, (*stack).stt, time, (*stack).txuly );
time += (*stack).txuly; ///
delete_element_in_head_process_list(stack);
}
/*printf("time=%d-------\nbuffer:\n",time);
show_process_list(buffer);
printf("gantt:\n");
show_table_gantt(gantt);
printf("stack:\n");
show_process_list(stack);
getch();*/
}
}
main()
{
read_data_from_file("input4.txt",a,&n);
show_process_all(a,n);
//getch();
SJF_KhongDocQuyen();
printf("\ngantt:\n");
show_table_gantt(gantt);
//getch();
printf("\nbang thoi gian cap phat cpu:\n");
show_CPU_time_table(gantt, a);
//getch();
tinh_tcho_tluu(gantt, a, n);
show_count_time(a, n);
getch();
}
AN NGUYN LY H IU HANH
NHOM 10
Trang 50
SJF c quyn
//SJF doc quyen
//
#include"pre.cpp"
#include<conio.h>
#include<iostream>
#include<stdlib.h>
PROCESS a[20];
int n;
process_list buffer;
table_gantt gantt;
//process vao truoc se thuc hien cho xen khi hoan thanh
//khong nhuong CPU cho process vao sau du thoi gian thuc hien ngan hon
//(doc quyen CPU)
//thuc hien giong uu tien doc quyen
//thay uu tien =tvao+txuly
void SJF_doc_quyen()
{
int i;
int time=0;
buffer = NULL;
gantt = NULL;
//dua process tu a[] vao buffer
for(i=0;i<n;i++)
add_element_to_tail_process_list(buffer, a[i].stt, a[i].tvao, a[i].txuly, a[i].tvao +
a[i].txuly);
//sap xep tang dan theo tvao
process_list tem_p1=buffer, tem_p2;
while( tem_p1!=NULL )
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao < (*tem_p1).tvao )
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
AN NGUYN LY H IU HANH
NHOM 10
Trang 51
//neu tvao bang nhau sap xep tang dan theo thoi gian tong
tem_p1=buffer;
while( tem_p1!=NULL )
{
tem_p2=tem_p1;
while(tem_p2!=NULL)
{
if( (*tem_p2).tvao == (*tem_p1).tvao && (*tem_p2).ttong <
(*tem_p1).ttong)
swap_node_process_list(tem_p1,tem_p2);
tem_p2=(*tem_p2).next;
}
tem_p1=(*tem_p1).next;
}
//printf("\nbuffer:\n");
//show_process_list(buffer);
time = 0;
while( buffer !=NULL )
{
if( time < (*buffer).tvao)
{
add_element_to_tail_table_gantt(gantt, -1, time, (*buffer).tvao-time );
time = (*buffer).tvao;
}
else
{
process_list tem=buffer;
process_list max_priority=buffer;
//tim process trong buffer xu ly process co uu tien cao nhat
//co tvao <= time
//cho process tim duoc vao gantt va xoa khoi buffer
while( tem!=NULL && (*tem).tvao <= time )
{
if( (*max_priority).ttong > (*tem).ttong )
max_priority = tem;
tem=(*tem).next;
}
//xu ly process tim duoc ( max_priority dang tro toi)
add_element_to_tail_table_gantt( gantt, (*max_priority).stt, time,
(*max_priority).txuly );
time+=(*max_priority).txuly;
AN NGUYN LY H IU HANH
NHOM 10
Trang 52
CHNG 4.
TRIN KHAI V NH GI KT QU
AN NGUYN LY H IU HANH
NHOM 10
Trang 53
4.2.1.2Thut ton RR
Hin th tin trnh vo:
AN NGUYN LY H IU HANH
NHOM 10
Trang 54
Hnh 13 : Qu trnh x l.
Kt qu:
Hnh 14 : Kt qu.
AN NGUYN LY H IU HANH
NHOM 10
Trang 55
AN NGUYN LY H IU HANH
NHOM 10
Trang 56
4.3 nh gi kt qu.
i vi 4 thut ton iu phi mt hng i ta nhn thy thut ton FIFO c thi
gian ch ln,cc tin trnh phi x l ln lut mc d thut ton n gin d phn
tch.
AN NGUYN LY H IU HANH
NHOM 10
Trang 57
Thut ton SJF c nh gi l thut ton c thi gian ch trung bnh trong CPU
ngn nht.