Synchronization Hank Levy 1.

Slides:



Advertisements
Similar presentations
Ch 7 B.
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
Classical Problems of Concurrency
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Synchronization April 14, 2000 Instructor Gary Kimura Slides courtesy of Hank Levy.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
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.
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 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.
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.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Previous Lecture Review n Concurrently executing threads often share data structures. n If multiple threads are allowed to access shared data structures.
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.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
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.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
IT 344: Operating Systems Winter 2008 Module 7 Semaphores and Monitors
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
CSE Operating System Principles Synchronization.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
1 CSE451 Basic Synchronization Autumn 2002 Gary Kimura Lecture #7 & 8 October 14 & 16, 2002.
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.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 6: Process Synchronization
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Chapter 6-7: Process Synchronization
CSE451 Basic Synchronization Spring 2001
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
Lecture 2 Part 2 Process Synchronization
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 19
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Presentation transcript:

Synchronization Hank Levy 1

Synchronization Basic Problem: If two concurrent processes are accessing a shared variable, and that variable is read, modified, and written by those processes, then the variable must be controlled to avoid erroneous behavior.

ATM Example Suppose each cash machine transaction is controlled by a separate process, and the withdraw code is: cur_balance=get_balance (acct_ID) withdraw_amt=read_amount_from_ATM() if withdraw_amt>curr_balance then error curr_balance=curr_balance - withdraw_amt put_balance (act_ID,curr_balance) deliver_bucks(withdraw_amt) Now, suppose that you and your s.o. share an account. You each to to separate cash machines and withdraw $100 from your balance of $1000.

ATM Example What happens? Why does it happen? you: curr_balance=get_balance(acct_ID) you: withdraw_amt=read_amount() you: curr_balance=curr_balance-withdraw_amt so: curr_balance=get_balance(acct_ID) so: withdraw_amt=read-amount() so: curr_balance=curr_balance-withdraw_amt so: put_balance(acct_ID,curr_balance) so: deliver_bucks(withdraw_amt) you: put_balance(acct_ID,curr_balance) you: deliver_bucks(withdraw_amt) What happens? Why does it happen? context switch context switch

Problems A problem exists because a shared data item (curr_balance) was accessed without control by processes that read, modified, and then rewrote that data. We need ways to control access to shared variables.

Semaphores Dijkstra, in the THE system, defined a type of variable and two synchronization operations that can be used to control access to critical sections. A semaphore is a variable that is manipulated atomically through operations signal(s) and wait(s). To access a critical section, you must: wait(s) ;wait until semaphore is available <critical section code> signal(s) ;signal others to enter

Semaphores Associated with each semaphore is a queue of waiting processes. If you execute wait(s) and the semaphore is free, you continue; if not, you block on the waiting queue. A signal(s) unblocks a process if it’s waiting.

Signal type semaphore = record value: integer: L: list of processes; end wait(S): S.value = S.value - 1; if S.value < 0 then begin add this process to S.L; block; end; signal(S): S.value = S.value + 1; if S.value <= 0 remove a process P from S.L; wakeup P Signal and Wait must be atomic

Example: Readers/Writers Problem Basic Problem: an object is shared among several processes, some which only read it, and some which write it. We can allow multiple readers at a time, but only one writer at a time. How do we control access to the object to permit this protocol?

Readers/Writers Sema Implementation var mutex: semaphore ; controls access to readcount wrt: semaphore ; control entry to a writer or first reader readcount: integer ; number of readers write process: wait(wrt) ; any writers or readers? <perform write operation> signal(wrt) ; allow others read process: wait(mutex) ; ensure exclusion readcount = readcount + 1 ; one more reader if readcount = 1 then wait(wrt) ; if we’re the first, synch with writers signal(mutex) <perform reading> readcount = readcount - 1 ; one fewer reader if readcount = 0 then signal(wrt) ; no more readers, allow a writer

Readers/Writers Impl. Notes Note that: 1. The first reader blocks if there is a writer; any other readers who try to enter will then block on mutex. 2. Once a writer exists, all readers will fall through. 3. The last reader to exit signals a waiting writer. 4. When a writer exits, if there is both a reader and writer waiting, which goes next depends on the scheduler.

Semaphore Types In general, there are two types of semaphores: a mutex semaphore guarantees mutually exclusive access to a resource (only one entry). The mutex sema is initialized to 1. A counted semaphore represents a resource with many unites available (as indicated by the count to which it is initialized). A counted semaphore lets a process pass as long as more instances are available.

Example: Bounded Buffer Problem The Problem: There is a buffer shared by producer processes, which insert into it, and consumer processes, which remove from it. The processes are concurrent, so we must control their access to the (shared) variables that describe the state of the buffer.

Bounded Buffer Sema Implementation var mutex: semaphore = 1 ;mutual exclusion to shared data empty: semaphore = n ;count of empty buffers (all empty to start) full: semaphore = 0 ;count of full buffers (none full to start) producer: wait(empty) ; one fewer buffer, block if none available wait(mutex) ; get access to pointers <add item to buffer> signal(mutex) ; done with pointers signal(full) ; note one more full buffer consumer: wait(full) ;wait until there’s a full buffer wait(mutex) ;get access to pointers <remove item from buffer> signal(empty) ; note there’s an empty buffer <use the item>

Busy Waiting with Test-and-Set A test-and-set instruction provides the atomic guarantees needed to implement critical sections Boolean Function test-and-set (var target: boolean) begin test-and-set := target ; return old value of target target := true ; set target to true end using test-and-set(initialize aLock:= false at beginning) while test-and-set(aLock)do no-op ; spin until lock is free (and atomically ; acquire the lock) <do the critical section> aLock := false ; free the lock for someone else