Lecture 5 - Process Synchonization - Revised
Lecture 5 - Process Synchonization - Revised
Chapter Two
Process Management
ASTU
Department of CSE
Content
Background of Process Synchronization
The Critical-Section Problem
Classic Problems of Synchronization
Semaphores
Monitors
Background to Process Synchronization
• Concurrent Processes can be
• Independent processes
• cannot affect or be affected by the execution of another process.
• Cooperating processes
• can affect or be affected by the execution of another process.
• Cooperating processes can either share directly similar address space or
share data through files and messages.
• Concurrent access to shared data may lead to data inconsistency.
• Concurrent execution requires
• process communication and process synchronization
Process Synchronization
• …mechanisms to ensure the orderly execution of cooperating processes
that share a logical address space, so that data consistency is
maintained.
• Although both producer and consumer routines are correct when executed
separately, they may not properly work when executed concurrently.
• To illustrate this, let’s assume the current value of the count variable is 5
Disadvantages
• Busy-waiting consumes processor time
• Starvation is possible when a process leaves a critical section and
more than one process is waiting
• Deadlock
• If a low priority process has the critical region and a higher priority
process needs, the higher priority process will obtain the
processor(CPU) to wait for the critical region
wakeup(P); }
}
The structure of the producer process The structure of the consumer process
while (true) { while (true) {
// produce an item
wait (full);
wait (empty);
wait (mutex);
wait (mutex);
// remove an item from buffer
// add the item to the buffer
signal (mutex);
signal (mutex);
signal (empty);
signal (full);
// consume the removed
} item
}
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
• Readers – only read the data set; they do not perform any
updates
• Writers – can both read and write.
Problem – allow multiple readers to read at the same time. Only
one single writer can access the shared data at the same time.
• Shared Data
• Data set
• Semaphore mutex initialized to 1.
• Semaphore wrt initialized to 1.
• Integer readcount initialized to 0.
Readers-Writers Problem (Cont.)
The structure of a writer process The structure of a reader process
while (true) {
while (true) { wait (mutex) ;
wait (wrt) ; readcount ++ ;
// writing is performed if (readcount == 1) wait (wrt) ;
signal (mutex)
• Shared data
• Bowl of rice (data set)
• Semaphore chopstick [5] initialized to 1
Dining-Philosophers Problem (Cont.)
• The structure of Philosopher i:
While (true) {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
}
Problems with Semaphores
• Incorrect use of semaphore operations: