0% found this document useful (0 votes)
57 views59 pages

Ch07 Synchronization

The document discusses process synchronization and solutions to critical section problems. It introduces key concepts like critical sections, race conditions and mutual exclusion. It presents Peterson's algorithm as a software solution that satisfies the requirements of mutual exclusion, progress and bounded waiting for the critical section problem between two processes. The algorithm uses shared flags and a turn variable to coordinate access to the critical section.

Uploaded by

Bình Nguyên
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views59 pages

Ch07 Synchronization

The document discusses process synchronization and solutions to critical section problems. It introduces key concepts like critical sections, race conditions and mutual exclusion. It presents Peterson's algorithm as a software solution that satisfies the requirements of mutual exclusion, progress and bounded waiting for the critical section problem between two processes. The algorithm uses shared flags and a turn variable to coordinate access to the critical section.

Uploaded by

Bình Nguyên
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

ong Bo va Giai Quyet Tranh Chap (Process Synchronization)

Chng 7

-1-

Noi dung
Khai niem c ban Critical section Cac giai phap phan mem
Giai thuat Peterson, va giai thuat bakery

ong bo bang hardware Semaphore Cac bai toan ong bo Critical region Monitor
2

Khai niem c ban

Khao sat cac process/thread thc thi ong thi va chia se d lieu (qua shared memory, file). Neu khong co s kiem soat khi truy cap cac d lieu chia se th co the a en ra trng hp khong nhat quan d lieu (data inconsistency). e duy tr s nhat quan d lieu, he thong can co c che bao am s thc thi co trat t cua cac process ong thi. V du: bounded buffer (ch. 4), them bien em count
#define BUFFER_SIZE 10 /* 10 buffers */ typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0, out = 0, count = 0;
3

Bounded buffer (tt)


Qua trnh Producer
item nextProduced; while(1) { while (count == BUFFER_SIZE); /* do nothing */ buffer[in] = nextProduced; count++; in = (in + 1) % BUFFER_SIZE; }

Qua trnh Consumer

bien count c chia se gia producer va consumer

item nextConsumed; while(1) { while (count == 0); /* do nothing */ nextConsumed = buffer[out] ; count--; out = (out + 1) % BUFFER_SIZE; }
4

Bounded buffer (tt)


Cac lenh tang, giam bien count tng ng trong ngon ng may la:

(Producer)

register1 = count register1 = register1 + 1 count = register1

count++:

(Consumer)

register2 = count register2 = register2 - 1 count = register2

count--:

Trong o, cac registeri la cac thanh ghi cua CPU.


5

Bounded buffer (tt)

Ma may cua cac lenh tang va giam bien count co the b thc thi xen ke Gia s count ang bang 5. Chuoi thc thi sau co the xay ra:

0: 1: 2: 3: 4: 5:

producer producer consumer consumer producer consumer

register1 := count register1 := register1 + 1 register2 := count register2 := register2 - 1 count := register1 count := register2

{register1 = 5} {register1 = 6} {register2 = 5} {register2 = 4} {count = 6} {count = 4}

Ca hai process thao tac ong thi len bien chung count. Tr cua bien chung nay khong nhat quan di cac thao tac cua hai process. Giai phap: cac lenh count++, count-- phai la n nguyen (atomic), ngha la thc hien nh mot lenh n, khong b ngat na chng.
6

Bounded buffer (tt)


Race condition: nhieu process truy xuat va thao tac ong thi len d lieu chia se (nh bien count)
Ket qua cuoi cung cua viec truy xuat ong thi nay phu thuoc th t thc thi cua cac lenh thao tac d lieu.

e d lieu chia se c nhat quan, can bao am sao cho tai moi thi iem ch co mot process c thao tac len d lieu chia se. Do o, can co c che ong bo hoat ong cua cac process nay.

Khai niem Critical Section


Gia s co n process cung truy xuat ong thi d lieu chia se Khong phai tat ca cac oan code eu can c giai quyet van e race condition ma ch nhng oan code co cha cac thao tac len d lieu chia se. oan code nay c goi la vung tranh chap (critical section, CS). Van e: phai bao am s loai tr tng ho (mutual exclusion, mutex), tc la khi mot process ang thc thi trong vung tranh chap, khong co process nao khac ong thi thc thi cac lenh trong vung tranh chap.
8

Cau truc tong quat


Gia s moi process thc thi bnh Mot so gia nh thng (i.e., nonzero speed) va Co the co nhieu CPU nhng khong cho phep co nhieu tac khong co s tng quan gia toc vu truy cap mot v tr trong bo o thc thi cua cac process nh cung luc (simultaneous) Cau truc tong quat cua mot Khong rang buoc ve th t process: thc thi cua cac process Cac process co the chia se do { mot so bien chung nham muc entry section ch ong bo hoat ong cua chung critical section Giai phap cua chung ta can exit section phai ac ta c cac phan remainder section entry section va exit section

} while(1);
9

Li giai cua bai toan tranh chap

Li giai phai thoa ba tnh chat


(1) Mutual exclusion: Khi mot process P ang thc thi trong vung tranh chap (CS) cua no th khong co process Q nao khac ang thc thi trong CS cua Q. (2) Progress: neu khong co process nao ang thc thi trong vung tranh chap va ang co mot so process ch i vao vung tranh chap th:
Ch nhng process khong ang thc thi trong remainder section mi c la ng c vien cho viec c chon vao vung tranh chap. Qua trnh chon la nay khong c tr hoan vo han (postponed indefinitely).

(3) Bounded waiting: Moi process ch phai ch e c vao vung tranh chap trong mot khoang thi gian co han nh nao o. Khong xay ra tnh trang oi tai nguyen (starvation).
10

Phan loai giai phap


Giai phap phan mem (software solutions)
user/programmer t thc hien (thong thng se co s ho tr cua cac th vien lap trnh) OS cung cap mot so cong cu (cac ham va cau truc d lieu) ho tr cho programmer qua system calls.

Giai phap phan cng (hardware solutions)


Da tren mot so lenh may ac biet Disable interrupt TestAndSet
11

Giai phap phan mem


Trng hp 2 process ong thi: P0 va P1
Giai thuat 1 va 2 Giai thuat 3 (Petersons algorithm)

Giai thuat cho n process


Bakery algorithm

12

Giai thuat 1
Bien chia se
int turn; /* khi au turn = 0 */ neu turn = i th Pi c phep vao critical section, vi i = 0 hay 1

Process Pi do { while (turn != i); critical section turn = j; remainder section } while (1);

Thoa man mutual exclusion (1) Nhng khong thoa man yeu cau ve progress (2) va bounded waiting (3) v tnh chat strict alternation cua giai thuat
13

Giai thuat 1 (tt)


Process P0: do while (turn != 0); critical section turn := 1; remainder section while (1); Process P1: do while (turn != 1); critical section turn := 0; remainder section while (1);

V du: P0 co RS (remainder section) rat ln con P1 co RS nho. Neu turn = 0, P0 c vao CS va sau o thc thi turn = 1 va vao vung RS. Luc o P1 vao CS va sau o thc thi turn = 0, ke o P1 vao va xong RS, va i vao CS mot lan na, nhng v turn = 0 nen P1 phai ch P0.
14

Giai thuat 2
Bien chia se Process Pi do {
boolean flag[ 2 ]; /* khi au flag[ 0 ] = flag[ 1 ] = false */ Neu flag[ i ] = true th Pi san sang vao critical section.

flag[ i ] = true; /* Pi san sang vao CS */ */ while ( flag[ j ] ); /* Pi nhng Pj critical section flag[ i ] = false; remainder section } while (1); Bao am c mutual exclusion. Chng minh? Khong thoa man progress. V sao? Trng hp sau co the xay ra:
P0 gan flag[ 0 ] = true P1 gan flag[ 1 ] = true P0 va P1 loop mai mai trong vong lap while
15

Giai thuat 3 (Peterson)


Bien chia se: ket hp ca giai thuat 1 va 2 Process Pi , vi i = 0 hay 1 do { flag[ i ] = true; /* Process i san sang */ turn = j; /* Nhng process j */ while (flag[ j ] and turn == j); critical section flag[ i ] = false; remainder section } while (1); Thoa man c ca 3 yeu cau (chng minh?) giai quyet bai toan critical section cho 2 process.
16

Giai thuat Peterson-2 process


Process P0 do {
/* 0 wants in */

Process P1 do {
/* 1 wants in */

flag[0] = true;
/* 0 gives a chance to 1 */

flag[1] = true;
/* 1 gives a chance to 0 */

turn = 1; while (flag[1] && turn == 1); critical section


/* 0 no longer wants in */

turn = 0; while (flag[0] && turn == 0); critical section


/* 1 no longer wants in */

flag[0] = false; remainder section } while(1);

flag[1] = false; remainder section } while(1);

17

Giai thuat 3: Tnh ung an

Giai thuat 3 thoa mutual exclusion, progress, va bounded waiting Mutual exclusion c bao am bi v
P0 va P1 eu trong CS neu va ch neu flag[0] = flag[1] = true va turn = i cho moi Pi (khong the xay ra)

Chng minh thoa yeu cau ve progress va bounded waiting


Pi khong the vao CS neu va ch neu b ket tai vong lap while() vi ieu kien flag[ j ] = true va turn = j . Neu Pj khong muon vao CS th flag[ j ] = false va do o Pi co the vao CS.
18

Giai thuat 3: Tnh ung an (tt)


Neu Pj a bat flag[ j ] = true va ang ch tai while() th co ch hai trng hp la turn = i hoac turn = j Neu turn = i th Pi vao CS. Neu turn = j th Pj vao CS nhng se bat flag[ j ] = false khi thoat ra cho phep Pi vao CS Nhng neu Pj co u thi gian bat flag[ j ] = true th Pj cung phai gan turn = i V Pi khong thay oi tr cua bien turn khi ang ket trong vong lap while(), Pi se ch e vao CS nhieu nhat la sau mot lan Pj vao CS (bounded waiting)
19

Giai thuat bakery: n process


Trc khi vao CS, process Pi nhan mot con so. Process nao gi con so nho nhat th c vao CS Trng hp Pi va Pj cung nhan c mot ch so:
Neu i < j th Pi c vao trc. (oi xng)

Khi ra khoi CS, Pi at lai so cua mnh bang 0 C che cap so cho cac process thng tao cac so theo c che tang dan, v du 1, 2, 3, 3, 3, 3, 4, 5, K hieu
(a,b) < (c,d) neu a < c hoac if a = c va b < d max(a0,,ak) la con so b sao cho b ai vi moi i = 0,, k
20

Giai thuat bakery: n process (tt)


/* shared variable */ boolean choosing[ n ]; int num[ n ]; do { /* initially, choosing[ i ] = false */ /* initially, num[ i ] = 0 */

choosing[ i ] = true; num[ i ] = max(num[0], num[1],, num[n 1]) + 1; choosing[ i ] = false; for (j = 0; j < n; j++) { while (choosing[ j ]); while ((num[ j ] != 0) && (num[ j ], j) < (num[ i ], i)); } critical section num[ i ] = 0; remainder section } while (1);

21

T software en hardware
Khuyet iem cua cac giai phap software
Cac process khi yeu cau c vao vung tranh chap eu phai lien tuc kiem tra ieu kien (busy waiting), ton nhieu thi gian x ly cua CPU Neu thi gian x ly trong vung tranh chap ln, mot giai phap hieu qua nen co c che block cac process can i.

Cac giai phap phan cng (hardware)


Cam ngat (disable interrupts) Dung cac lenh ac biet

22

Cam ngat
Trong he thong uniprocessor: mutual exclusion c bao am.
Nhng neu system clock c cap nhat do interrupt th

Process Pi: do { disable_interrupts(); critical section enable_interrupts(); remainder section } while (1);

Tren he thong multiprocessor: mutual exclusion khong c am bao


Ch cam ngat tai CPU thc thi lenh disable_interrupts Cac CPU khac van co the truy cap bo nh chia se

23

Dung cac lenh ac biet


Y tng c s
Viec truy xuat vao vao mot a ch cua bo nh von a co tnh loai tr tng ho (ch co mot thao tac truy xuat tai mot thi iem)

M rong
thiet ke mot lenh may co the thc hien hai thao tac chap (atomic, indivisible) tren cung mot o nh (vd: read va write) Viec thc thi cac lenh may nh tren luon bao am mutual exclusive (ngay ca vi he thong multiprocessor)

Cac lenh may ac biet co the am bao mutual exclusion tuy nhien cung can ket hp vi mot so c che khac e thoa man hai yeu cau con lai la progress va bounded waiting cung nh tranh tnh trang starvation va deadlock.
24

Lenh TestAndSet
oc va ghi mot bien trong mot thao tac atomic (khong chia cat c). boolean TestAndSet(boolean &target) { boolean rv = target; target = true; return rv; } Shared data: boolean lock = false; Process Pi : do { while (TestAndSet(lock)); critical section lock = false; remainder section } while (1);
25

Lenh TestAndSet (tt)


Mutual exclusion c bao am: neu Pi vao CS, cac process Pj khac eu ang busy waiting Khi Pi ra khoi CS, qua trnh chon la process Pj vao CS ke tiep la tuy y khong bao am ieu kien bounded waiting. Do o co the xay ra starvation (b bo oi) Cac processor (v du Pentium) thong thng cung cap mot lenh n la Swap(a, b) co tac dung hoan chuyen noi dung cua a va b.
Swap(a, b) cung co u nhc iem nh TestAndSet

26

Swap va mutual exclusion


Bien chia se lock c khi tao gia tr false Moi process Pi co bien cuc bo key Process Pi nao thay gia tr lock = false th c vao CS.
Process Pi se loai tr cac process Pj khac khi thiet lap lock = true

Bien chia se (khi tao la false) bool lock; bool waiting [n]; Process Pi do { key = true; while (key == true) Swap(lock, key); critical section lock = false; remainder section } while (1)
Khong thoa man bounded waiting
27

void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; }

Giai thuat dung TestAndSet thoa man 3 yeu cau (1) Cau truc d lieu dung chung (khi tao la false)
bool waiting[ n ]; bool lock;

Mutual exclusion: Pi ch co the vao CS neu va ch neu hoac waiting[ i ] = false, hoac key = false
key = false ch khi TestAndSet (hay Swap) c thc thi
Process au tien thc thi TestAndSet mi co key == false;

cac process khac eu phai i

waiting[ i ] = false ch khi process khac ri khoi CS


Ch co mot waiting[ i ] co gia tr false

Progress: chng minh tng t nh mutual exclusion Bounded waiting: waiting in the cyclic order
28

Giai thuat dung TestAndSet thoa man 3 yeu cau (2)


do { waiting[ = true; waiting[ i i]]= true; key = true; key = true; while (waiting[ && key) while (waiting[ i i]]&& key) key = TestAndSet(lock); key = TestAndSet(lock); waiting[ = false; waiting[ i i]]= false; critical section = (i + 1) % n; j j= (i + 1) % n; while (j != i) && !waiting[ while (((j != i) && !waiting[ j j]])) = (j + 1) % n; j j= (j + 1) % n; (j == i) ifif(j == i) lock = false; lock = false; else else waiting[ = false; waiting[ j j]]= false; remainder section } while (1)
29

Semaphore

La cong cu ong bo cung cap bi OS ma khong oi hoi busy waiting Semaphore S la mot bien so nguyen, ngoai thao tac khi ong bien th ch co the c truy xuat qua hai tac vu co tnh n nguyen (atomic) va loai tr (mutual exclusive)
wait(S) hay con goi la P(S): giam gia tr semaphore. Ke o neu gia tr nay am th process thc hien lenh wait() b blocked. signal(S) hay con goi la V(S): tang gia tr semaphore. Ke o neu gia tr nay khong dng, mot process ang blocked bi mot lenh wait() se c hoi phuc e thc thi.

Tranh busy waiting: khi phai i th process se c at vao mot blocked queue, trong o cha cac process ang ch i cung mot s kien.

30

Hien thc semaphore


nh ngha semaphore la mot record
typedef struct { int value; struct process *L; } semaphore;

/* process queue */

Gia s he ieu hanh cung cap hai tac vu (system call):


block(): tam treo process nao thc thi lenh nay wakeup(P): hoi phuc qua trnh thc thi cua process P ang blocked
31

Hien thc semaphore (tt)


Cac tac vu semaphore c hien thc nh sau void wait(semaphore S) { S.value--; if (S.value < 0) { add this process to S.L; block(); } } void signal(semaphore S) { S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(P); } }
32

Hien thc semaphore (tt)


Khi mot process phai ch tren semaphore S, no se b blocked va c at trong hang i semaphore
Hang i nay la danh sach lien ket cac PCB

Tac vu signal() thng s dung c che FIFO khi chon mot process t hang i va a vao hang i ready block() va wakeup() thay oi trang thai cua process
block: chuyen t running sang waiting wakeup: chuyen t waiting sang ready
33

Hien thc mutex vi semaphore


Dung cho n process Khi tao S.value = 1 Ch duy nhat mot process c vao CS (mutual exclusion) e cho phep k process vao CS, khi tao S.value = k Shared data: semaphore mutex; /* initially mutex.value = 1 */ Process Pi: do { wait(mutex); critical section signal(mutex); remainder section } while (1);
34

ong bo process bang semaphore


Hai process: P1 va P2 Yeu cau: lenh S1 trong P1 can c thc thi trc lenh S2 trong P2 nh ngha semaphore synch e ong bo Khi ong semaphore: synch.value = 0 e ong bo hoat ong theo yeu cau, P1 phai nh ngha nh sau:
S1; signal(synch);

Va P2 nh ngha nh sau:
wait(synch); S2;

35

Nhan xet
Khi S.value 0: so process co the thc thi wait(S) ma khong b blocked = S.value Khi S.value < 0: so process ang i tren S la S.value Atomic va mutual exclusion: khong c xay ra trng hp 2 process cung ang trong than lenh wait(S) va signal(S) (cung semaphore S) tai mot thi iem (ngay ca vi he thong multiprocessor) do o, oan ma nh ngha cac lenh wait(S) va signal(S) cung chnh la vung tranh chap

36

Nhan xet (tt)


Vung tranh chap cua cac tac vu wait(S) va signal(S) thong thng rat nho: khoang 10 lenh. Giai phap cho vung tranh chap wait(S) va signal(S)
Uniprocessor: co the dung c che cam ngat (disable interrupt). Nhng phng phap nay khong lam viec tren he thong multiprocessor. Multiprocessor: co the dung cac giai phap software (nh giai thuat Dekker, Peterson) hoac giai phap hardware (TestAndSet, Swap). V CS rat nho nen chi ph cho busy waiting se rat thap.

37

Deadlock va starvation
Deadlock: hai hay nhieu process ang ch i vo han nh mot s kien khong bao gi xay ra (vd: s kien do mot trong cac process ang i tao ra). Goi S va Q la hai bien semaphore c khi tao = 1 P0 wait(S); wait(Q); M signal(S); signal(Q); P1 wait(Q); wait(S); M signal(Q); signal(S);

P0 thc thi wait(S), roi P1 thc thi wait(Q), roi P0 thc thi wait(Q) b blocked, P1 thc thi wait(S) b blocked. Starvation (indefinite blocking) co the xay ra khi process vao hang i va c lay ra theo c che LIFO.
38

Cac loai semaphore


Counting semaphore: mot so nguyen co gia tr khong han che. Binary semaphore: co tr la 0 hay 1. Binary semaphore rat de hien thc. Co the hien thc counting semaphore bang binary semaphore.

39

Cac bai toan ong bo


Bai toan bounded buffer
D lieu chia se: semaphore full, empty, mutex; Khi tao: full = 0; /* so buffers ay */ empty = n; /* so buffers trong */ mutex = 1;
n buffers out
40

Bounded buffer
producer
do { nextp = new_item(); wait(empty); wait(mutex); insert_to_buffer(nextp); signal(mutex); signal(full); } while (1);

consumer
do { wait(full) wait(mutex); nextc = get_buffer_item(out); signal(mutex); signal(empty); consume_item(nextc); } while (1);

41

Bai toan Dining Philosophers (1)


5 triet gia ngoi an va suy ngh Moi ngi can 2 chiec ua (chopstick) e an Tren ban ch co 5 ua Bai toan nay minh hoa s kho khan trong viec phan phoi tai nguyen gia cac process sao cho khong xay ra deadlock va starvation
4 3 3 4 0 1 0 2 1 2

D lieu chia se: semaphore chopstick[5]; Khi au cac bien eu la 1


42

Bai toan Dining Philosophers


Triet gia th i: do { wait(chopstick [ i ]) wait(chopstick [ (i + 1) % 5 ]) eat signal(chopstick [ i ]); signal(chopstick [ (i + 1) % 5 ]); think } while (1);

(2)

43

Bai toan Dining Philosophers


Giai phap tren co the gay ra deadlock

(3)

Khi tat ca triet gia oi bung cung luc va ong thi cam chiec ua ben tay trai deadlock

Mot so giai phap khac giai quyet c deadlock


Cho phep nhieu nhat 4 triet gia ngoi vao cung mot luc Cho phep triet gia cam cac ua ch khi ca hai chiec ua eu san sang (ngha la tac vu cam cac ua phai xay ra trong CS) Triet gia ngoi v tr le cam ua ben trai trc, sau o mi en ua ben phai, trong khi o triet gia v tr chan cam ua ben phai trc, sau o mi en ua ben trai

Starvation?
44

Bai toan Readers-Writers (1)


D lieu chia se semaphore mutex = 1; semaphore wrt = 1; int readcount = 0; Writer process wait(wrt); ... writing is performed ... signal(wrt);
Reader Process wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); ... reading is performed ... wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex);
45

Bai toan Readers-Writers (2)


mutex: bao ve bien readcount wrt
Bao am mutual exclusion oi vi cac writer c s dung bi reader au tien hoac cuoi cung vao hay ra khoi vung tranh chap.

Neu mot writer ang trong CS va co n reader ang i th mot reader c xep trong hang i cua wrt va n 1 reader kia trong hang i cua mutex Khi writer thc thi signal(wrt), he thong co the phuc hoi thc thi cua mot trong cac reader ang i hoac writer ang i.
46

Cac van e vi semaphore


Semaphore cung cap mot cong cu manh me e bao am mutual exclusion va phoi hp ong bo cac process Tuy nhien, neu cac tac vu wait(S) va signal(S) nam rai rac rat nhieu processes kho nam bat c hieu ng cua cac tac vu nay. Neu khong s dung ung co the xay ra tnh trang deadlock hoac starvation. Mot process b die co the keo theo cac process khac cung s dung bien semaphore.
signal(mutex) signal(mutex) critical section critical section wait(mutex) wait(mutex) wait(mutex) wait(mutex) critical section critical section wait(mutex) wait(mutex) signal(mutex) signal(mutex) critical section critical section signal(mutex) signal(mutex)
47

Critical Region (CR)


La mot cau truc ngon ng cap cao (high-level language construct, c dch sang ma may bi mot compiler), thuan tien hn cho ngi lap trnh. Mot bien chia se v kieu d lieu T, khai bao nh sau v: shared T; Bien chia se v ch co the c truy xuat qua phat bieu sau region v when B do S; /* B la mot bieu thc Boolean */

Y ngha: trong khi S c thc thi, khong co qua trnh khac co the truy xuat bien v. Khi mot process muon thc thi cac lenh trong region (tc la S), bieu thc Boolean B c kiem tra. Neu B = true, lenh S c thc thi. Neu B = false, process b tr hoan cho en khi B = true.
48

CR va bai toan bounded buffer


D lieu chia se: struct buffer { int pool[n]; int count, in, out; } Producer region buffer when (count < n) region buffer when (count < n) {{ pool[in] = nextp; pool[in] = nextp; in = (in + 1) % n; in = (in + 1) % n; count++; count++; }} Consumer region buffer when (count > 0){ region buffer when (count > 0){ nextc = pool[out]; nextc = pool[out]; out = (out + 1) % n; out = (out + 1) % n; count--; count--; }}
49

Monitor (1)
Cung la mot cau truc ngon ng cap cao tng t CR, co chc nang nh semaphore nhng de ieu khien hn Xuat hien trong nhieu ngon ng lap trnh ong thi nh
Concurrent Pascal, Modula-3, Java,

Co the hien thc bang semaphore

50

Monitor (2)
La mot module phan mem, bao gom
Mot hoac nhieu thu tuc (procedure) Mot oan code khi tao (initialization code) Cac bien d lieu cuc bo (local data variable)

ac tnh cua monitor


Local variable ch co the truy xuat bi cac thu tuc cua monitor Process vao monitor bang cach goi mot trong cac thu tuc o Ch co mot process co the vao monitor tai mot thi iem mutual exclusion c bao am

shared data

operations initialization code

entry queue

Mo hnh cua mot monitor n gian


51

Cau truc cua monitor


monitor monitor-name { shared variable declarations procedure body P1 () { ... } procedure body P2 () { ... } procedure body Pn () { ... } { initialization code } }
52

Condition variable
Nham cho phep mot process i trong monitor, phai khai bao bien ieu kien (condition variable) condition a, b; Cac bien ieu kien eu cuc bo va ch c truy cap ben trong monitor. Ch co the thao tac len bien ieu kien bang hai thu tuc:
a.wait: process goi tac vu nay se b block tren bien ieu kien a process nay ch co the tiep tuc thc thi khi co process khac thc hien tac vu a.signal a.signal: phuc hoi qua trnh thc thi cua process b block tren bien ieu kien a. Neu co nhieu process: ch chon mot Neu khong co process: khong co tac dung

53

Monitor co condition variable


shared data
a b

entry queue
Cac process co the i entry queue hoac i cac condition queue (a, b,) Khi thc hien lenh a.wait, process se c chuyen vao condition queue a Lenh a.signal chuyen mot process t condition queue a vao monitor Khi o, e bao am mutual exclusion, process goi a.signal se b blocked va c a vao urgent queue
54

... operations initialization code

Monitor co condition variable (tt)


entry queue

monitor waiting area

entrance

MONITOR
local data condition c1 c1.wait condition variables procedure 1

...

condition cn cn.wait urgent queue cx.signal

procedure k initialization code exit


55

...

Monitor va dining philosophers


3 2

4 0

monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5];


56

Dining philosophers (tt)


void pickup(int i) { state[ i ] = hungry; test[ i ]; if (state[ i ] != eating) self[ i ].wait(); } void putdown(int i) { state[ i ] = thinking; // test left and right neighbors test((i + 4) % 5); // left neighbor test((i + 1) % 5); // right }
57

Dining philosophers (tt)


void test(int i) { if ( (state[(i + 4) % 5] != eating) && (state[ i ] == hungry) && (state[(i + 1) % 5] != eating) ) { state[ i ] = eating; self[ i ].signal(); } void init() { for (int i = 0; i < 5; i++) state[ i ] = thinking; } }
58

Dining philosophers (tt)


Trc khi an, moi triet gia phai goi ham pickup(), an xong roi th phai goi ham putdown()
dp.pickup(i); an dp.putdown(i);

Giai thuat khong deadlock nhng co the gay starvation.

59

You might also like