Operating Systems
UNIT-III
Semaphores
Presented by
Tmt.P.Tharani
AP/CSE
GCE,Salem
1
Semaphores
 Synchronization tool –solves critical section problem
 It is an integer variable shared by multiple processes and can
be accesses through two standard atomic operation wait and
signal.
2
• P1:
• P2:
3
 The main disadvantage of mutual exclusion solutions requires
busy waiting.
 Busy waiting wastes CPU cycle and this type of semaphore is
called as Spinlock.
 Each Process has an integer value and a list of processes.
 When a process must wait on a semaphore it is added to the
list of processes.
 A signal operation removes one process from the list of
waiting processes and awakens that process.
 The wakeup operation resumes the execution of a blocked
process.
4
• Mutual Exclusion with no busy waiting
Consider three values: value =1 (CS empty, Queue empty)
Value=0 (CS occupied, queue empty)
Value=-3 (CS occupied,three process are waiting in the queue)
5
• Deadlock and Starvation
 Consider a system consisting of two processes P0 & P1 each
accessing two semaphore S and Q set to the value 1.
6
• Binary Semaphore
 A binary semaphore is a semaphore with an integer value that
can range only between 0 and 1. Counting Semaphore has a
range over unrestricted domain.
 Let S be a counting semaphore .To implement it in terms of
binary semaphore.
type binary semaphore=record
value(0,1)
queue:list of process
end
var S: binary_semaphore
 Consider value=1 CS empty and value=0 CS occupied.
7
wait B(s)
if s.value=1
then s.value=0
else begin
Place this process to s.queue
Block this process
End
Signal B(s)
if s.queue is empty
then s.value=1
else begin
Remove a process from s.queue
Place process in ready queue
end
8
Thank You
9

Unit-II Semaphore.ppt

  • 1.
  • 2.
    Semaphores  Synchronization tool–solves critical section problem  It is an integer variable shared by multiple processes and can be accesses through two standard atomic operation wait and signal. 2
  • 3.
  • 4.
     The maindisadvantage of mutual exclusion solutions requires busy waiting.  Busy waiting wastes CPU cycle and this type of semaphore is called as Spinlock.  Each Process has an integer value and a list of processes.  When a process must wait on a semaphore it is added to the list of processes.  A signal operation removes one process from the list of waiting processes and awakens that process.  The wakeup operation resumes the execution of a blocked process. 4
  • 5.
    • Mutual Exclusionwith no busy waiting Consider three values: value =1 (CS empty, Queue empty) Value=0 (CS occupied, queue empty) Value=-3 (CS occupied,three process are waiting in the queue) 5
  • 6.
    • Deadlock andStarvation  Consider a system consisting of two processes P0 & P1 each accessing two semaphore S and Q set to the value 1. 6
  • 7.
    • Binary Semaphore A binary semaphore is a semaphore with an integer value that can range only between 0 and 1. Counting Semaphore has a range over unrestricted domain.  Let S be a counting semaphore .To implement it in terms of binary semaphore. type binary semaphore=record value(0,1) queue:list of process end var S: binary_semaphore  Consider value=1 CS empty and value=0 CS occupied. 7
  • 8.
    wait B(s) if s.value=1 thens.value=0 else begin Place this process to s.queue Block this process End Signal B(s) if s.queue is empty then s.value=1 else begin Remove a process from s.queue Place process in ready queue end 8
  • 9.