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

Tanenbaum & Woodhull, Operating Systems: Design and Implementation, (c) 2006 Prentice-Hall, Inc. All rights reserved. 0-13-142938-8

Tanenbaum & Woodhull, Operating Systems: Design and Implementation, (c) 2006 Prentice-Hall, Inc. All rights reserved. 0-13-142938-8

Tanenbaum & Woodhull, Operating Systems: Design and Implementation, (c) 2006 Prentice-Hall, Inc. All rights reserved. 0-13-142938-8

Tanenbaum & Woodhull, Operating Systems: Design and Implementation, (c) 2006 Prentice-Hall, Inc. All rights reserved. 0-13-142938-8

Tanenbaum & Woodhull, Operating Systems: Design and Implementation, (c) 2006 Prentice-Hall, Inc. All rights reserved. 0-13-142938-8

Tanenbaum & Woodhull. Operating Systems: Design and Implementation. Inc. (c) 2006 Prentice-Hall. All rights reserved.Figure 2-8 Two processes want to access shared memory at the same time. 0-13-142938-8 .

0-13-142938-8 . Operating Systems: Design and Implementation.Problems with Concurrent Execution  Concurrent processes often need to share data (maintained either in shared memory or files) and resources If there is no controlled access to shared data. All rights reserved. Inc. some processes may obtain an inconsistent view of data Non-determinism: the action performed by concurrent processes will depend on the order in which their execution is interleaved   Tanenbaum & Woodhull. (c) 2006 Prentice-Hall.

Inc. Operating Systems: Design and Implementation. All rights reserved.Race Conditions  Situations where processes access the same data concurrently and the outcome of execution depends on the particular order in which the access takes place are called race conditions How can processes coordinate (or synchronise) in order to guard against race conditions?  Tanenbaum & Woodhull. (c) 2006 Prentice-Hall. 0-13-142938-8 .

The Critical Section Problem  When a process executes code that manipulates shared data (or resource). All rights reserved. 0-13-142938-8 . (c) 2006 Prentice-Hall. we say that the process is in its critical section (for that shared data) The execution of critical sections must be mutually exclusive: at any time: only one process is allowed to execute in its critical section (even with multiple CPUs) Each process must request the permission to enter a critical section   Tanenbaum & Woodhull. Inc. Operating Systems: Design and Implementation.

(c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. No process should have to wait forever to enter its critical region. 4. No process running outside its critical region may block other processes. Inc. 2. No assumptions may be made about speeds or the number of CPUs. 0-13-142938-8 . 3. Tanenbaum & Woodhull. No two processes may be simultaneously inside their critical regions.Critical Sections Necessary to avoid race conditions: 1. All rights reserved.

Inc. Tanenbaum & Woodhull. All rights reserved. Operating Systems: Design and Implementation. 0-13-142938-8 . (c) 2006 Prentice-Hall.Mutual Exclusion with Busy Waiting Figure 2-9 Mutual exclusion using critical regions.

(a) Process 0. be sure to note the semicolons terminating the while statements. All rights reserved. (c) 2006 Prentice-Hall. (b) Process 1. In both cases. Tanenbaum & Woodhull. 0-13-142938-8 .Strict Alternation Figure 2-10. Inc. Operating Systems: Design and Implementation. A proposed solution to the critical region problem.

0-13-142938-8 . Inc. Operating Systems: Design and Implementation.Definitions critical section : a section of code that reads/writes (CS) shared data race condition: potential for interleaved execution of a critical section by multiple threads => results are non-deterministic mutual exclusion: synchronization mechanism to avoid (ME) race conditions by ensuring exclusive execution of critical sections Tanenbaum & Woodhull. (c) 2006 Prentice-Hall. All rights reserved.

Figure 2-11 Peterson¶s solution for achieving mutual exclusion. 0-13-142938-8 .Peterson¶s Solution (1) { .. Tanenbaum & Woodhull. All rights reserved. Operating Systems: Design and Implementation. (c) 2006 Prentice-Hall. Inc..

Operating Systems: Design and Implementation. All rights reserved. (c) 2006 Prentice-Hall. 0-13-142938-8 . Tanenbaum & Woodhull. Inc.Peterson¶s Solution (2) Figure 2-11 Peterson¶s solution for achieving mutual exclusion.

0-13-142938-8 . All rights reserved. Operating Systems: Design and Implementation. Inc.The TSL Instruction Figure 2-12. (c) 2006 Prentice-Hall. Entering and leaving a critical region using the TSL instruction. Tanenbaum & Woodhull.

the higher priority process will obtain the processor just to wait for the critical region Tanenbaum & Woodhull. Operating Systems: Design and Implementation. Deadlock ‡ If a low priority process has the critical region and a higher priority process needs it. (c) 2006 Prentice-Hall.Mutual Exclusion Machine Instructions ‡ ± ± ± Disadvantages Busy-waiting consumes processor time Starvation is possible when a process leaves a critical section and more than one process is waiting. All rights reserved. 0-13-142938-8 . Inc.

(c) 2006 Prentice-Hall.. Tanenbaum & Woodhull.The Producer-Consumer Problem (1) . Inc.. All rights reserved. Figure 2-13. The producer-consumer problem with a fatal race condition. 0-13-142938-8 . Operating Systems: Design and Implementation.

The producer-consumer problem with a fatal race condition Tanenbaum & Woodhull. (c) 2006 Prentice-Hall.. 0-13-142938-8 . Inc. Operating Systems: Design and Implementation. All rights reserved.The Producer-Consumer Problem (2) .. Figure 2-13.

(c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. Inc. All rights reserved. the OS will put it in a blocked queue of processes waiting for that semaphore ‡ Tanenbaum & Woodhull. apart from initialization. can only be accessed through 2 atomic and mutually exclusive operations: ± Up (S) ± Down (S) Avoids busy waiting ± when a process has to wait. 0-13-142938-8 .OS Solutions: Semaphores ‡ ‡ Synchronization tool (provided by the OS) that does not require busy waiting A semaphore S is an integer variable that.

Inc.. Operating Systems: Design and Implementation. The producer-consumer problem using semaphores. Tanenbaum & Woodhull. All rights reserved. 0-13-142938-8 . Figure 2-14.The Producer-Consumer Problem (3) . (c) 2006 Prentice-Hall..

Inc.The Producer-Consumer Problem (4) . Tanenbaum & Woodhull.. Figure 2-14. Operating Systems: Design and Implementation. 0-13-142938-8 .. (c) 2006 Prentice-Hall. All rights reserved. The producer-consumer problem using semaphores.

Inc. Operating Systems: Design and Implementation.Binary Semaphores ‡ ‡ ‡ Similar to general (counting) semaphores except that ³count´ is Boolean valued Counting semaphores can be implemented using binary semaphores More difficult to use than counting semaphores (eg: they cannot be initialized to an integer k > 1) Tanenbaum & Woodhull. (c) 2006 Prentice-Hall. All rights reserved. 0-13-142938-8 .

Inc. (c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. All rights reserved. 0-13-142938-8 .Problems with Semaphores Semaphores are a powerful tool for enforcing mutual exclusion and coordinate processes Problem: wait(S) and signal(S) are scattered among several processes ‡ It is difficult to understand their effects ‡ Usage must be correct in all processes ‡ One bad (or malicious) process can fail the entire collection of processes Tanenbaum & Woodhull.

All rights reserved. (c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. Inc.Monitors Are high-level language constructs that provide equivalent functionality to semaphores but are easier to control Tanenbaum & Woodhull. 0-13-142938-8 .

Monitors Is a software module containing: ‡ one or more procedures ‡ an initialization sequence ‡ local data variables Characteristics: ‡ local variables accessible only by monitor¶s procedures ‡ a process enters the monitor by invoking one of it¶s procedures ‡ only one process can be in the monitor at any one time Tanenbaum & Woodhull. Inc. Operating Systems: Design and Implementation. All rights reserved. (c) 2006 Prentice-Hall. 0-13-142938-8 .

(c) 2006 Prentice-Hall. All rights reserved.Monitors The monitor ensures mutual exclusion ‡ no need to program this constraint explicitly Shared data are protected by placing them in the monitor ‡ The monitor locks the shared data on process entry Process synchronization is done using condition variables. 0-13-142938-8 . Inc. Operating Systems: Design and Implementation. which represent conditions a process may need to wait for before executing in the monitor Tanenbaum & Woodhull.

‡ If several such process exists: choose any one ‡ If no such process exists: do nothing Tanenbaum & Woodhull. Inc.Condition Variables Local to the monitor (accessible only within the monitor) Can be access and changed only by two functions: ‡ wait(a): blocks execution of the calling process on condition (variable) a ‡ the process can resume execution only if another process executes signal(a) ‡ signal(a): resume execution of some process blocked on condition (variable) a. Operating Systems: Design and Implementation. 0-13-142938-8 . All rights reserved. (c) 2006 Prentice-Hall.

Operating Systems: Design and Implementation. 0-13-142938-8 . (c) 2006 Prentice-Hall. All rights reserved.Monitors Awaiting processes are either in the entrance queue or in a condition queue A process puts itself into condition queue cn by issuing wait(cn) signal(cn) brings into the monitor one process in condition cn queue signal(cn) blocks the calling process and puts it in the urgent queue (unless signal is the last operation of the monitor procedure) Tanenbaum & Woodhull. Inc.

(c) 2006 Prentice-Hall. 0-13-142938-8 . All rights reserved. Inc. Operating Systems: Design and Implementation.Monitors (1) Figure 2-15. Tanenbaum & Woodhull. A monitor.

Operating Systems: Design and Implementation. (c) 2006 Prentice-Hall. An outline of the producer-consumer problem with monitors. The buffer has N slots Tanenbaum & Woodhull. Inc. All rights reserved. 0-13-142938-8 . Only one monitor procedure at a time is active.Monitors (2) Figure 2-16.

0-13-142938-8 . Operating Systems: Design and Implementation. (c) 2006 Prentice-Hall. Only one monitor procedure at a time is active. All rights reserved. The buffer has N slots Tanenbaum & Woodhull.Monitors (3) Figure 2-16. An outline of the producer-consumer problem with monitors. Inc.

Inc. Receive( source . Tanenbaum & Woodhull. 0-13-142938-8 . &message). &message). (c) 2006 Prentice-Hall. All rights reserved. Operating Systems: Design and Implementation.Message Passing Send( destination .

Figure 2-17. Inc. Tanenbaum & Woodhull.Message Passing (1) . (c) 2006 Prentice-Hall. Operating Systems: Design and Implementation.. The producer-consumer problem with N messages. 0-13-142938-8 . All rights reserved..

All rights reserved. Inc. The producer-consumer problem with N messages. (c) 2006 Prentice-Hall. Tanenbaum & Woodhull. 0-13-142938-8 .Message Passing (2) . Operating Systems: Design and Implementation.. Figure 2-17..

Operating Systems: Design and Implementation.The Dining Philosophers Problem (1) Figure 2-18. (c) 2006 Prentice-Hall. Tanenbaum & Woodhull. All rights reserved. Inc. 0-13-142938-8 . Lunch time in the Philosophy Department.

The Dining Philosophers Problem (2) Figure 2-19. Inc. Operating Systems: Design and Implementation. A nonsolution to the dining philosophers problem. 0-13-142938-8 . (c) 2006 Prentice-Hall. Tanenbaum & Woodhull. All rights reserved.

. (c) 2006 Prentice-Hall. A solution to the dining philosophers problem. All rights reserved. Operating Systems: Design and Implementation.. 0-13-142938-8 . Figure 2-20. Tanenbaum & Woodhull. Inc.The Dining Philosophers Problem (3) .

All rights reserved. Tanenbaum & Woodhull.. A solution to the dining philosophers problem. . (c) 2006 Prentice-Hall. Operating Systems: Design and Implementation..The Dining Philosophers Problem (4) . Figure 2-20. Inc... 0-13-142938-8 .

.. All rights reserved.The Dining Philosophers Problem (5) . Figure 2-20. Operating Systems: Design and Implementation. 0-13-142938-8 . (c) 2006 Prentice-Hall. Tanenbaum & Woodhull. A solution to the dining philosophers problem. Inc.

A solution to the readers and writers problem.. Operating Systems: Design and Implementation.The Readers and Writers Problem (1) . (c) 2006 Prentice-Hall. All rights reserved.. 0-13-142938-8 . Inc. Figure 2-21. Tanenbaum & Woodhull.

. 0-13-142938-8 . Inc. Figure 2-21. A solution to the readers and writers problem. Operating Systems: Design and Implementation. (c) 2006 Prentice-Hall. All rights reserved. Tanenbaum & Woodhull..The Readers and Writers Problem (2) .

0-13-142938-8 .Threads (1) Figure 2-6 (a) Three processes each with one thread. Operating Systems: Design and Implementation. All rights reserved. (c) 2006 Prentice-Hall. Tanenbaum & Woodhull. Inc.

Inc. 0-13-142938-8 . (c) 2006 Prentice-Hall. All rights reserved. Tanenbaum & Woodhull.Threads (2) Figure 2-6 (b) One process with three threads. Operating Systems: Design and Implementation.

(c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. All rights reserved.Tanenbaum & Woodhull. 0-13-142938-8 . Inc.

0-13-142938-8 . All rights reserved. Operating Systems: Design and Implementation. (c) 2006 Prentice-Hall. Inc.Benefits Tanenbaum & Woodhull.

(c) 2006 Prentice-Hall. Inc. Operating Systems: Design and Implementation. 0-13-142938-8 .Multithreading Models Tanenbaum & Woodhull. All rights reserved.

Many to One Tanenbaum & Woodhull. (c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. Inc. 0-13-142938-8 . All rights reserved.

(c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. Inc. All rights reserved.One to One Tanenbaum & Woodhull. 0-13-142938-8 .

All rights reserved. (c) 2006 Prentice-Hall.Many to Many Tanenbaum & Woodhull. Inc. Operating Systems: Design and Implementation. 0-13-142938-8 .

Tanenbaum & Woodhull. The first column lists some items shared by all threads in a process. All rights reserved. The second one lists some items private to each thread. 0-13-142938-8 . Operating Systems: Design and Implementation. (c) 2006 Prentice-Hall.Threads (3) Figure 2-7. Inc.

(c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. All rights reserved.Thread Scheduling (1) (a) Figure 2-28. Tanenbaum & Woodhull. (a) Possible scheduling of user-level threads with a 50-msec process quantum and threads that run 5 msec per CPU burst. 0-13-142938-8 . Inc.

All rights reserved.Thread Scheduling (2) (b) Figure 2-28. (b) Possible scheduling of kernel-level threads with the same characteristics as (a). 0-13-142938-8 . (c) 2006 Prentice-Hall. Inc. Operating Systems: Design and Implementation. Tanenbaum & Woodhull.

Inc.Deadlocks Tanenbaum & Woodhull. Operating Systems: Design and Implementation. (c) 2006 Prentice-Hall. 0-13-142938-8 . All rights reserved.

. (c) 2006 Prentice-Hall. Operating Systems: Design and Implementation. ‡ ‡ ‡ Tanenbaum & Woodhull. 0-13-142938-8 .. All rights reserved. Inc.Deadlock ‡ Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involves conflicting needs for resources by two or more processes There is no satisfactory solution in the general case Some OSes (ex: Unix SVR4) ignore the problem and pretend that deadlocks never occur.

Inc. 0-13-142938-8 . All rights reserved. Operating Systems: Design and Implementation.Tanenbaum & Woodhull. (c) 2006 Prentice-Hall.

Sign up to vote on this title
UsefulNot useful