000-Lecture 7 -Process Synchronization
000-Lecture 7 -Process Synchronization
000-Lecture 7 -Process Synchronization
• Background
• The Critical-Section Problem
• Peterson’s Solution
• Synchronization Hardware
• Semaphores
• Classic Problems of Synchronization
• Monitors
• Synchronization Examples
• Atomic Transactions
Background
• Independent Process: cannot affect /affected by the other executing processes in
the system (Does not share data with any other process) .
• Cooperative Process: can affect /affected by the other executing processes in the
system:
• Shares data with other processes.
• Require an Inter-Process Communication (IPC) mechanism that allow them to
exchange data and Information using IPC models (Shared memory or Message
passing).
• Concurrent access to shared data may result in data inconsistency problem
(consumer-producer problem).
• Solution:
• A mechanisms to ensure the orderly execution of cooperating processes.
• A solution to the consumer-producer problem that fills all the buffers.
• Having an integer count that keeps track of the number of full buffers.
• Initially, count is set to 0.
• It is incremented / decremented by the producer after it produces/consumes
a buffer.
Producer-consumer
• Solution
• Modify the acquire() and release() semaphore operations.
• Instead, the process continue looping it can block itself.
• The block operation places a process into a waiting queue of the
semaphore and change the state from running to waiting.
• The CPU scheduler can select another process to execute.
• The blocked process should be restarted when some other process
executes a release() operation using wakeup() operation (changes
the process from the waiting state to the ready state).
Semaphore – Solution to require Busy waiting
• Scenario:
• P0 executes S.acquire(), and P1 executes Q.acquire() ➔ S & Q=0.
• P0 executes Q.acquire(), it must wait until P1 executes Q.release().
• P1 executes S.acquire(), it must wait until P0 executes S.release().
• These operations cannot be executed, P0 and P1 are deadlocked.
• DeadLock:
• Two or more processes are waiting indefinitely for an event that can be caused
only by one of the waiting processes.
• indefinite blocking, or starvation
• A processes wait indefinitely within the semaphore (if we add and remove
processes from the list associated with a semaphore in (LIFO) order.
Priority Inversion
• When a higher-priority process needs to modify kernel data that are
currently being accessed by a lower-priority processes.
• Since kernel data are typically protected with a lock, the higher-priority
process will have to wait for a lower-priority one to finish.
• The situation becomes more complicated if the lower-priority process is
preempted in favor of another process with a higher priority.
• Example: assume three processes, Lpriority < Mpriority < Hpriority. and
process H requires resource R, which is locked by process L.
• Process H would wait for L to finish using resource R.
• If process M becomes runnable, thereby preempting process L.
• Indirectly, a process with a lower priority—process M—has affected how
long process H must wait for L to use resource R.
• Solution:
• Use only one priority.
• The Process that access the resource inherit the high priority
(Priority inheritance Protocol)
Classical Problems of Synchronization
•Classical problems used to test newly-proposed
synchronization schemes
• Bounded-Buffer Problem
• Dining-Philosophers Problem
Bounded-Buffer Problem
• N buffers, each can hold one item
• Semaphore mutex initialized to the value 1
• Semaphore full initialized to the value 0
• Semaphore empty initialized to the value N
Dining-Philosophers Problem
• 5 philosophers (PH) spend their lives thinking and
eating. They share a circular table with five chairs,
each belonging to one PH.
• In the center of the table is a bowl of rice, and a
five single chopsticks
• When a PH thinks, she does not interact with
others.
• When he gets hungry and tries to pick up the two
chopsticks that are closest to her.
• A PH may pick up only one chopstick at a time.
• He cannot pick up a chopstick that is already in
the hand of a neighbor.
• When a hungry PH has both her chopsticks at the
same time, she eats without releasing her
chopsticks. Representation of the need
to allocate several resources
• When she is finished eating, she puts down both
of her chopsticks and starts thinking again. among several processes in a
deadlock-free and starvation-
free manner.
Dining-Philosophers Problem-solution
• Represent each chopstick with a semaphore.
• A PH tries to grab the chopstick by executing an acquire() operation;
• she releases a chopstick by executing the release() operation.