You are on page 1of 11

NG DNG H THNG NHNG

SEMAPHORE
Nguyn o Xun Uyn 50702897
Mai c Ph
507

nh ngha
Semaphore
- Tnh sn sng ca mt ti nguyn
- S lng ti nguyn
- Kim sot vng tranh chp
(mutual exclusion)
- ng b ho khi s dng mt ti nguyn
- nh danh bi mt ID (semaphore ID)
- Mt s m ti nguyn
- Hng i cc task ang ch chim
gi ti nguyn
3/31/16

ng dng h thng nhng

nh ngha

3/31/16

ng dng h thng nhng

Phn loi (1)


Binary

semaphore

Counting

semaphore

Phn loi (2)


Mutual-exclusion

semaphore

(Mutex)

S dng
To

mt semaphore

ID semid = tk_cre_sem ( T_CSEM *pk_csem )

Yu

cu ti nguyn t semaphore

ER ercd = tk_wai_sem ( ID semid, INT cnt,


TMO tmout )

Tr

ti nguyn cho semaphore

ER ercd = tk_sig_sem ( ID semid, INT cnt )

Xa

semaphore

ER ercd = tk_del_sem ( ID semid )

3/31/16

ng dng h thng nhng

Ch : kt thc bnh thng k c


khi c 1 task ang i semaphore,
m li c tr v

3/31/16

ng dng h thng nhng

Khng s dng semapore

Producer product = 2
Producer product = 2
Consumer task running
Consumer task running
Consumer product = 1
Consumer product = 1
Consumer product = 0
Consumer product = 0
Producer product = 1
Producer product = 1
Consumer product = 0
Consumer product = 0
Consumer product = -1
Consumer product = -1
Producer product = 0
Producer product = 0
Consumer product = -1
Consumer product = -1
Consumer product = -2
Consumer product = -2

3/31/16

S dng semaphore

Producer product = 2
Producer product = 2
Consumer task running
Consumer task running
Consumer product = 1
Consumer product = 1
Consumer product = 0
Consumer product = 0
Producer product = 1
Producer product = 1
Consumer product = 0
Consumer product = 0
Producer product = 1
Producer product = 1
Consumer product = 0
Consumer product = 0

ng dng h thng nhng

Semaphore - Qun l tranh chp

3/31/16

ng dng h thng nhng

Deadlock
Deadlock

: cc dng thc thi b blocked


vnh vin do yu cu ti nguyn khng
bao gi c tho mn
T0 T1
wait(S); wait(Q);
wait(Q);
wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);

Deadlock

c th xy ra cho trng hp
nhiu hn 2 task

3/31/16

ng dng h thng nhng

10

Starvation
Starvation:

mt task b t chi
vnh vin ti nguyn yu cu
Starvation c th xy ra khi ta
thc thi cc task trong hng i
theo c ch LIFO

3/31/16

ng dng h thng nhng

11

You might also like