Professional Documents
Culture Documents
Bài 10 - Hệ điều hành
Bài 10 - Hệ điều hành
H IU HNH
Giao tip
p gia
g
cc tin trnh
ng (race condition)
Race condition: tnh hung m nhiu tin trnh cng
truyy cpp v thao tc d liu chia s mt cch ng
g
thi. D liu cui cng ph thuc vo tin trnh cui
cng.
ngn nga ng , cc tin
trnh ng
hnh phi
c ng b ha.
ng (race condition)
trong min
i gng
ca
n.
10
Alg.
g 1 & 2, Peterson, Dekker, Bakery,
y
TSL, Interrupt
Semaphore
Monitor
11
Giai thuat 1
Bien chia se
Process Pi
do {
while (turn != i) ;
()
critical section();
turn = j;
remainder section();
} while
i (1);
(1)
Thoa man
man mutual exclusion (1)
Thoa
Progress (2) & bounded-waiting (3) ?
12
Giai thuat 1
Process P0:
do
while(turn !=0 );
Critical_Section();
turn=1;;
Remainder_Section();
Process P1:
do
while(turn!=1);
Critical_Section();
turn=0;;
Remainder_Section();
while (1);
while (1);
Giai thuat 2
Bien chia se
Process
ocess Pi
do {
flag[i] = true;
while
i (flag[j]) ;
Critical_Section();
flag
ag [i]
[ ] = false;
a se;
Remainder_Section();
} while (1);
14
15
Neu
Ne
u i < j th Pi
cc vao
vao trc,
trc ngc
ngc la
laii Pj c
c vao
vao
trc.
16
while (1)
{
while (TestandSet(lock)) ;
Critical_Section;
lock = false;
Remainder_Section;
}
17
Semaphores
L mt cng c ng b ha c cung cp bi
HH khng i hi busy waiting.
Mt semaphore S l mt bin
nguyn m ngoi lnh
khi to ra, ch c th c truy xut thng qua hai
thao tc c quyn truy xut v nguyn t:
wait(S)
signal(S)
18
Semaphores
Truy cp vi 2 thao tc
( )
wait (S):
while S 0 do no-op;
S--;
signal (S):
S++;
ttrnh
h busy
b
waiting:
iti khi mt
t tin
ti ttrnh
h phi
hi i,
i n s
c t vo hng i block.
Khi mt
tin trnh pphi i
mt
semaphore
p
S,, n s b block
v t vo hng i ca semaphore tng ng.
Thao tc signal ly mt tin trnh t trong hng i v t n
vo
trong
t
danh
d h sch
h cc
tin
ti trnh
t h trng
t
thi sn
sng.
19
Semaphores
nh ngha cu trc:
typedef struct {
int value;
struct p
process *L;;
} semaphore;
Gi s c 2 thao tc c bn:
20
Semaphores
wait(S):
S.value--;
if (S.value < 0) {
add this process to S.L;
bl k
block;
}
signal(S):
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
21
Tnh huong: mot tap cac process b blocked, moi process gi tai
V du
du 2
Semaphore A va B, khi tao bang 1
P0
P1
wait (A);
wait(B);
wait (B);
wait(A);
-8.22-
He thong gom cac loai tai nguyen, k hieu R1, R2,, Rm , bao
gom:
C
CPU
U cycle,
cyc e, kho
ong g
gian
a bo nh
,, tthie
ett b I/O,
/O, file,
e, se
semaphore,
ap o e,
Cac
Ca
c ta
tac
c vu
vu ye
yeu
u cau
cau (request) va
va hoan
hoan tra
tra (release) e
eu
u la
la
system call. V du
request/release device
open/close file
allocate/free memory
wait/signal
-8.23-
-8.24-
P1
P2
-8.25-
-8.26-
P
Process:
Pi
Loai tai nguyen vi 4 thc the:
R
j
R
Pi
R
j
-8.27-
V du ve RAG
R3
R1
P1
P3
P2
R2
R4
-8.28-
V du ve RAG (tt)
R3
R1
P1
P3
P2
D dl k xay
Deadlock
ra!!
R2
R4
-8.29-
RAG va deadlock
P1
R1
P2
R2
P3
P4
-8.30-
-8.31-
-8.32-
Ngan deadlock
Ngan deadlock bang cach ngan mot trong 4 ieu
kien gay deadlock
1. Ngan mutual exclusion
oi vi nonsharable resource ((vd: printer):
p
) khong lam c
oi vi sharable resource (vd: read-only file): khong can
thiet
-8.33-
-8.34-
-8.36-
Cach 1: moi process ch co the yeu cau thc the cua mot loai tai
nguyen theo th t tang dan cua loai tai nguyen. V du
Chuoi yeu cau thc the hp le: tape drive disk drive
printer
Chuoi yeu cau thc the khong hp le: disk drive tape drive
Cach 2: Khi mot process yeu cau mot thc the cua loai tai nguyen
Rj th no phai tra lai cac tai nguyen Ri vi F(Ri) > F(Rj).
R
P1
P2
R
2
R
P4
P3
-8.37-
Deadlock avoidance
-8.38-
-8.39-
Chuoi an toan
-8.40-
g gi
g
ang
P0
10
P1
P2
-8.41-
ang gi
P0
10
P1
P2
-8.42-
Khi mot process yeu cau mot tai nguyen ang san
sang (available), he thong se kiem tra: neu viec cap
phat nay
phat
nay khong
khong dan
dan en
en tnh tra
trang
ng unsafe th se
se cap
cap
phat ngay.
-8.43-
deadlock
unsafe
safe
-8.44-
ieu kien
Mo
Moii process pha
phaii khai ba
bao
o so
so l
lng
ng th
thc
c the
the (instance) toi
toi
a cua moi loai tai nguyen ma no can
Khi process yeu cau tai nguyen th co the phai i mac du
tai nguyen c yeu cau ang co san
Khi process a co c ay u tai nguyen th phai hoan tra
trong mot khoa
khoang
ng thi
thi gian h
hu
u ha
han
n nao
nao o.
o
-8.45-
Available:
a ab e vector
ec o o da
dai m
Available[ j ] = k loai tai nguyen Rj co k instance san sang
Max: ma tran n m
Max[ i,
i j ] = k qua
qua trnh Pi yeu
yeu cau
cau toi
toi a k instance cua
cua loai
loai
tai nguyen Rj
Allocation: ma tran n m
Need: ma tran n m
Need[i, j] = k Pi can them k instance cua Rj
Nhan xet: Need [i, j] = Max[i, j] Allocation [i, j]
2 Tm i tho
2.
thoa
a
(a) Finish [i] = false
(b) Needi Work (hang th i cua Need)
N khong
Neu
kh ton
t ta
t i i nh
h vay, en
bc
b 4.
4
3. Work
:= Work + Allocationi
Finish[i] := true
quay ve bc
b 2.
2
4. Neu Finish[i] = true, i = 1,, n, th he thong ang trang thai
safe
Thi gian chay cua giai thuat la O(mn2)
-8.47-
-8.48-
Ap dung giai thuat kiem tra trang thai an toan len trang thai
tren
Neu trang thai la safe th tai nguyen c cap thc s cho
Pi .
Neu trang thai la unsafe th Pi phai i, va
phuc hoi trang thai:
Available := Available + Requesti
Allocationi := Allocationi Request
q
i
Needi
:= Needi + Requesti
-8.49-
V du
Co 5 process P0 ,, P4
Max
Available
Need
A B C
A B C
P0
P1
P2
P3
P4
-8.50-
Vd (tt)
Chuoi an toan <P1, P3, P4, P2, P0>
Allocation
Need
Available
ABC
ABC
A B C
P0
010
743
3 3 2
P1
200
122
P2
302
600
P3
211
011
7 4 3
P4
002
431
7 4 5
5 3 2
10 4 7
10 5 7
-8.51-
Need
Available
P0
P1
P2
P3
P4
-8.53-
R3
R4
P1
P2
P3
R2
P4
R5
P1
P2
P3
P4
Mot giai thuat kiem tra co ton tai chu trnh trong wait-for graph
y khong se c g
goi nh
ky
y. Giai thuat p
phat hien chu trnh
hay
co thi gian chay la O(n 2), vi n la so nh cua graph.
-8.54-
Cac cau truc d lieu dung trong giai thuat phat hien deadlock
Allocation: ma tran n m
so instance cua moi loai tai nguyen a cap phat cho moi process
Request: ma tran n m
yeu cau hien tai cua moi process.
Request [i,j] = k Pi ang yeu cau them k instance cua Rj
-8.55-
3. Work
:= Work + Allocationi
Finish[i] := true
quay ve bc 2.
O(mn2)
V du
Request
Available
A B C
P0
P1
P2
P3
P4
Chay giai thuat, tm c chuoi <P0, P2, P3, P1, P4> vi Finish[i] =
true, i = 1,, n, vay he thong khong b deadlocked.
-8.57-
V du (tt)
ABC
P0
P1
P2
P3
P4
000
202
001
100
002
-8.58-
Deadlock Recovery
-8.59-
Da tren y
yeu to nao e cham dt p
process?
Chon nan nhan e toi thieu chi ph (co the da tren so tai
nguyen
hu,
h thi
thi gian
i CPU
a tieu
ti ton,...)
t
)
s
Rollback: rollback process b lay lai tai nguyen tr ve trang
thai safe
thai
safe, ba
batt au
au process t
t tra
trang
ng tha
thaii o
o. He tho
thong
ng ca
can
n lu
gi mot so thong tin ve trang thai cac process ang thc thi.
Starvation: phai
phai ba
bao
o am
am khong
khong co
co process se
se luon
luon luon
luon b
lay lai tai nguyen moi khi deadlock xay ra.
-8.61-