OS_L10
OS_L10
Semaphore
Lecture - 10
Semaphore and its Types
Cont..
Counting Semaphore
• There are the scenarios in which more than one processes need to execute in
critical section simultaneously. However, counting semaphore can be used when
we need to have more than one process in the critical section at the same time.
• In this mechanism, the entry and exit in the critical section are performed on the
basis of the value of counting semaphore.
• The value of counting semaphore at any point of time indicates the maximum
number of processes that can enter in the critical section at the same time.
• A process which wants to enter in the critical section first decrease the semaphore
value by 1 and then check whether it gets negative or not. If it gets negative then
the process is pushed in the list of blocked processes (i.e. blocked queue)
otherwise it gets enter in the critical section.
• When a process exits from the critical section, it increases the counting semaphore
by 1 and then checks whether it is negative or zero. If it is negative then that means
that at least one process is waiting in the blocked state hence, to ensure bounded
waiting, the first process among the list of blocked processes will wake up and gets
enter in the critical section.
• If the value of counting semaphore is negative then it states the number of
processes in the blocked state while if it is positive then it states the number of
slots available in the critical section.
Cont..
Cont..
• A Counting Semaphore was initialized to 12. then
10P (wait) and 4V (Signal) operations were computed
on this semaphore. What is the result?
• S = 12 (initial)
• 10 p (wait) :
• SS = S -10 = 12 - 10 = 2
• then 4 V :
• SS = S + 4 =2 + 4 = 6
Cont..
Options: A) -2 B) -1 C) 1 D) 2
W X Y Z
Wait(s) Wait(s) Wait(s) Wait(s)
R(x) R(x) R(x) R(x)
X=x+1 X=x+1 X=x-2 X=x-2
W(x) W(x) W(x) W(x)
Signal(s) Signal(s) Signal(s) Signal(s)
Cont..
• First, process W will be executed, after rearing the value of x, process W
will be preempted, then process Y and Z will be fully executed, followed
by Process W and X.
• Binary Semaphore:
• In counting semaphore, Mutual exclusion was not provided because we has
the set of processes which required to execute in the critical section
simultaneously.
• However, Binary Semaphore strictly provides mutual exclusion.
• Here, instead of having more than 1 slots available in the critical section,
we can only have at most 1 process in the critical section.
• The semaphore can have only two values, 0 or 1.
Cont..
Cont..
Answer: 10
Difference B/W Binary and Counting Semaphore
Yes, it guarantees mutual exclusion, since No, it doesn’t guarantees mutual exclusion,
Mutual
just one process or thread can enter the since more than one process or thread can
Exclusion
critical section at a time. enter the critical section at a time.
No, it doesn’t guarantees bounded wait, as Yes, it guarantees bounded wait, since it
only one process can enter the critical maintains a list of all the process or threads,
Bounded wait section, and there is no limit on how long using a queue, and each process or thread get
the process can exist in the critical section, a chance to enter the critical section once. So
making another process to starve. no question of starvation.