Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.

Slides:



Advertisements
Similar presentations
Module 6: Process Synchronization
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Silberschatz, Galvin and Gagne  Operating System Concepts Background Shared-memory solution to bounded-buffer problem allows at most n – 1 items.
The Critical-Section Problem
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
6: Process Synchronization 1 1 PROCESS SYNCHRONIZATION I This is about getting processes to coordinate with each other. How do processes work with resources.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Chapter 2.3 : Interprocess Communication
Process Synchronization
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
6: Process Synchonization II 1 PROCESS SYNCHRONIZATION II THE BOUNDED BUFFER ( PRODUCER / CONSUMER ) PROBLEM: This is the same producer / consumer problem.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating System I Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Concurrency, Mutual Exclusion and Synchronization.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
OSes: 5. Synch 1 Operating Systems v Objectives –describe the synchronization problem and some common mechanisms for solving it Certificate Program in.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
Interprocess Communication Race Conditions
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Chapter 7: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Introduction to Cooperating Processes
Module 7a: Classic Synchronization
Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6

cs431-cotter2 Interprocess Communications Passing information between processes Used to coordinate process activity May result in data inconsistency if mechanisms are not in place to ensure orderly execution of the processes. The problem can be identified as a race condition

cs431-cotter3 Producer / Consumer Problem Buffer Producer Consumer

cs431-cotter4 Producer / Consumer Problem Buffer Producer Consumer

cs431-cotter5 Producer / Consumer Problem Shared data: int counter, in, out item buffer[n];

cs431-cotter6 Producer / Consumer Problem Shared data: int counter, in, out item buffer[n]; Producer: repeat... produce an item in nextp... while counter = n do noop; buffer[in] := nextp; in := (in + 1) mod n; counter := counter + 1; until false;

cs431-cotter7 Producer / Consumer Problem Shared data: int counter, in, out item buffer[n]; Producer: repeat... produce an item in nextp... while counter = n do noop; buffer[in] := nextp; in := (in + 1) mod n; counter := counter + 1; until false; Consumer: repeat... while counter = 0 do noop; nextc := buffer[out]; out := (out + 1) mod n; counter := counter -1;... consume the item in nextc... until false;

cs431-cotter8 Producer / Consumer Problem Shared data: int counter, in, out item buffer[n]; Producer: repeat... produce an item in nextp... while counter = n do noop; buffer[in] := nextp; in := (in + 1) mod n; counter := counter + 1; until false; Consumer: repeat... while counter = 0 do noop; nextc := buffer[out]; out := (out + 1) mod n; counter := counter -1;... consume the item in nextc... until false; The Problem: atomic execution of counter changes

cs431-cotter9 Figure Two processes want to access shared memory at the same time. Race Conditions Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter10 The Critical Section / Region Problem Occurs in systems where multiple processes all compete for the use of shared data. Each process includes a section of code (the critical section) where it accesses this shared data. The problem is to ensure that only one process at a time is allowed to be operating in its critical section.

cs431-cotter11 The Critical Section / Region Problem Occurs in systems where multiple processes all compete for the use of shared data. Each process includes a section of code (critical section) where it accesses this shared data. The problem is to ensure that only one process at a time is allowed to be operating in its critical section. repeat entry_to_section critical section exit_section remainder of program until false;

cs431-cotter12 Criteria for Critical Region 1.Mutual Exclusion If one process is in its critical section, no other process may be in its respective section 2.Progress If no process is executing in its critical section and there exists some process that needs to enter its critical section, then the selection of that process cannot be delayed indefinitely 3.Bounded Waiting There is a bound on the number of times that a waiting process can be superceded

cs431-cotter13 Conditions required to avoid race condition: No two processes may be simultaneously inside their critical regions. No assumptions may be made about speeds or the number of CPUs. No process running outside its critical region may block other processes. No process should have to wait forever to enter its critical region. Critical Regions (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter14 Figure Mutual exclusion using critical regions. Critical Regions (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter15 Proposals for achieving mutual exclusion: Disabling interrupts Lock variables Strict alternation Peterson's solution The TSL instruction Mutual Exclusion with Busy Waiting Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter16 Figure A proposed solution to the critical region problem. (a) Process 0. (b) Process 1. In both cases, be sure to note the semicolons terminating the while statements. Strict Alternation Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter17 Second Approach Shared variables: int flag[2]; // if (flag[i]==true), P i ready to enter its CS initially flag[0] := flag[1] := false; Process P i repeat flag[i] := true; while (flag[j] = true), do noop; critical section flag[i] := false; remainder of program until false;

cs431-cotter18 Figure Peterson’s solution for achieving mutual exclusion. Peterson's Solution Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter19 Multiple Process Solution The Bakery Algorithm Shared variables: int choosing[n], number [n]; Process P i repeat choosing[i] := true; number[i] := max (number[...]) +1; choosing[i] := false; for (j := 0..n-1) while (choosing[j]:=true:) noop; while (number[j] && number[j],j < number[i],i) noop; critical section number[i] := 0; remainder of program until false;

cs431-cotter20 Figure Entering and leaving a critical region using the TSL instruction. The TSL Instruction (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter21 Figure Entering and leaving a critical region using the XCHG instruction. The TSL Instruction (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter22 Figure The producer-consumer problem with a fatal race condition. The Producer-Consumer Problem Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter23Semaphores Need to generalize critical section problems Need to ensure ATOMIC access to shared variables. Semaphore provides an integer variable that is only accessible through semaphore operations:

cs431-cotter24Semaphores Need to generalize critical section problems Need to ensure ATOMIC access to shared variables. Semaphore provides an integer variable that is only accessable through semaphore operations: WaitP while (s < 0) ; // empty while loop s--; SignalV s++;

cs431-cotter25 Figure The producer-consumer problem using semaphores. Semaphores Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter26 Sync with Semaphore Process 1 : A signal (sync); : Process 2 wait (sync); B :

cs431-cotter27 Counting Semaphore Implementation struct semaphore { intvalue; intL[size]; } s ; Assumes 2 internal operations: block; wakeup(p);

cs431-cotter28 Counting Semaphore Implementation wait(s) s.value--; if (s.value <0) add to s.L block; signal(s) s.value++; if (s.value <= 0) remove P from s.L; wakeup(P )

cs431-cotter29 Semaphore Deadlock P wait(B); wait(A); : signal(B); signal(A); Q wait(A); wait(B); : signal(A); signal(B);

cs431-cotter30 Figure Implementation of mutex lock and mutex unlock. Mutexes Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter31 “Critical Regions” Objective of semaphores (etc.) is to ensure that a critical region of code is used exclusively. Misuse can defeat the objective. One solution is to define a higher level object that specifically creates the critical region

cs431-cotter32 “Critical Regions” Objective of semaphores (etc.) is to ensure that a critical region of code is used exclusively. Misuse can defeat the objective. One solution is to define a higher level object that specifically creates the critical region region V when B do S; The region V is a critical region. When a given boolean expression B is true, allow an operation S that operates on the region to take place. However, ensure that only one process is in V at a time.

cs431-cotter33 “Critical Region” var buffer: shared record pool: array [0..n-1] of item; count, in, out: integer; end;

cs431-cotter34 “Critical Region” var buffer: shared record pool: array [0..n-1]of item; count, in, out: integer; end; Producer: region buffer when count < n do begin pool[in]:= nextp; in :=( in + 1) mod n; count := count + 1; end; Consumer: region buffer when count > 0 do begin pool[out]:= nextp; out :=( out + 1) mod n; count := count - 1; end;

cs431-cotter35Monitors Another high level sync construct Programmer defined operators. Local data accessible only through monitor procedures. Only 1 process can be executing in the monitor at a time. (Many may be queued up at procedures)

cs431-cotter36Monitors Another high level sync construct Programmer defined operators. Local data accessible only through monitor procedures. Only 1 process can be executing in the monitor at a time. C1... C2... C3... C4... Input... local data Procedure 1 Procedure n ::::

cs431-cotter37 Figure An outline of the producer-consumer problem with monitors. Monitors Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter38 Classical IPC Problems Dining Philosophers Readers and Writers Bakery Problem

cs431-cotter39 Figure Lunch time in the Philosophy Department. Dining Philosophers Problem (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter40 Figure A nonsolution to the dining philosophers problem. Dining Philosophers Problem (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter41 Figure A solution to the dining philosophers problem. Dining Philosophers Problem (3) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter42 Figure A solution to the dining philosophers problem. Dining Philosophers Problem (4) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter43 Figure A solution to the dining philosophers problem. Dining Philosophers Problem (5) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter44 Figure A solution to the readers and writers problem. The Readers and Writers Problem (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter45 Figure A solution to the readers and writers problem. The Readers and Writers Problem (2) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

cs431-cotter46 Atomic Transactions Database origins Transaction: A collection of instructions that performs a single logical function. Many operations need to be atomic. Either fully committed, or fully removed. Examples: Bank account transactions database updates.

cs431-cotter47Transactions Begin transaction do reads and writes if (transaction successful) commit transaction; else abort transaction; How do we determine when we need to commit or abort transaction??

cs431-cotter48 Computer Storage Media Volatile Storage Subject to data loss in the event of power failure. (main memory) Non-volatile Storage Resistant to power failure, system failures. (hard disk, tape) Stable Storage - Data "never" lost (combination of non-volatile elements)

cs431-cotter49 Transaction Log based recovery Log entry to record each event transaction name (number?) data item name old value new value (when write)

cs431-cotter50 Transaction Log based recovery Log entry to record each event transaction name (number?) data item name old value new value (when write) Entries are idempotent

cs431-cotter51 Transaction Log based recovery undo command start, but no commit redo command start and commit Checkpoints save log records save data items

cs431-cotter52 Concurrent Atomic Transactions Need to ensure that any interactions between transaction elements do not affect end results. Serializability

cs431-cotter53 T 0 T 1 R(A) W(A) R(B) W(B) R(A) W(A) R(B) W(B) Concurrent Atomic Transactions Need to ensure that any interactions between transaction elements do not affect end results. Serializability

cs431-cotter54 T 0 T 1 R(A) W(A) R(B) W(B) R(A) W(A) R(B) W(B) Concurrent Atomic Transactions Need to ensure that any interactions between transaction elements do not affect end results. Serializability T 0 T 1 R(A) W(A) R(A) W(A) R(B) W(B) R(B) W(B)

cs431-cotter55 Concurrent Atomic Transactions Serializability T 0 T 1 R(A) W(A) R(A) R(B) W(A) W(B) R(B) W(B) Step 1

cs431-cotter56 Concurrent Atomic Transactions Serializability T 0 T 1 R(A) W(A) R(A) R(B) W(A) W(B) R(B) W(B) T 0 T 1 R(A) W(A) R(B) R(A) W(A) W(B) R(B) W(B) Step 1 Step 2

cs431-cotter57 Concurrent Atomic Transactions Serializability T 0 T 1 R(A) W(A) R(A) R(B) W(A) W(B) R(B) W(B) T 0 T 1 R(A) W(A) R(B) R(A) W(A) W(B) R(B) W(B) T 0 T 1 R(A) W(A) R(B) R(A) W(B) W(A) R(B) W(B) Step 1 Step 2 Step 3

cs431-cotter58 T 0 T 1 R(A) W(A) R(B) W(B) R(B) W(B) R(A) W(A) Concurrent Atomic Transactions Need to ensure that any interactions between transaction elements do not affect end results. Not Serializable! T 0 T 1 R(A) W(A) R(B) W(B) R(B) W(B) R(A) W(A)

cs431-cotter59 Concurrent Transactions Locking Protocol Shared Locks (read but not write) Exclusive Locks (read or write) A transaction may require a number of locks that must be applied consistently (same sequence..)

cs431-cotter60 Concurrent Transactions Locking Protocol Shared Locks (read but not write) Exclusive Locks (read or write) A transaction may require a number of locks that must be applied consistently (same sequence..) 2 Phase locking protocol Growing Phase (obtain but not release locks) Shrinking Phase (release but not obtain locks)

cs431-cotter61Summary Main issue with interprocess communications is the need to synchronize access to shared data Many techniques available: Critical Section, locking techniques, Bakery Algorithm Semaphore, Mutex, Monitor Message passing Classical IPC Problems Transactions

cs431-cotter62Questions Why is it necessary to synchronize processes? Give an example that demonstrates the need for process synchronization. What is a "critical section"? How is it used in synchronizing the operations of more than 1 process? How does the Bakery Algorithm solve the problem of synchronizing multiple processes? What is the difference between a semaphore and a mutex? What is an atomic transaction? What is a serial schedule? How does it differ from a serializable schedule?

Questions Discuss how the Baker's algorithm ensures that each process that tries to get access to a critical section will eventually be guaranteed of getting in.