6: Process Synchonization II 1 PROCESS SYNCHRONIZATION II THE BOUNDED BUFFER ( PRODUCER / CONSUMER ) PROBLEM: This is the same producer / consumer problem.

Slides:



Advertisements
Similar presentations
Module 6: Process Synchronization
Advertisements

Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Chapter 6 Process Synchronization: Part 2. Problems with Semaphores Correct use of semaphore operations may not be easy: –Suppose semaphore variable called.
Classical Problems of Concurrency
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Quick Review of May 1 material Concurrent Execution and Serializability –inconsistent concurrent schedules –transaction conflicts serializable == conflict.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6: Process Synchronization.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Critical Regions § 7.6 Although semaphores provide a convenient and effective mechanism for process synchronization, their incorrect use can still result.
Process Synchronization
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Modified from Silberschatz, Galvin and Gagne & Stallings Lecture 12 Chapter 6: Process Synchronization (cont)
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Solution to Dining Philosophers. Each philosopher I invokes the operations pickup() and putdown() in the following sequence: dp.pickup(i) EAT dp.putdown(i)
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
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 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
7c.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Module 7c: Atomicity Atomic Transactions Log-based Recovery Checkpoints Concurrent.
Process Synchronization Monitors Simulation of Monitors Checkpoint and Recovery Monitors Simulation of Monitors Checkpoint and Recovery Topics:
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Synchronization. 6.2Operating System Principles Module 6: Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 6 Operating Systems.
Chapter 6: Process Synchronization. 6.2 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization.
Chapter 6: Process Synchronization. 6.2CSCI 380 – Operating Systems Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s.
Fall 2000M.B. Ibáñez Lecture 08 High Level mechanisms for process synchronization Critical Regions Monitors.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Module 6: Process Synchronization
Chapter 6: Process Synchronization
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 6-7: Process Synchronization
Advanced Operating Systems - Fall 2009 Lecture 8 – Wednesday February 4, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours: M,
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Lecture 25 Syed Mansoor Sarwar
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Chapter 6: Process Synchronization
Synchronization Hank Levy 1.
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Module 6: Process Synchronization
Chapter 6: Process Synchronization
Synchronization Hank Levy 1.
Chapter 6: Synchronization
Process Synchronization
Chapter 6: Process Synchronization
CSE 542: Operating Systems
CSE 542: Operating Systems
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Presentation transcript:

6: Process Synchonization II 1 PROCESS SYNCHRONIZATION II THE BOUNDED BUFFER ( PRODUCER / CONSUMER ) PROBLEM: This is the same producer / consumer problem as before. But now we'll do it with signals and waits. Remember: a wait decreases its argument and a signal increases its argument. INITIALIZE: mutex = 1; empty = n; full = 0; SOME INTERESTING PROBLEMS producer: repeat /* produce an item in nextp */ wait (empty); /* Do action */ wait (mutex); /* Buffer guard*/ /* add nextp to buffer */ signal (mutex); signal (full); until ( false ); consumer: repeat wait (full); wait (mutex); /* remove an item from buffer to nextc */ signal (mutex); signal (empty); /* consume an item in nextc */ until ( false );

6: Process Synchonization II 2 PROCESS SYNCHRONIZATION THE READERS/WRITERS PROBLEM: This is the same as the Producer / Consumer problem except - we now can have many concurrent readers and one exclusive writer. Locks: are shared (for the readers) and exclusive (for the writer). Two possible ( contradictory ) guidelines can be used: No reader is kept waiting unless a writer holds the lock (the readers have precedence). If a writer is waiting for access, no new reader gains access (writer has precedence). ( NOTE: starvation can occur on either of these rules if they are followed rigorously.) Some Interesting Problems

6: Process Synchonization II 3 THE READERS/WRITERS PROBLEM: varmutex, wrt:semaphore; readcount:integer; Reader: repeat wait( mutex ); /* Allow 1 reader in entry*/ readcount = readcount + 1; if readcount == 1 then wait(wrt ); /* 1st reader locks writer */ signal( mutex ); /* reading is performed */ wait( mutex ); readcount = readcount - 1; if readcount == 0 then signal(wrt ); /*last reader frees writer */ signal( mutex ); until( false ); Writer: repeat wait( wrt ); /* writing is performed */ signal( wrt ); until( false ); PROCESS SYNCHRONIZATION Some Interesting Problems

6: Process Synchonization II 4 THE DINING PHILOSOPHERS PROBLEM: 5 philosophers with 5 chopsticks sit around a circular table. They each want to eat at random times and must pick up the chopsticks on their right and on their left. Clearly deadlock is rampant ( and starvation possible.) PROCESS SYNCHRONIZATION Some Interesting Problems Several solutions are possible: Allow only 4 philosophers to be hungry at a time. Allow pickup only if both chopsticks are available. ( Done in critical section ) Odd # philosopher always picks up left chopstick 1st, even # philosopher always picks up right chopstick 1 st.

6: Process Synchonization II 5 High Level synchronization construct implemented in a programming language. A shared variable v of type T, is declared as: var v; shared T Variable v is accessed only inside a statement: region v when B do S where B is a Boolean expression. While statement S is being executed, no other process can access variable v. Regions referring to the same shared variable exclude each other in time. When a process tries to execute the region statement, the Boolean expression B is evaluated. If B is true, statement S is executed. If it is false, the process is delayed until B is true and no other process is in the region associated with v. PROCESS SYNCHRONIZATION Critical Regions Critical Region Entry Section Shared Data Exit Section

6: Process Synchonization II 6 EXAMPLE: Bounded Buffer: Shared variables declared as: varbuffer: shared record pool: array [ 0.. n - 1] of item; count, in, out: integer; end; Producer process inserts nextp into the shared buffer: region buffer when count < n do begin pool[in] = nextp; in = in + 1 mod n; count = count + 1; end; Consumer process removes an item from the shared buffer and puts it in nextc. region buffer when count > n do begin nextc = pool[out]; out = out + 1 mod n; count = count - 1; end; PROCESS SYNCHRONIZATION Critical Regions

6: Process Synchonization II 7 The book discusses Solaris 2 as an example, but other operating systems work this way as well. Spin locks are used around critical sections that should be held only a short time. This is determined by: a) Is the lock holder currently running? b) Have we already spun for a while? c) Spin for some time and then cause reschedule. (This is very common because it’s deterministic.) Long held locks (those held across a process reschedule or during a disk access) always cause a reschedule / sleep. PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 8 Atomic Transactions Transaction – a program unit that must be executed atomically; that is, either all the operations associated with it are executed to completion, or none are performed. Must preserve atomicity despite possibility of failure. We are concerned here with ensuring transaction atomicity in an environment where failures result in the loss of information on volatile storage. We will look at several common uses of atomic transactions – situations where atomicity is required. PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 9 Log-Based Recovery Write-ahead log - all updates are recorded on the log, which is kept in stable storage; log has following fields: 1.Transaction name 2.Data item name; old value; new value The log has a record of, and either if the transactions commits, or if the transaction aborts. Recovery algorithm uses two procedures: undo( Ti ) - restores value of all data updated by transaction Ti to the old values. It is invoked if the log contains record, but not. redo( Ti ) - sets value of all data updated by transaction Ti to the new values. It is invoked if the log contains both and. PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 10 Checkpoints - reduce recovery overhead Output all log records currently residing in volatile storage onto stable storage. Output all modified data residing in volatile storage to stable storage. Output log record onto stable storage. Recovery routine examines log to determine the most recent transaction Ti that started executing before the most recent checkpoint took place. Search log backward for first record. Find subsequent record. redo and undo operations need to be applied to only transaction Ti and all transactions Tj that started executing after transaction Ti. PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 11 Concurrent Atomic Transactions Serial schedule - the transactions are executed sequentially in some order. Example of a serial schedule in which T0 is followed by T1 : T0|T read( A )| write( A )| read( B )| write( B )| |read( A ) |write( A ) |read( B ) |write( B ) Conflicting operations - Oi and Oj conflict if they access the same data item, and at least one of these operations is a write operation. PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 12 Conflict serializable schedule - schedule that can be transformed into a serial schedule by a series of swaps of nonconflicting operations. Example of a concurrent serializable schedule: T0|T | read( A )| write( A )| |read( A ) |write( A ) read( B )| write( B )| |read( B ) |write( B ) PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 13 Locking protocol governs how locks are acquired and released; data item can be locked in following modes: Shared: If Ti has obtained a shared-mode lock on data item Q, then Ti can read this item, but it cannot write Q. Exclusive: If Ti has obtained an exclusive- mode lock on data item Q, then Ti can both read and w rite Q. PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 14 Two-phase locking protocol Growing phase: A transaction may obtain locks, but may not release any lock. Shrinking phase: A transaction may release locks, but may not obtain any new locks. The two-phase locking protocol ensures conflict serializability, but does not ensure freedom from deadlock. Timestamp-ordering scheme - transaction ordering protocol for determining serializability order. With each transaction Ti in the system, associate a unique fixed timestamp, denoted by TS( Ti ). If Ti has been assigned timestamp TS( Ti ), and a new transaction Tj enters the system, then TS( Ti ) < TS( Tj ). PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 15 Implement by assigning two timestamp values to each data item Q. W-timestamp (Q) - denotes largest timestamp of any transaction that executed write (Q) successfully. R-timestamp (Q) - denotes largest timestamp of any transaction that executed read (Q) successfully. Example of a schedule possible under the timestamp protocol: T2|T read( B )| |read( B ) |write( B ) read( A )| |read( A ) |write( A ) PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 16 There are schedules that are possible under the two-phase locking protocol but are not possible under the timestamp protocol, and vice versa. The timestamp-ordering protocol ensures conflict serializability; conflicting operations are processed in timestamp order. PROCESS SYNCHRONIZATION How Is This Really Used?

6: Process Synchonization II 17 Synchronization IS used in real life. Generally programmers don’t use the really primitive hardware locks, but use higher level mechanisms as we’ve demonstrated. PROCESS SYNCHRONIZATION Wrap Up