Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Processes and Threads Part II Chapter 2 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.

Similar presentations


Presentation on theme: "1 Processes and Threads Part II Chapter 2 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling."— Presentation transcript:

1 1 Processes and Threads Part II Chapter 2 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

2 2 2.3 Interprocess Communication 2.3.1 Race Conditions Two processes want to access shared memory (variable in) at same time and they may put their files in the same slot Race Condition: When more than one process shares the same memory and the final result depends on the order of execution

3 3 2.3.2 Critical Regions (1) Four conditions to provide mutual exclusion 1. No two processes simultaneously in critical region 2. No assumptions made about speeds or numbers of CPUs 3. No process running outside its critical region may block another process 4. No process must wait forever to enter its critical region

4 4 2.3.2 Critical Regions (2) Mutual exclusion using critical regions

5 5 2.3.3 Mutual Exclusion with Busy Waiting (1) Disabling interrupts –Each process disables all interrupts just after entering its critical region(CR) and enables all interrupts before leaving it –Unsafe for user processes able to turn off all interrupts Lock Variables –Having a shared variable (lock), initially 0 –Before entering its CR the process checks lock, if lock is1, it waits; if lock is 0, it sets lock to 1 and enters its CR. –Before leaving its CR, the process resets lock to 0 –It does not work, why?

6 6 2.3.3 Mutual Exclusion with Busy Waiting (2) Proposed solution to critical region problem (a) Process 0. (b) Process 1. Variable turn is shared by both processes Strict Alternation

7 7 2.3.3 Mutual Exclusion with Busy Waiting(3) Peterson's solution

8 8 Mutual Exclusion with Busy Waiting (4) TSL(Test and Set Lock) Machine instruction Format: TSL RX, LOCK// LOCK is a shared variable, RX: a register Function: RX  LOCK LOCK  1 Since it is a machine instruction, so it is always executed atomically.

9 9 2.3.5 Semaphores (1) Definition: Class Semaphore { Private: value: integer; list: list of processes Public: void down() { value = value –1; if value < 0 { list.insert(this process) sleep(); } void up() { value = value + 1; if value <= 0 { p = list.remove(); wakeup(p); }

10 10 Semaphore (2) Semaphores are system facilities The up() and down() operations are system calls. The operating system guarantees that the two operations are atomic operations. I.e., when an up() or down() is being executed, no other up() and down() would be executed on the same semaphore.

11 11 Semaphore: application Control execution sequence –E.g., three processes: p1, p2 and p3, and p3’s stmnt3 must be executed after p1’stmnt1 and p2’s stmnt2 have completed. Semaphore seq = 0; P1p2p3 ……… Stmnt1;stmnt2Seq.down(); Seq.up();Seq.up(); Seq.down(); ……stmnt3 Mutual exclusion Semaphore mutex = 1; P1p2 Mutex.down();mutex.down(); Critical region;critical region Mutex.up();mutex.up()

12 12 2.3.5 Semaphores: application The producer-consumer problem using semaphores

13 13 2.3.5 Implementations of Semaphore (3) Disable all interrupts Use Peterson’s algorithm and treat the up() and down() operations as critical regions.

14 14 2.3.6 Mutexes Implementation of mutex_lock and mutex_unlock

15 15 2.3.8 Message Passing (1) Processes can communicate with each other via message passing. The operating system provides two primitive functions: –Send(dest, &message); –Receive(source, &message); Design issues: –Sender waits for acknowledgement, no buffer needed –When sender does not wait, the OS must maintain a buffer –Message scramble and loss

16 16 2.3.8 Message Passing (2) The producer-consumer problem with N messages

17 17 2.4 Classical IPC Problems 2.4.0 The producer and consumer problem

18 18 2.4.1 Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time

19 19 2.4.1 Dining Philosophers (2) A nonsolution to the dining philosophers problem Deadlock? How?How to prevent deadlock

20 20 2.4.2 The Readers and Writers Problem (1) Multiple readers and writers need to access the database Readers only read the content Writers modify the database Multiple readers may read at the same time Only one writer is allowed to write at a time When a writer is writing, no reader is allowed to read

21 21 2.4.2 The Readers and Writers Problem (2)


Download ppt "1 Processes and Threads Part II Chapter 2 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling."

Similar presentations


Ads by Google