Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inter Process Communication

Similar presentations


Presentation on theme: "Inter Process Communication"— Presentation transcript:

1 Inter Process Communication
Prepared By: Dr. Vipul Vekariya

2 Inter Process Communication
Processes frequently need to communicate with other processes is called the IPC. There are three issues here . How one process can pass the information to other? The second has to do with making sure two or more processes do no get into each other’s way when engaging in their activities. The third concerns proper sequencing when dependencies are present. An operating system must allocate computer resources among the potentially competing requirements of multiple processes. In the case of the processor, the resource to be allocated is execution time on the processor and the means of allocation is scheduling.

3 Race Conditions Two processes want to access shared memory at the same time.

4 Second example of Race Condition
Void echo() { chin=getchar(); chout=chin; putchar(chout); } ->>> b=1 , c=2 P1 : b = b+c P2: c= b+c

5 Race condition Two or more processes are reading or writing some shared data and final result depends on who run specifically when, are called race conditions.

6 Critical Region How do we avoid Race Condition? Mutual Exclusion:
Some way of making sure that if one process is using shared variable or file, the other process will be excluded from doing same thing. The part of the program where shared memory is accessed called the critical region or critical section.

7 Critical Regions Conditions required to avoid race condition:
No two processes may be simultaneously inside their critical regions. No assumptions may be made about speeds or the number of CPUs. No process running outside its critical region may block other processes. No process should have to wait forever to enter its critical region.

8 Critical Regions Mutual exclusion using critical regions.

9 Mutual Exclusion with Busy Waiting
Proposals for achieving mutual exclusion: Disabling interrupts Lock variables Strict alternation Peterson's solution The TSL instruction Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

10 Disabling Interrupts The simplest solution is to have each process disable all interrupts just after entering in critical regions and re- enable them just before leaving it. Problem: This approach is generally unattractive. Because one process do turn off the interrupts and never turned them on again. That could be the end of the system. Furthermore if the system is multi processor, with two more or CPU. Then disabling interrupts affects only one CPU. This solution is convenient for the kernel itself. Not for the user process.

11 Lock Variables Let us look for software solution.
Consider having a single, Shared(lock) Variable initially 0. When process want to enter in critical region, it first test the lock. If the lock is 0, the process is set to 1 and enter in to critical region. If the lock is already 1 , the process is just wait until it becomes 0. Problem: Busy Waiting Same problem like printer spooler

12 Strict Alternation A proposed solution to the critical region problem. (a) Process 0. (b) Process 1. In both cases, be sure to note the semicolons terminating the while statements.

13 Peterson's Solution Problem: Busy Waiting Waste CPU time

14 The TSL Instruction Entering and leaving a critical region using the TSL instruction.

15 Sleep & Wake up Peterson and TSL solution are correct, but both have the defect of requiring busy waiting. Sleep & Wakeup. Sleep is the system call that cause the caller to block, that is be suspended until another process wakes it up. The wake up call has one parameter the process to awakened.

16 Producer Consumer Problem.
It is also known as bounded buffer problem. Two process share a common ,fixed size buffer. The producer put the information in to buffer. Consumer takes it out. Trouble arise when the producer want to put a new item in the buffer, but is already full. The solution is for the producer go to sleep, to be awakened when the consumer has removed one or more items. Similarly if the consumer wants to remove item and see that buffer is empty, it goes to sleep until the producer put the something in the buffer and wake it up. Buffer P C

17 Producer Consumer Problem.(cont.)
To keep track of the umber of item, we will need a variable count. Maximum number of item the buffer can hold is N. First producer code test the count. If count=N then goto sleep otherwise add an item. And increment the count. Consumer code is similar. First it test the count. If count =0, then go to sleep. Otherwise it remove item and decrement the count.

18 The Producer-Consumer Problem
. . .

19 Wake up lost problem Solution: Wake up waiting bit
When wake up is sent to process that is still awake, this bit is set. Later when process tries to go to sleep, If the wakeup bit is on, it will turned off.

20 Semaphores Semaphore is a protected variable that provides a simple but useful abstraction for controlling access by Multiple process to a common resource. Semaphore is as record how many units of particular resource are available. Semaphore are a useful tool in the prevention of race condition and deadlocks. Semaphore which allow an arbitrary resource count are called counting semaphores. Semaphore which are restricted to the values 0 and 1 are called binary semaphores

21 Semaphores(cont.) Counting semaphore are equipped with two operation.
Here two operations. Down and up. The value of the semaphore S is the number of units of the resource that are currently available. Down(P) operation on semaphore check to see if value is greater then 0, if so, it decrement the value and just continue. If the value is 0, the process is put to sleep. This operation is indivisible atomic action. The up(V) operation increment the value of semaphore.

22

23 Semaphores(cont.) Semaphore solve the Lost wakeup Problem.
This solution uses three semaphores. One called full for counting the number of the slots that are full. One called empty for counting the number of slots that are empty. One called mutex to make sure the producer and consumer do not access the buffer at the same time. Full is initially 0. Empty is initially equal to number of slot in buffer. Mutex is initially 1.

24 The producer-consumer problem using semaphores
. . .

25 Mutexes When the semaphore’s ability to count is not needed, a simplified version of the semaphores is called the MUTEX. Mutex are good only for mutual exclusion to shared some resources. This is specially useful in thread packages that are implemented entirely in user space. A mutex is variable that can be one of two states. Unlocked or Locked With 0 meaning unlocked and all other value meaning locked. Thread_yield is thread call which allow thread to voluntary give the CPU and to let another thread run.

26 Mutexes Implementation of mutex lock and mutex unlock.

27 Monitors Hoare and brinch hansen proposed higher level synchronization primitive called Monitors. A monitor is collection of procedures, variables and data structures that group together in a special kind of package. Process may call the monitor procedure but they can not directly access the internal data structure of procedure in monitor. Monitor have an important property to achieved mutual exclusion. Only one process can active in monitor at any instant. It easy to put all the test of buffer full and buffer empty in monitor procedure. But how should producer block when it find the buffer full. This solution lies in condition variable.

28 Synchronization Synchronisation achieved by condition variables within a monitor only accessible by the monitor. Monitor Functions: wait(c): it does wait on some condition variable c and this action cause the calling process to block. signal(c) : wake up its sleeping partner by doing a signal on condition variable. A monitor supports synchronization by the use of condition variables that are contained within the monitor and accessible only within the monitor. cwait(c): Suspend execution of the calling process on condition c. The monitor is now available for use by another process. csignal(c): Resume execution of some process blocked 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.

29 Chief characteristics
Local data variables are accessible only by the monitor Process enters monitor by invoking one of its procedures Only one process may be executing in the monitor at a time Implemented in a number of programming languages, including Pascal, Pascal-Plus,Java The chief characteristics of a monitor are the following: 1. The local data variables are accessible only by the monitor’s procedures and not by any external procedure. 2. A process enters the monitor by invoking one of its procedures. 3. Only one process may be executing in the monitor at a time; any other processes that have invoked the monitor are blocked, waiting for the monitor to become available.

30 Monitors

31 An outline of the producer-consumer problem with monitors.

32 Process Interaction When processes interact with one another, two fundamental requirements must be satisfied: synchronization and communication. Message Passing is one solution to the second requirement Added bonus: It works with shared memory and with distributed systems When processes interact with one another, two fundamental requirements must be satisfied: synchronization and communication. Processes need to be synchronized to enforce mutual exclusion; cooperating processes may need to exchange information. One approach to providing both of these functions is message passing. Message passing has the further advantage that it lends itself to implementation in distributed systems as well as in shared-memory multiprocessor and uniprocessor systems.

33 Message Passing The actual function of message passing is normally provided in the form of a pair of primitives: send (destination, message) receive (source, message) Emphasize that message-passing systems come in many forms. We provide a general introduction that discusses features typically found in such systems. These primitives are a minimum set of operations needed for processes to engage in message passing. A process sends information in the form of a message to another process designated by a destination. A process receives information by executing the receive primitive, indicating the source and the message.

34 Design Issue for Message passing
Message can be lost by network. To guard again lost message the receiver can send the acknowledgement to the sender. Acknowledgement can be lost in network, then sender again retransmit the message. So the receiver get it twice. It is very difficult for receiver to distinguish between old message and new message. If receiver get the same sequence number as the previous message then ignore the new message. Authentication is also an issue in message system. One design issue is that when client and server both are on same machine. Message size, how process are named?

35 Direct Addressing Send primitive includes a specific identifier of the destination process Receive primitive could know ahead of time which process a message is expected Receive primitive could use source parameter to return a value when the receive operation has been performed

36 Indirect addressing Messages are sent to a shared data structure consisting of queues Queues are called mailboxes One process sends a message to the mailbox and the other process picks up the message from the mailbox A strength of the use of indirect addressing is that, by decoupling the sender and receiver, it allows for greater flexibility in the use of messages.

37 General Message Format
The format of the message depends on the objectives of the messaging facility and whether the facility runs on a single computer or on a distributed system. This is a typical message format for operating systems that support variable-length messages. The message is divided into two parts: a header, which contains information about the message. The header may contain an identification of the source and intended destination of the message, a length field, and a type field to discriminate among various types of messages. additional control information, e.g. pointer field so a linked list of messages can be created; a sequence number, to keep track of the number and order of messages passed between source and destination; and a priority field. a body, which contains the actual contents of the message.

38 Producer consumer problem with message passing
We assume that all message are the same size. Message sent but yet not received are buffered automatically by the OS. In this N message is used. It means N slots in shared memory buffer. Consumer start by sending N empty message to the producer. When producer has an item to give the consumer, it takes an empty message and send back a full one. If producer works faster than, it block for some time.

39 Producer-Consumer Problem with Message Passing
. . .

40 Producer-Consumer Problem with Message Passing
. . .

41 Dining Philosophers Problem: Scenario
Five philosophers live in a house, where a table is laid for them. The life of each philosopher consists principally of thinking and eating, and through years of thought, all of the philosophers had agreed that the only food that contributed to their thinking efforts was spaghetti. Due to a lack of manual skill, each philosopher requires two forks to eat spaghetti. A philosopher wishing to eat goes to his or her assigned place at the table and, using the two forks on either side of the plate, takes and eats some spaghetti.

42 Solution This solution required the five statement following the call to think by a Binary semaphores. Before starting acquiring forks , a philosopher would do down on mutex. After replacing forks , she would up on a mutex. From theoretically point it is true. But practically it is wrong. It use an array state to keep track of whether a philosopher is eating, thinking, or hungry. A philosopher move only eating state if neither neighbors is eating. Philosopher I’s neighbors are defined by the macro LEFT and RIGHT. If I is 2, LEFT is 1and RIGHT is 3. The program use an array of semaphores . One per philosopher.

43 Dining Philosophers Problem
. . . . A solution to the dining philosophers problem.

44 Dining Philosophers Problem
. . . . . .

45 Readers/Writers Problem
A data area is shared among many processes Some processes only read the data area, some only write to the area Conditions to satisfy: Multiple readers may read the file at once. Only one writer at a time may write If a writer is writing to the file, no reader may read it. The readers/writers problem is: There is a data area shared among a number of processes. The data area could be a file, a block of main memory,or even a bank of processor registers. There are a number of processes that only read the data area (readers) and a number that only write to the data area (writers). The conditions that must be satisfied are as follows: 1. Any number of readers may simultaneously read the file. 2. Only one writer at a time may write to the file. 3. If a writer is writing to the file, no reader may read it.

46 The Readers and Writers Problem
A solution to the readers and writers problem.

47 The Readers and Writers Problem
. . . . A solution to the readers and writers problem.

48 The Sleeping Barber Problem

49 The Sleeping Barber Problem
Solution to sleeping barber problem.


Download ppt "Inter Process Communication"

Similar presentations


Ads by Google