1 / 21

Processes and Threads Part II

Processes and Threads Part II. 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Chapter 2. 2.3 Interprocess Communication 2.3.1 Race Conditions.

rex
Download Presentation

Processes and Threads Part II

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. Processes and ThreadsPart II 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling Chapter 2

  2. 2.3 Interprocess Communication2.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. 2.3.2 Critical Regions (1) Four conditions to provide mutual exclusion • No two processes simultaneously in critical region • No assumptions made about speeds or numbers of CPUs • No process running outside its critical region may block another process • No process must wait forever to enter its critical region

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

  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. 2.3.3 Mutual Exclusion with Busy Waiting (2) Strict Alternation Proposed solution to critical region problem (a) Process 0. (b) Process 1. Variable turn is shared by both processes

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

  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. 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. 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. 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; P1 p2 p3 … … … Stmnt1; stmnt2 Seq.down(); Seq.up(); Seq.up(); Seq.down(); … … stmnt3 • Mutual exclusion Semaphore mutex = 1; P1 p2 Mutex.down(); mutex.down(); Critical region; critical region Mutex.up(); mutex.up()

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

  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. 2.3.6 Mutexes Implementation of mutex_lock and mutex_unlock

  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. 2.3.8 Message Passing (2) The producer-consumer problem with N messages

  17. 2.4 Classical IPC Problems2.4.0 The producer and consumer problem

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

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

  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. 2.4.2 The Readers and Writers Problem (2)

More Related