Professional Documents
Culture Documents
OS Synchronization Notes
OS Synchronization Notes
Why Synchronization?
Race Condition
• Situation when several processes access and manipulate the same data
• O/P depends on the order in which the accesses to the data took place.
Critical Section
Synchronization
• Mutual Exclusion
• Progress
• When no process is in the critical section, any process that requests enter
into the Critical Section must be permitted without any delay
Page 1 of 7
• There is an upper bound on the number of times a process enters the critical
section, while another is waiting
• process should not wait infinitely in order to gain access into the Critical
Section
Software Solution - 1
Software Solution – 2
Page 2 of 7
Software Solution – 3
Page 3 of 7
Peterson’s solution works efficiently for two processes
Bakery Algorithm
Page 4 of 7
Hardware Solution 2: xchg Instruction
High Level Constructs for solving Critical Section Problems (using Instructions like xchg)
• Spinlocks
• Mutex
• Semaphore
• Monitor
Page 5 of 7
• Two functions
• acquire(&locked)
• release(&locked)
• One process will acquire the lock and the other will wait in a loop repeatedly checking if the
lock is available
Busy Waiting
Page 6 of 7
Producer – Consumer Problem (Solution using Mutex)
Lost wakeup()
Shared Resource
Atomic functions to store/access the wakeup calls
o down/wait
o up/signal
2 Types
o Counting Semaphore
o Binary Semaphore
Page 7 of 7