450 likes | 1.03k Views
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.
E N D
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
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; }
Producer: while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Consumer Blocked: while (in == out) ; out in In = out = 0
Producer: while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Consumer : while (in == out) ; // falls through loop O out in in = 1, out = 0 //either can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Consumer : while (in == out) ; //falls through loop O O out in in = 2, out = 0 //either can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Consumer : while (in == out) ; //falls through loop O O O in out in = 3, out = 0 //either can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; //blocks Consumer : while (in == out) ; // falls through loop O O O O in out in = 4, out = 0 // consumer can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) ; // falls through loop X O O O in out in = 4, out = 1 // producer and consumer can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) ; // falls through loop X X O O O in out in = 0, out = 2 // producer and consumer can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) ; // falls through loop X X X O O in out in = 0, out = 3 // producer and consumer can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) ; // falls through loop O X X X O in out in = 1, out = 4 // producer and consumer can fire
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).