Post on 19-Dec-2015
Concurrency: Mutual Exclusion and Synchronization
• Why we need Mutual Exclusion?
• Classical examples:
• Bank Transactions:Read Account (A); Read Account (A);
Compute A = A + 1000; Compute A = A – 500;
Write Account (A); Write Account (A);
• Keep A = B all the timeA = A + 1; A = 2 * A;
B = B + 1; B = 2 * B;
• Wrong results if mutual exclusion is not enforced!
Disable InterruptSoftware Solution•Dekker’s Algorithm•Peterson’s Algorithm
Hardware Solution•Disable Interrupt•Test & Set•Exchange Instruction
Monitors
• Semaphores is a powerful and flexible tool for enforcing mutual exclusion and for coordinating processes.
• It may be difficult to produce a correct program and very difficult to debug.
• The difficulty is that wait and signal operations will be scattered throughout a program and it is not easy to see the overall effect of these operations on the semaphores they affect.
• A monitor is a programming language construct that provides equivalent functionality to that of semaphores but easier to control.
• Monitors is implemented in:– Concurrent Pascal
– Pascal-Plus
– Modula-2, Module-3
– Ada
– As a programming library
Monitors (continue)• Monitors can lock on any object.
• One may want to lock all linked list with one lock;
• or lock a particular linked list by one lock;
• or lock an element of a particular linked list by one lock.
Monitor with Signal– cwait(condition): Suspended execution of the calling process on a condition. The
monitor is now available for use by another process.
– csignal(condition): Resume execution of some process suspended after a cwait on the same condition. If there are several such processes, choose one of them; if there is no such process, do nothing.
• A monitor is a software module consisting of one or more procedures, an initialization sequence, and local data.
– The local data variables are accessible only by the monitor’s procedures and not by any external procedures.
– A process enters the monitor by invoking one of its procedures.
– Only one process may be executing in the monitor at a time; any other process that has invoked the monitor is suspended, waiting for the monitor to become available.
Message Passing• Two fundamental requirements must be satisfied when processes interact: synchronization and
communication.
• Message passing is a common approach for distributed systems.
• It is normally provided in the form of a pair of primitives:
– send(destination, message)
– receive(source, message)
• Major design characteristics of message systems for Inter-processor Communication and Synchronization:
– Synchronization
• Send : blocking vs. non-blocking
• Receive : blocking, non-blocking, test for arrival
– Addressing:
• Direct
– Send
– Receive : explicit vs. implicit
• Indirect
– Static, dynamic, ownership
– Format: message content; Length -- fixed vs. variable
– Queuing Discipline: FIFO, Priority Driven
Readers/Writers Problem
The readers/writers problem is defined as follows:
• There is a data shared among a number of processes.
• The data area could be a file, a block of main memory, or even a back of processor registers.
• There are a number of processes that only read the data (readers) and a number of processes that only write to the data (writers)
• The following conditions must be satisfied:
– 1. Any number of readers may read the data at the same time.
– 2. Only one writer at a time may write to the data.
– 3. If a writer is writing to the data, no reader may read it.
• Two observations:
– Readers have priority (writers might starve)
– Writers have priority