Cooperating Processes Independent process cannot affect or be
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 next. Produced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; //BUSY WAIT buffer[in] = next. Produced; in = (in + 1) % BUFFER_SIZE; }
Bounded-Buffer – Consumer Process item next. Consumed; while (1) { while (in == out) ; /* do nothing */ next. Consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; }
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; //falls through loop Consumer Blocked: while (in == out) ; in out 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) O out ; //falls through loop O in in = 2, out = 0 //either can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; //falls through loop Consumer : while (in == out) O out O ; //falls through loop O in in = 3, out = 0 //either can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; //blocks Consumer : while (in == out) O O O ; // falls through loop O out in = 4, out = 0 // consumer can fire in
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) X O out O ; // falls through loop O in in = 4, out = 1 // producer and consumer can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) X in X O ; // falls through loop O O out in = 0, out = 2 // producer and consumer can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) X in X X ; // falls through loop O O out in = 0, out = 3 // producer and consumer can fire
Producer: while (((in + 1) % BUFFER_SIZE) == out) ; // no longer blocked Consumer : while (in == out) O X in X ; // falls through loop X O 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).
- Slides: 20