Interprocess Communication
Interprocess Communication
Interprocess Communication
-R.R.TEWARI
INTERPROCESS COMMUNICATION
Processes frequently need to communicate with other processes
So, there is a need for a wellstructured communication, without using interrupts, among processes.
RACE CONDITIONS
In operating systems, processes that are working together share some common storage (main memory, file etc.) that each process can read and write. When two or more processes are reading or writing some shared data and the final result depends on who runs precisely when, are called race conditions.
CRITICAL SECTION
The characteristic properties of the code that form a Critical Section are:
Codes that reference one or more variables in a readupdate-write fashion while any of those variables is possibly being altered by another thread. Codes that alter one or more variables that are possibly being referenced in read-update-write fashion by another thread. Codes use a data structure while any part of it is possibly being altered by another thread. Codes alter any part of a data structure while it is possibly in use by another thread.
CRITICAL SECTION
The important point is that when one process is executing shared modifiable data in its critical section, no other process is to be allowed to execute in its critical section. Thus, the execution of critical sections by the processes has to be mutually exclusive in time.
MUTUAL EXCLUSION
It is a way of making sure that if one process is using a shared modifiable data, the other processes will be excluded from doing the same thing.
MUTUAL EXCLUSION
Formally, while one process executes the shared variable, all other processes desiring to do so at the same moment should be kept waiting While that process has finished executing the shared variable, one of the processes waiting to do so should be allowed to proceed. In this fashion, each process executing the shared data (variables) excludes all others from doing so simultaneously. This is called Mutual Exclusion.
MUTUAL EXCLUSION
Note that mutual exclusion needs to be enforced only when processes access shared modifiable data When processes are performing operations that do not conflict with one another they should be allowed to proceed concurrently.
We need four conditions to hold to have a good solution for the critical section problem (mutual exclusion).
HOW TO ACHIEVE MUTUAL EXCLUSION? The mutual exclusion problem is to devise a preprotocol (or entry protocol) and a post-protocol (or exit protocol) to keep two or more threads from being in their critical sections at the same time. Problem When one process is updating shared modifiable data in its critical section, no other process should be allowed to enter in its critical section.
LOCK VARIABLE
Conclusion The flaw in this proposal can be best explained by example. Suppose process A sees that the lock is 0. Before it can set the lock to 1 another process B is scheduled, runs, and sets the lock to 1. When the process A runs again, it will also set the lock to 1, and two processes will be in their critical section simultaneously.
Strict Alteration
In this proposed solution, the integer variable 'turn' keeps track of whose turn is to enter the critical section. Initially, process A inspect turn, finds it to be 0, and enters in its critical section. Process B also finds it to be 0 and sits in a loop continually testing 'turn' to see when it becomes 1. Continuously testing a variable waiting for some value to appear is called the Busy-Waiting.
STRICT ALTERATION
Conclusion Taking turns is not a good idea when one of the processes is much slower than the other. Suppose process 0 finishes its critical section quickly, so both processes are now in their non-critical section. This situation violates above mentioned condition 3.
HOW TO ACHIEVE MUTUAL EXCLUSION? The Bounded Buffer Producers and Consumers
The bounded buffer producers and consumers assumes that there is a fixed buffer size i.e., a finite numbers of slots are available. Statement To suspend the producers when the buffer is full, to suspend the consumers when the buffer is empty, and to make sure that only one process at a time manipulates a buffer so there are no race conditions or lost updates.
HOW TO ACHIEVE MUTUAL EXCLUSION? Trouble arises when 1. The producer wants to put a new data in the buffer, but buffer is already full. Solution: Producer goes to sleep and to be awakened when the consumer has removed data. 2. The consumer wants to remove data from the buffer but buffer is already empty. Solution: Consumer goes to sleep until the producer puts some data in buffer and wakes consumer up.
SEMAPHORES
E.W. Dijkstra (1965) abstracted the key notion of mutual exclusion in his concepts of semaphores. Definition A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V and initialization operation called 'Semaphore-initialize'. P is for Dutch word Proberen means to test and V for Verhogen means to increment. Binary Semaphores can assume only the value 0 or 1. Counting semaphores also called general semaphores can assume only nonnegative values.
SEMAPHORES
The semaphore allows one process to control the shared resource while the other waits for the resource to release. It acts like a traffic signal.
SEMAPHORES
The P (or wait or sleep or down) operation on semaphore S, written as P(S) or wait (S), operates as follows: P(S): IF S > 0 THEN S := S - 1 ELSE (wait on S)
The P operation is intended to indivisibly test an integer variable and to block the calling process if the variable is not positive.
SEMAPHORES
The V (or signal or wakeup or up) operation on semaphore S, written as V(S) or signal (S), operates as follows: V(S): IF (one or more process are waiting on S) THEN (let one of these processes proceed) ELSE S := S +1 The V operation indivisibly signals a blocked process to allow it to resume operation. Usually, the semaphore is initialized to have value 1.
SEMAPHORES
Operations P and V are done as single, indivisible, atomic action. It is guaranteed that once a semaphore operation has started, no other process can access the semaphore until operation has completed. Mutual exclusion on the semaphore, S, is enforced within P(S) and V(S). If several processes attempt a P(S) simultaneously, only one process will be allowed to proceed. The other processes will be kept waiting, but the implementation of P and V guarantees that processes will not suffer indefinite postponement. Semaphores solve the lost-wakeup problem.
SEMAPHORES
Producer-Consumer Problem Using Semaphores The Solution to producer-consumer problem uses three semaphores, namely, full, empty and mutex.
The semaphore 'full' is used for counting the number of slots in the buffer that are full. The 'empty' for counting the number of slots that are empty and semaphore 'mutex' to make sure that the producer and consumer do not access modifiable shared section of the buffer simultaneously.
SEMAPHORES
Initialization Set full buffer slots to 0. i.e., semaphore Full = 0. Set empty buffer slots to N. i.e., semaphore empty = N. For control access to critical section set mutex to 1. i.e., semaphore mutex = 1.
SEMAPHORES
Producer ( ) WHILE (true) produce-Item ( ); P (empty); P (mutex); enter-Item ( ) V (mutex) V (full); Consumer ( ) WHILE (true) P (full) P (mutex); remove-Item ( ); V (mutex); V (empty); consume-Item (Item)