1 / 20

Cooperating Processes

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.

kana
Download Presentation

Cooperating Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Producer: while (((in + 1) % BUFFER_SIZE) == out) ;//falls through loop Consumer Blocked: while (in == out) ; out in In = out = 0

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

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

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

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

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

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

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

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

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

More Related