
OperatingSystemsCh5Process Communication & Synchronization1
Operating Systems Ch5 Process Communication & Synchronization 1

Story so far..· Process concept + operations- Programmer's perspective + kernel's perspectiveThread-Lightweight process We mainly talked about the stuffs related to a singleprocess/thread, what if multiple processes exist..2
Story so far. • Process concept + operations – Programmer’s perspective + kernel’s perspective • Thread – Lightweight process • We mainly talked about the stuffs related to a single process/thread, what if multiple processes exist. 2

Processes. The processes within a system may be-independent or: Independent process cannot affect or be affected by otherprocesses-cooperating· Cooperating process can affect or be affected by otherprocesses Note: Any process that shares data with others is acooperating process3
3 Processes • The processes within a system may be – independent or • Independent process cannot affect or be affected by other processes – cooperating • Cooperating process can affect or be affected by other processes • Note: Any process that shares data with others is a cooperating process

CooperatingProcesses. Why we need cooperating processes-Information sharing: e.g., shared file-Computationspeedup·executing subtasks inparallel-Modularity: dividing system functions into separate processes-Convenience: single user can have multiple processes to execute many tasks4
4 Cooperating Processes • Why we need cooperating processes – Information sharing • e.g., shared file – Computation speedup • executing subtasks in parallel – Modularity • dividing system functions into separate processes – Convenience • single user can have multiple processes to execute many tasks

s communication (IPC)Inter-process- What and how?
5 P P P Inter-process communication (IPC) - What and how?

Interprocess Communication· IPC: used for exchanging data between processes- Cooperating processes need·interprocess communication (IpC)for exchanging dataHow to illustrate? Paradigm for cooperating processes Producer-consumer problem, useful metaphor for manyapplications (abstractedproblem model)producer process produces information that is consumed by aconsumerprocess·Atleastoneproducerandoneconsumer6
6 Interprocess Communication • IPC: used for exchanging data between processes – Cooperating processes need • interprocess communication (IPC) for exchanging data • How to illustrate? – Paradigm for cooperating processes – Producer-consumer problem, useful metaphor for many applications (abstracted problem model) • producer process produces information that is consumed by a consumer process • At least one producer and one consumer

Two models Two (abstracted) models of IPC-Sharedmemory: Establish a shared memory region, read/write to shared region.Accesses aretreated as routine memory accesses·FasterprocessAsharedmemoryprocess Bkernel7
7 Two models • Two (abstracted) models of IPC – Shared memory • Establish a shared memory region, read/write to shared region • Accesses are treated as routine memory accesses • Faster

Two models Two (abstracted) models of IPC-Messagepassing·Exchange message.Requirekernel intervention·EasiertoimplementindistributedsystemprocessAprocess BmessagegueuemTkernel8
8 Two models • Two (abstracted) models of IPC – Message passing • Exchange message • Require kernel intervention • Easier to implement in distributed system

Shared Memory.Producer-consumerproblem- A buffer is needed to allow processes to run concurrentlyProducerConsumerdequeueenqueuebounded/unboundedbuffer-Itisasharedobject;Abuffer-It is a queue (imagine that it is an array implementation of queue)Aproducer-Itproducesaunitofdata,and-writesthata pieceof data tothetail of thebufferat onetime.process-It removes a unit of datafromthehead of thebounded buffer atAconsumerone time.process9
Shared Memory • Producer-consumer problem – A buffer is needed to allow processes to run concurrently 9 A buffer -It is a shared object; -It is a queue (imagine that it is an array implementation of queue). A producer process -It produces a unit of data, and -writes that a piece of data to the tail of the buffer at one time. A consumer process -It removes a unit of data from the head of the bounded buffer at one time. bounded/unbounded buffer Producer enqueue dequeue Consumer

Shared Memory. Focus on bounded buffer: what are the requirements?Whentheproducerwantsto(a)putanewiteminthebuffer,but(b)thebufferis already full..Producer-consumerThen,requirement #1(1)Theproducershouldbe suspended,and(2)Theconsumershouldwaketheproducerupaftershehasdequeued an item.Whentheconsumerwantsto(a)consumesanitemfromthebuffer,but(b) the buffer is empty..Producer-consumerThen,requirement #2(1)Theconsumershouldbesuspended,and(2)Theproducershouldwaketheconsumerupaftershehasenqueuedanitem10
Shared Memory 10 Producerconsumer requirement #1 When the producer wants to (a) put a new item in the buffer, but (b) the buffer is already full. Producerconsumer requirement #2 When the consumer wants to (a) consumes an item from the buffer, but (b) the buffer is empty. Then, (1) The producer should be suspended, and (2) The consumer should wake the producer up after she has dequeued an item. Then, (1) The consumer should be suspended, and (2) The producer should wake the consumer up after she has enqueued an item. • Focus on bounded buffer: what are the requirements?