PROCESS SYNCHRONIZATION
PREVIOUS SESSION
• IO BOUND VS CPU BOUND PROCESS
• SCHEDULING QUEUES AND SCHEDULERS
• PREEMPTIVE VS NONPREEMPTIVE SCHEDULING
• DISPATCHER AND CONTEXT SWITCH
• SCHEDULING ALGORITHMS
INTERVIEW QUESTIONS
• What is process synchronization?
• What is race condition and critical section?
• What is critical section problem?
• What are software and hardware solutions for CSP?
• Difference between mutex, spinlock, binary semaphore and counting
semaphore
• Solution to producer-consumer problem using mutex and semaphore
• Solution to reader-writer problem using mutex and semaphore
• What is bounded buffer problem?
PROCESS SYNCHRONIZATION
• Sharing data among multiple processes doesn’t lead to corruption of
data
• Cooperating process v independent process
• Multiple threads accessing data concurrently
RACE CONDITION
Initially i = 7, final value = 9 and two threads
incrementing i
Initial value = 7, final value = 8
RACE CONDITION
• When several threads tries to access and manipulate shared data
concurrently and the outcome of execution depends on the order in
which access takes place. These situations are called race condition.
• Threads are racing to access the data
• Different ways race condition can happen:
• Kernel preemption
• Parallel processing
• Detailed video: https://youtu.be/Q9lH_eXGOp4
CRITICAL SECTION
• Common section of code where data is shared and can lead to race
condition
• Can you code lead to race condition?
• Global variables – can multiple threads access it?
• If a process is preempted while accessing data, can the newly scheduled
process access this data?
• What happens if the function is called on multiple processors?
CRITICAL SECTION PROBLEM
• What is the problem here?
• To design a protocol that process can use to cooperate or that doesn’t
lead to race condition.
• Each process should request to enter the critical section – entry
section
• Once done, the process should allow the other process to enter – exit
section
CRITICAL SECTION PROBLEM SOLUTION
• Mutual exclusion – only one process can execute in the critical section
at a time.
• Progress
• No process in the critical section & exit section.
• Some process are waiting in the entry section
• Waiting process should not be blocked by any other process
• Bounded wait – process should not starve of the critical section. Fair
solution.
• First two requirements should be fulfilled.
ONE FLAG SOLUTION
• ME -> No
• Progress -> Yes
• Bounded waiting -> no
TWO FLAG SOLUTION
• ME -> YES
• PROGRESS -> NO
• BOUNDED WAIT -> YES
TURN SOLUTION
• ME -> YES
• PROGRESS -> NO
• BOUNDED WAIT -> YES
TWO FLAG + TURN SOLUTION
• PETERSON SOLUTION
• SOFTWARE SOLUTION
• SATISFIES ALL THREE REQUIRMENTS
Limitations of Peterson solution
• Spinlock – wastes cpu cycles
• Doesn’t work well with unknown number of processes
• Doesn’t work well on modern OS having multiple cores
• https://youtu.be/mCYYHCbtsn0
HARDWARE SOLUTIONS
• TestAndSet Instruction
• Swap Instruction
• Hardware guarantees that these instruction cannot be run on
multiple CPU at the same time
• Instruction will be executed atomically
TestAndSet Solution
Always returns input
Changes input to true
Swap instruction solution
DRAWBACK
• Spinlock solutions – wastes lot of CPU cycles
• Detailed video
• Testandset - https://youtu.be/pn3mxfdriFw
• Swap - https://youtu.be/ybYm66oEWzo
SEMAPHORES
WAIT OPERATION
SIGNAL SEMAPHORE
NOTE:
• What if two process runs wait() or signal() operation at the same
time?
• Race condition again
• Hardware solutions can be used to prevent this.
• This is guaranteed by the hardware.
• Software + hardware solution
• Detailed video - https://youtu.be/nPQFBec0q3A
SPINLOCK VS BINARY SEMAPHORE VS
COUNTING SEMAPHORE VS MUTEX
• SPINLOCK
• Process waiting in while loop
• Testandset(), swap(), Peterson solution
• Protecting critical region from race condition
• Wastes cpu cycles
• COUNTING SEMAPHORE
• Sleeping lock used for signaling
• Semaphore s > 1
• Wait() and signal() operation
• BINARY SEMAPHORE
• Sleeping lock used for signaling
• Semaphore s = {0,1}
• Wait() and signal()
• MUTEX
• Sleeping lock used for providing mutual exclusion
• Mutex m
• Wait() and signal()
• Doesn’t wait cpu cycles as spinlock
SPINLOCK VS MUTEX
• Mutex involves context switch
• Spinlock wastes cpu cycles
• Critical section is small, use spinlock
• Otherwise, spinlock
MUTEX VS BINARY SEMAPHORE
• In binary semaphore
• Wait() -> thread1
• Signal() -> thread2
• Mutex
• Wait() -> thread1
• Signal() -> thread2 -> errors
• Detailed video - https://youtu.be/Yv4uyqOjdOU
CLASSICAL PROBLEMS OF
SYNCHRONIZATION
PRODUCER-CONSUMER PROBLEM
• Bounded buffer problem
• A bounded queue
• Producer produces and puts items in the queue
• Consumer consumes from the queue
• Example: network interface card/device
• Problem - https://youtu.be/peiDSe0QbIg
• Solution - https://youtu.be/6jpyz1yxbH8
• C solution - https://youtu.be/caFjPdWsJDU
• Issue
• Buffer empty
• Buffer full
• Produce and consume at the same time
SOLUTION
• Buffer size = N
• Semaphore full = 0
• Semaphore empty = N
• Mutex
READER-WRITER PROBLEM
• A database
• Some process wants to only read the database -> readers
• Some process wants to both read and update -> writer
• Multiple readers -> no issues
• Reader + writer or multiple writers -> issues
• First reader writer problem
• No reader can be kept waiting till writer has acquired the lock
• Problem - https://youtu.be/SjUSnpsJEQs
• C solution - https://youtu.be/-DLCGdiRdMM
•
SOLUTION
Semaphore readcount = 0
Semaphore wrt = 0
mutex
DEADLOCK
REAL LIFE DEADLOCK SOLUTIONS
• You can't get the job without having the (professional) experience and
you can't get the experience without having a job
• https://www.quora.com/What-are-some-real-life-examples-of-
deadlock
THANK YOU

Process Synchronization in operating system | mutex | semaphore | race condition

  • 1.
  • 2.
    PREVIOUS SESSION • IOBOUND VS CPU BOUND PROCESS • SCHEDULING QUEUES AND SCHEDULERS • PREEMPTIVE VS NONPREEMPTIVE SCHEDULING • DISPATCHER AND CONTEXT SWITCH • SCHEDULING ALGORITHMS
  • 3.
    INTERVIEW QUESTIONS • Whatis process synchronization? • What is race condition and critical section? • What is critical section problem? • What are software and hardware solutions for CSP? • Difference between mutex, spinlock, binary semaphore and counting semaphore • Solution to producer-consumer problem using mutex and semaphore • Solution to reader-writer problem using mutex and semaphore • What is bounded buffer problem?
  • 4.
    PROCESS SYNCHRONIZATION • Sharingdata among multiple processes doesn’t lead to corruption of data • Cooperating process v independent process • Multiple threads accessing data concurrently
  • 5.
  • 7.
    Initially i =7, final value = 9 and two threads incrementing i
  • 8.
    Initial value =7, final value = 8
  • 9.
    RACE CONDITION • Whenseveral threads tries to access and manipulate shared data concurrently and the outcome of execution depends on the order in which access takes place. These situations are called race condition. • Threads are racing to access the data • Different ways race condition can happen: • Kernel preemption • Parallel processing • Detailed video: https://youtu.be/Q9lH_eXGOp4
  • 11.
    CRITICAL SECTION • Commonsection of code where data is shared and can lead to race condition • Can you code lead to race condition? • Global variables – can multiple threads access it? • If a process is preempted while accessing data, can the newly scheduled process access this data? • What happens if the function is called on multiple processors?
  • 12.
    CRITICAL SECTION PROBLEM •What is the problem here? • To design a protocol that process can use to cooperate or that doesn’t lead to race condition. • Each process should request to enter the critical section – entry section • Once done, the process should allow the other process to enter – exit section
  • 14.
    CRITICAL SECTION PROBLEMSOLUTION • Mutual exclusion – only one process can execute in the critical section at a time. • Progress • No process in the critical section & exit section. • Some process are waiting in the entry section • Waiting process should not be blocked by any other process • Bounded wait – process should not starve of the critical section. Fair solution. • First two requirements should be fulfilled.
  • 15.
    ONE FLAG SOLUTION •ME -> No • Progress -> Yes • Bounded waiting -> no
  • 16.
    TWO FLAG SOLUTION •ME -> YES • PROGRESS -> NO • BOUNDED WAIT -> YES
  • 17.
    TURN SOLUTION • ME-> YES • PROGRESS -> NO • BOUNDED WAIT -> YES
  • 18.
    TWO FLAG +TURN SOLUTION • PETERSON SOLUTION • SOFTWARE SOLUTION • SATISFIES ALL THREE REQUIRMENTS
  • 19.
    Limitations of Petersonsolution • Spinlock – wastes cpu cycles • Doesn’t work well with unknown number of processes • Doesn’t work well on modern OS having multiple cores • https://youtu.be/mCYYHCbtsn0
  • 20.
    HARDWARE SOLUTIONS • TestAndSetInstruction • Swap Instruction • Hardware guarantees that these instruction cannot be run on multiple CPU at the same time • Instruction will be executed atomically
  • 21.
    TestAndSet Solution Always returnsinput Changes input to true
  • 22.
  • 23.
    DRAWBACK • Spinlock solutions– wastes lot of CPU cycles • Detailed video • Testandset - https://youtu.be/pn3mxfdriFw • Swap - https://youtu.be/ybYm66oEWzo
  • 24.
  • 25.
  • 26.
  • 27.
    NOTE: • What iftwo process runs wait() or signal() operation at the same time? • Race condition again • Hardware solutions can be used to prevent this. • This is guaranteed by the hardware. • Software + hardware solution • Detailed video - https://youtu.be/nPQFBec0q3A
  • 28.
    SPINLOCK VS BINARYSEMAPHORE VS COUNTING SEMAPHORE VS MUTEX • SPINLOCK • Process waiting in while loop • Testandset(), swap(), Peterson solution • Protecting critical region from race condition • Wastes cpu cycles • COUNTING SEMAPHORE • Sleeping lock used for signaling • Semaphore s > 1 • Wait() and signal() operation
  • 29.
    • BINARY SEMAPHORE •Sleeping lock used for signaling • Semaphore s = {0,1} • Wait() and signal() • MUTEX • Sleeping lock used for providing mutual exclusion • Mutex m • Wait() and signal() • Doesn’t wait cpu cycles as spinlock
  • 30.
    SPINLOCK VS MUTEX •Mutex involves context switch • Spinlock wastes cpu cycles • Critical section is small, use spinlock • Otherwise, spinlock
  • 31.
    MUTEX VS BINARYSEMAPHORE • In binary semaphore • Wait() -> thread1 • Signal() -> thread2 • Mutex • Wait() -> thread1 • Signal() -> thread2 -> errors • Detailed video - https://youtu.be/Yv4uyqOjdOU
  • 32.
  • 33.
    PRODUCER-CONSUMER PROBLEM • Boundedbuffer problem • A bounded queue • Producer produces and puts items in the queue • Consumer consumes from the queue • Example: network interface card/device • Problem - https://youtu.be/peiDSe0QbIg • Solution - https://youtu.be/6jpyz1yxbH8 • C solution - https://youtu.be/caFjPdWsJDU • Issue • Buffer empty • Buffer full • Produce and consume at the same time
  • 34.
    SOLUTION • Buffer size= N • Semaphore full = 0 • Semaphore empty = N • Mutex
  • 35.
    READER-WRITER PROBLEM • Adatabase • Some process wants to only read the database -> readers • Some process wants to both read and update -> writer • Multiple readers -> no issues • Reader + writer or multiple writers -> issues • First reader writer problem • No reader can be kept waiting till writer has acquired the lock • Problem - https://youtu.be/SjUSnpsJEQs • C solution - https://youtu.be/-DLCGdiRdMM •
  • 36.
    SOLUTION Semaphore readcount =0 Semaphore wrt = 0 mutex
  • 37.
  • 38.
    REAL LIFE DEADLOCKSOLUTIONS • You can't get the job without having the (professional) experience and you can't get the experience without having a job
  • 39.
  • 40.