You are on page 1of 30

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.

. • To avoid race conditions and flawed results.HOW TO AVOID RACE? • The key to preventing trouble involving shared storage is to find some way to prohibit more than one process from reading and writing the shared data simultaneously. one must identify codes in Critical Sections in each thread. • That part of the program where the shared memory is accessed is called the Critical Section.

 Codes alter any part of a data structure while it is possibly in use by another thread. .  Codes use a data structure while any part of it 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.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.

the execution of critical sections by the processes has to be mutually exclusive in time. . no other process is to be allowed to execute in its critical section. Thus.CRITICAL SECTION • The important point is that when one process is executing shared modifiable data in its critical section.

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. .

one of the processes waiting to do so should be allowed to proceed. • In this fashion.MUTUAL EXCLUSION • Formally. . while one process executes the shared variable. each process executing the shared data (variables) excludes all others from doing so simultaneously. all other processes desiring to do so at the same moment should be kept waiting • While that process has finished executing the shared variable. 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).MUTUAL EXCLUSION CONDITIONS • If we could arrange matters such that no two processes were ever in their critical sections simultaneously. . we could avoid race conditions.

MUTUAL EXCLUSION CONDITIONS No two processes may at the same moment be inside their critical sections. No process should wait arbitrary long to enter its critical section. No process outside its critical section should block other processes. No assumptions are made about relative speeds of processes or number of CPUs. .

no other process should be allowed to enter in its critical section. .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.

• With interrupts turned off the CPU could not be switched to other process. Hence.HOW TO ACHIEVE MUTUAL EXCLUSION? • Disabling Interrupts (Hardware Solution) • Each process disables all interrupts just after entering in its critical section and re-enable all interrupts just before leaving critical section. no other process will enter its critical section and mutual exclusion achieved. . • Disabling interrupts is sometimes a useful technique within the kernel of an operating system. but it is not appropriate as a general mutual exclusion mechanism for users process. The reason is that it is unwise to give user process the power to turn off interrupts.

. and 1 means hold your horses . shared. we consider a single.some process is in its critical section. the process just waits until (lock) variable becomes 0. • When a process wants to enter in its critical section.HOW TO ACHIEVE MUTUAL EXCLUSION? • Lock Variable (Software Solution) • In this solution. it first test the lock. If lock is 0. a 0 means that no process in its critical section. the process first sets it to 1 and then enters the critical section. • If the lock is already 1. • Thus. (lock) variable initially 0.

and two processes will be in their critical section simultaneously. it will also set the lock to 1. and sets the lock to 1.LOCK VARIABLE • Conclusion The flaw in this proposal can be best explained by example. . When the process A runs again. runs. Before it can set the lock to 1 another process B is scheduled. • Suppose process A sees that the lock is 0.

finds it to be 0. the integer variable 'turn' keeps track of whose turn is to enter the critical section. Process B also finds it to be 0 and sits in a loop continually testing 'turn' to see when it becomes 1. and enters in its critical section. process A inspect turn. Initially. • Continuously testing a variable waiting for some value to appear is called the Busy-Waiting. .HOW TO ACHIEVE MUTUAL EXCLUSION? • Strict Alteration • In this proposed solution.

• Suppose process 0 finishes its critical section quickly.STRICT ALTERATION • Conclusion Taking turns is not a good idea when one of the processes is much slower than the other. so both processes are now in their non-critical section. This situation violates above mentioned condition 3. .

start busy waiting) until it is allowed to enter.. Sleep It is a system call that causes the caller to block. If it is not. the process goes into tight loop and waits (i. be suspended until some other process wakes it up.HOW TO ACHIEVE MUTUAL EXCLUSION? • • • Using Systems calls 'sleep' and 'wakeup' Basically. Both 'sleep' and 'wakeup' system calls have one parameter that represents a memory address used to match up 'sleeps' and 'wakeups' • • • • • . Wakeup It is a system call that wakes up the process. This approach wastes CPU-time. what above mentioned solution dooes is this: When a process wants to enter in its critical section .e. it checks to see if the entry is allowed. that is.

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? • The Bounded Buffer Producers and Consumers • The bounded buffer producers and consumers assumes that there is a fixed buffer size i. to suspend the consumers when the buffer is empty. • Statement • To suspend the producers when the buffer is full. a finite numbers of slots are available..e. .

• Two processes share a common. consider the producerconsumer problem also known as bounded buffer problem. The producer puts information into the buffer and the consumer takes information out. . fixedsize (bounded) buffer.BOUNDED BUFFER (PRODUCER CONSUMER) PROBLEM • As an example how sleep-wakeup system calls are used.

The consumer wants to remove data from the buffer but buffer is already empty. 2. The producer wants to put a new data in the buffer.HOW TO ACHIEVE MUTUAL EXCLUSION? • Trouble arises when 1. Solution: Producer goes to sleep and to be awakened when the consumer has removed data. Solution: Consumer goes to sleep until the producer puts some data in buffer and wakes consumer up. but buffer is already full. .

BOUNDED BUFFER PRODUCER & CONSUMER • Conclusion This approach also leads to same race conditions we have seen in earlier approaches. The essence of the problem is that a wakeup call. sent to a process that is not sleeping. . • Race condition can occur due to the fact that access to 'count' is unconstrained. is lost.

SEMAPHORES • E.W. • 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'. . • Counting semaphores also called general semaphores can assume only nonnegative values. • Binary Semaphores can assume only the value 0 or 1. Dijkstra (1965) abstracted the key notion of mutual exclusion in his concepts of semaphores. P is for Dutch word “Proberen” means “to test” and V for “Verhogen” means “to increment”.

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. • It is used to solve the critical section problem. .

SEMAPHORES • The P (or wait or sleep or down) operation on semaphore 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. written as P(S) or wait (S).

the semaphore is initialized to have value 1. • Usually. written as V(S) or signal (S).SEMAPHORES • The V (or signal or wakeup or up) operation on semaphore 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. .

. is enforced within P(S) and V(S). • If several processes attempt a P(S) simultaneously. It is guaranteed that once a semaphore operation has started. atomic action. Mutual exclusion on the semaphore.SEMAPHORES • Operations P and V are done as single. but the implementation of P and V guarantees that processes will not suffer indefinite postponement. indivisible. The other processes will be kept waiting. only one process will be allowed to proceed. no other process can access the semaphore until operation has completed. S. • Semaphores solve the lost-wakeup problem.

namely.SEMAPHORES • Producer-Consumer Problem Using Semaphores • The Solution to producer-consumer problem uses three semaphores. . full. • The semaphore 'full' is used for counting the number of slots in the buffer that are full. empty and mutex. 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.

e. semaphore Full = 0. semaphore mutex = 1.. i. i.e.. • Set empty buffer slots to N.SEMAPHORES • Initialization • Set full buffer slots to 0. semaphore empty = N. i. . • For control access to critical section set mutex to 1.e..

P (empty). enter-Item ( ) V (mutex) V (full). V (mutex). remove-Item ( ). V (empty). Consumer ( ) WHILE (true) P (full) P (mutex). consume-Item (Item) • . P (mutex).SEMAPHORES • Producer ( ) WHILE (true) produce-Item ( ).