Cooperating Processes 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
Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actions –Shared memory –Message Passing
Communications Models
Shared Memory in POSIX Requires allocating a shared memory region Attaching region to processes address space –Read/write to memory as usual. Detach from address space Delete shared memory.
Producer-Consumer Problem Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. –unbounded-buffer places no practical limit on the size of the buffer. –bounded-buffer assumes that there is a fixed buffer size.
Constraints Do not over-write an item not yet consumed. Do not write to a full buffer Do not read from a previously read item. Do not read from an empty buffer.
Bounded-Buffer – Shared-Memory Solution Shared data #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;
Buffer is empty when in == out ; Buffer is full when ((in + 1) % BUFFER_SIZE) == out ;
Bounded-Buffer – Producer Process item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; //BUSY WAIT buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; }
Bounded-Buffer – Consumer Process item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; }
out in while (in == out) ; while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Producer: Consumer Blocked: In = out = 0
outin while (in == out) ; // falls through loop while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Producer: Consumer : O in = 1, out = 0 //either can fire
outin while (in == out) ; //falls through loop while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Producer: Consumer : O in = 2, out = 0 //either can fire O
out in while (in == out) ; //falls through loop while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Producer: Consumer : O in = 3, out = 0 //either can fire OO
out in while (in == out) ; // falls through loop while (((in + 1) % BUFFER_SIZE) == out) ; //blocks Producer: Consumer : O in = 4, out = 0 // consumer can fire OOO
out in while (in == out) ; // falls through loop while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Producer: Consumer : X in = 4, out = 1 // producer and consumer can fire OOO
outin while (in == out) ; // falls through loop while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Producer: Consumer : X in = 0, out = 2 // producer and consumer can fire XOOO
outin while (in == out) ; // falls through loop while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Producer: Consumer : X in = 0, out = 3 // producer and consumer can fire XOOX
outin while (in == out) ; // falls through loop while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Producer: Consumer : O in = 1, out = 4 // producer and consumer can fire XOXX
Message Passing Requires significantly less programming. Do not need to share address space (i.e., can be extended to processes executing on a different system when connected via some network).