Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected."— Presentation transcript:

1 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

2 Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actions –Shared memory –Message Passing

3 Communications Models

4 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.

5 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.

6 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.

7 Bounded-Buffer – Shared-Memory Solution Shared data #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;

8 Buffer is empty when in == out ; Buffer is full when ((in + 1) % BUFFER_SIZE) == out ;

9 Bounded-Buffer – Producer Process item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; //BUSY WAIT buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; }

10 Bounded-Buffer – Consumer Process item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; }

11 out in while (in == out) ; while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Producer: Consumer Blocked: In = out = 0

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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).


Download ppt "Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected."

Similar presentations


Ads by Google