Inter-process Communication 
Independent process cannot affect or be affected by

the execution of another process. 
Cooperating process can affect or be affected by the

execution of another process 
Advantages of process cooperation

‡ Information sharing ‡ Computation speed-up ‡ Modularity ‡ Convenience

Three objectives: 1. Passing information from one process to another 2. Making sure two processes don·t get into each others· way (e.g., trying to grab the last 1MB of memory) 3. Making sure the proper sequencing is implemented (e.g., if process A produces data used by process B, B should not start using the data before A finishes producing it). Discussion applies to threads as well (except 1).

Communication Models
Msg Passing Shared Memory

process A

M

process A 1 shared memory 2

process B

M

process B

2

1

kernel

M

kernel

(a)

(b)

Definitions  Race Conditions: Two or more processes are reading and writing on shared data and the final result depends on who runs precisely when  Mutual exclusion : Making sure that if one process is accessing a shared memory. the other will be excluded from doing the same thing  Critical region: The part of the program where shared variables are accessed .

Critical Region Mutual exclusion using critical regions .

Requirements for Correct Mutual Exclusion? .

Mutual Exclusion Four conditions to provide CORRECT mutual exclusion 1. 3. 2. No two processes simultaneously in critical region No assumptions made about speeds or numbers of CPUs No process running outside its critical region may block another process 4. No process must wait forever to enter its critical region .

Mutexes  Monitors  Message passing .Mutual Exclusion  Disabling Interrupts  Busy waiting: Testing a variable until some value appears ‡ Lock Variables ‡ Strict Alternation ‡ Peterson·s Solution ‡ Test and Set Lock (TSL)  Sleep and wakeup  Semaphores.

Disabling Interrupts  Process disables all interrupts before entering its critical region  (CPU is switched between processes only as a result of interrupts)  Enables all interrupts just before leaving its critical region  So disabling interrupts guarantees that there will be no process switch  Disadvantage: ‡ Should give the power to control interrupts to user (what if a user turns off the interrupts and never turns them on again?) ‡ Does not work in case of multiple CPUs. Only the CPU that executes the disable instruction is affected.  Not suitable approach for the general case but can be used by the Kernel when needed .

‡ If lock = 1 then wait until it is set to 0  Does the above scheme work? . set it back to 0 before leaving the critical region.Mutual Exclusion with Busy Waiting: Lock Variables  A lock that uses busy waiting is called a spin lock  A single shared lock variable (lock) initially 0  When a process wants to enter its critical region ‡ Check if the lock is 0 ‡ If lock = 0 then set it to 1 and enter the critical region.

Mutual Exclusion with Busy Waiting: Strict Alternation Process 0 Initially turn = 0 Process 1 .

process 1 has to wait for process 0 to finish its non-critical region so that it can enter its critical region This violates the third condition of providing mutual exclusion . and leaves critical region 3. sets turn to 1. sets turn to 0.Process 1 enters critical region. enters non critical region 2.Process 0 Process 1 Scenario: 1.Process 1 enters non-critical region.Process 0 leaves critical region. quickly finishes its job and goes back to the while loop Since turn is 0.

Mutual Exclusion with Busy Waiting: Peterson·s Solution .

Mutual Exclusion with Busy Waiting: TSL Instruction  No other processor can reach LOCK during TSL ‡ In order to achieve that. CPU executing TSL instruction locks the memory bus to prevent other processors accessing LOCK .

H does busy waiting ²But L is never scheduled while H is running ²And the system is blocked forever. .Mutual Exclusion with Busy Waiting  Causes a problem called ´PRIORITY INVERSIONµ  Two processes: H (high priority) and L (Low Priority) ‡ Scheduler must make sure that H is executed whenever it is in ready state Scenario: ²Process H blocks for I/O ²Process L now can execute and enters its critical section ²I/O operation H waits for is now complete ²H is scheduled now but since L is in the critical section.

Producer Consumer Problem (bounded buffer)  Two processes: producer and consumer that share a fixed size buffer  Producer puts an item into the buffer  Consumer takes out an item from the buffer  PROBLEMS: ‡ What happens when the producer wants to put an item into the buffer while the buffer is already full? ‡ OR when the consumer wants to consume an item from the buffer when the buffer is empty? .

Sleep and Wakeup .

Since consumer is not sleeping yet. and issues a wakeup call.Consumer is running It checks count when count == 0 Scheduler decides to run Producer just before consumer sleeps Producer inserts an item and increments the count Producer notices that count is 1. and after filling up the buffer it sleeps Both processes sleep forever (or until the OS comes and sends a kill signal to kill both) . the wakeup signal is lost Scheduler decides to run the consumer Consumer sleeps Producer is scheduled. which runs N times.

then the process is put to sleep without completing its down operation ‡ Up: increments the value of the semaphore  If there are processes sleeping on the semaphore.Solution: Semaphores by Dijkstra (1930 ² 2002)  A semaphore can be ‡ 0 when no wakeups are present ‡ > 0 when there are pending wakeups  Two atomic operations on a semaphore ‡ Down: checks the value of the semaphore  If value is > 0. then one of them is chosen. then it decrements it (by using one wakeup signal) and continues  If value = 0.  Disabling interrupts when one CPU exists or TSL instruction could be used when multiple CPUs exist . and it is allowed to complete its down operation  Checking the value of a semaphore and updating them is done in an atomic fashion.

The producer-consumer problem using semaphores .

//Buffer is full  Producer will enter the critical region and will not be able to proceed since the buffer is full  Consumer cannot access the critical region since the producer is already present  The system will enter an indefinite state . down(&mutex). //Enter critical region down(&empty). What happens when we do a down on mutex first in the producer function? void producer(void) { int item. while(TRUE){ item=produce_item().

but condition variables are required to block the processes when they cannot proceed: wait and signal . useful for achieving mutual exclusion By turning all the critical regions into monitor procedures. variables and data structures that are grouped together as a module Important property: Only one process can be active in a monitor at any instant.Monitors Monitor is a collection of procedures. no two processes will execute their CR·s at the same time Mutual exclusion can be achieved using monitors.

remove. begin while TRUE do begin item = produce_item. consume_item(item).insert(item). begin while TRUE do begin item = ProducerConsumer. end end // make a new item // call enter function in monitor // call remove function in monitor // consume an item .procedure Producer(). end end procedure Consumer. ProducerConsumer.

wake producer end. block insert_item(item). // increment count of full slots if (count == 1) signal(empty). // if buffer is empty.monitor ProducerConsumer condition full. empty. begin if (count == N) wait(full). begin if (count == 0) wait(empty). // if buffer is full. // put item in buffer count := count + 1. // wake consumer end procedure remove:integer. end monitor. .1. // decrement count of full slots if (count == N-1) signal(full). count := 0. integer count. procedure insert(item:integer). // if buffer was full. // remove item from buffer count := count . block remove_item(item).

Rules: Process doing signal must exit the monitor immediately ‡To avoid having two active processes in the monitor at the same time Wait must come before the signal ‡So that signals will not be lost ‡In practice. the process can check the condition variables to see if the operation is necessary Difference between sleep/wakeup and wait/ signal: ‡Race condition cannot occur: Only one process can be running at a time .

Mutexes Implementation of mutex_lock and mutex_unlock using TSL instruction .

it calls mutex_lock() on a mutex  While leaving the critical region it calls mutex_unlock()  If a mutex is already in locked state. then the thread calling the mutex_lock() on that mutex blocks  When a thread calls mutex_unlock.Mutexes  Simplified versions of semaphores  Variable that can be in one of two states : ‡ Locked (1) ‡ Unlocked (0)  When a thread (or process) needs access to a critical region. . then one of the threads blocked on the mutex is unblocked and allowed to acquire the lock.

monitors solve mutual exclusion between one or more CPUs that have access to a common memory MP: For a distributed system consisting of multiple CPUs. // sends message to destination // receives message from source . each with its own private memory.Message Passing Semaphores. receive(source. &message). &message). connected by a LAN Uses two primitives: send and receive which are system calls rather than language constructs send(destination.

#define N 100 void producer(void) { int item. message m. &m). i. while(TRUE){ item=produce_item(). build_message(&m.i<N. } } void consumer(void) { int item. consume_item(item). message m.&m). while(TRUE){ receive(producer.i++) send(producer. &m). item). receive(consumer. item=extract_item(&m).&m). send(producer.&m). send(consumer. } } //wait for an empty to arrive //construct a message to send //send item to consumer //send N empties //get message containing item //extract item from message //send back empty reply . for (i=0.

To avoid this acknowledgement can be sent by the receiver and the sender ‡If message is sent and acknowledgement is lost.Design Issues: Messages can be lost. retransmission may happen (duplication of msgs) Authentication: Authenticating the sender and the receiver Performance: Copying messages from one process to another is always slower than doing a semaphore operation or entering a monitor .

Barriers Useful for groups of processes rather than two processes No process may proceed into the next phase until all processes are ready to proceed to the next phase Can be achieved using barriers No process may start on iteration n+1 until iteration n is complete .

A B a r r i e r D A B a r r i e r B a r r i e r A Process B B B C C C D D Time Time Time .

Classical IPC Problems Dining philosophers problem (Dijkstra) Models processes competing for exclusive access to a limited number of resources such as I/O devices Readers and writers problem (Courtois et al.) Models access to a database (both read and write) Sleeping barber problem Models queuing situations such as a multi-person helpdesk with a computerized call waiting system for holding a limited number of incoming calls .

Dining Philosophers ‡ ‡ ‡ ‡ 5 Philosophers around a table and 5 forks Philosophers eat (spaghetti)/think Eating needs 2 forks Pick one fork at a time (first the left fork then the 2 right one and eat) R L 1 3 What problems may occur in this case? 4 0 .

All philosophers picking up their left forks Right forks will not be available Put back the forks and pick the forks again simultaneously Results in Starvation: All the programs continue to run indefinitely without making any progress .

Dining Philosopher: Solution If i=2. LEFT=1 RIGHT=3 .

.

it will lock the database against writers) ± A reader that finishes reading will decrement rc (when the rc=0 it will unlock the database so that a writer can proceed) ± A writer can have access to the database when rc = 0 and it will lock the database for other readers or writers ± Readers will access the database only when there are no writers (but there may be other readers) .Readers and Writers Problem  Processes compete for reading from and writing to a file  Multiple processes may read the file without any problem  A process can write only if there are no other processes reading or writing  Basic steps on the r/w problem ± rc = reader counter (number of processes currently reading the file) ± A reader who gains access to the database increments rc (when rc=1.

The Readers and Writers Problem Problem? The writer will starve when there is constant supply of readers !!!! Solution is to queue new readers behind the current writer at the expense of reduced concurrency A solution to the readers and writers problem .

and the barber is busy. then he will wakeup the barber ‡When a new customer arrives. then he will sit on the chairs if there is any available. .Sleeping Barber Problem ‡ There is one barber. and n chairs for waiting customers ‡If there are no customers. then the barber sits in his chair and sleeps (as illustrated in the picture) ‡When a new customer arrives and the barber is sleeping. otherwise (when all the chairs are full) he will leave.

 If a customer arrives when the shop is full.Barber Shop Hints Consider the following:  Customer process should invoke a function named getHairCut. he exits. .  Barber process should invoke cutHair.  When the barber invokes cutHair there should be exactly one process invoking getHairCut concurrently.

// To read current value of a semaphore void barber (void) { while(TRUE){ down(&customers). up(&mutex). int waiting =0. // barber is idle semaphore mutex = 1. // # customers waiting for service semaphore barbers =0.#define CHAIRS 5 typedef int semaphore // #chairs for waiting customers semaphore customers = 0. } } // goto sleep if customers=0 // enter critical region // barber is busy // leave critical region . waiting = waiting-1. up(&barbers). down(&mutex). cutHair().

if barber is not free .void customer (void) { down(&mutex). } } // No free chairs. getHairCut(). up(&customers). if (waiting<CHAIRS) { waiting=waiting+1. down(&barbers). } else{ up(&mutex). up(&mutex). if necessary // goto sleep. leave // wake up barber.

Sign up to vote on this title
UsefulNot useful