Synchronization Basic Problem:

Similar documents
Process Coordination

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Prof. Hui Jiang Dept of Computer Science and Engineering York University

Synchronization Classic Problems

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Chapter 7: Process Synchronization. Background

Chapter 6: Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Process Synchronization

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

CS370 Operating Systems

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Lesson 6: Process Synchronization

Process Synchronization(2)

Chapter 6: Process Synchronization

CSE 4/521 Introduction to Operating Systems

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Process Synchronization(2)

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Process Synchronization(2)

Chapter 7: Process Synchronization!

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Classic Problems of Synchronization

Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CS370 Operating Systems

OS Process Synchronization!

CHAPTER 6: PROCESS SYNCHRONIZATION

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Synchronization Principles

Module 6: Process Synchronization

Process Synchronization

Interprocess Communication By: Kaushik Vaghani

Process Synchronization

Process Synchronization

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Process Management And Synchronization

Dealing with Issues for Interprocess Communication

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning

Process Synchronization

Chapter 6: Process Synchronization

CS3733: Operating Systems

Semaphores (by Dijkstra)

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

High-level Synchronization

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Concurrency and Synchronisation

Process Synchronization

Concurrency and Synchronisation

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

CSE Opera,ng System Principles

Introduction to Operating Systems

Process Synchronization

CS 153 Design of Operating Systems Winter 2016

Chapter 5: Process Synchronization

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

9/29/2014. CS341: Operating System Mid Semester Model Solution Uploaded Semaphore ADT: wait(), signal()

Synchronization Principles II

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

CS370 Operating Systems

1. Motivation (Race Condition)

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Lecture 6 (cont.): Semaphores and Monitors

Concept of a process

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Chapter 7 Process Synchronization

Chapter 5: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Co-ordination OPERATING SYSTEMS

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Silberschatz and Galvin Chapter 6

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Process Synchronization. studykorner.org

Module 6: Process Synchronization

$ %! 0,-./ + %/ 0"/ C (" &() + A &B' 7! .+ N!! O8K + 8 N. (Monitors) 3+!

Concurrency pros and cons. Concurrent Programming Problems. Linked list example. Linked list example. Mutual Exclusion. Concurrency is good for users

CSE Traditional Operating Systems deal with typical system software designed to be:

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Multitasking / Multithreading system Supports multiple tasks

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Lecture 5: Inter-process Communication and Synchronization

Synchronization I. Jo, Heeseung

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Process Synchronization Examples

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

CS370 Operating Systems Midterm Review

Transcription:

Synchronization

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. 2

ATM Example Suppose each cash machine transaction is controlled by a separate process, and the withdraw code is: current_balance = get_balance(acct_id) curr_balance = curr_balance - withdraw_amt put_balance(act_id,curr_balance) deliver_bucks(withdraw_amt) Now, suppose that you and your SO share an account. You each to to separate cash machines and withdraw $100 from your balance of $1000. 3

ATM Example 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) context switch context switch What happens? Why does it happen? 4

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. 5

Critical Sections The Too Much Milk or the bank balance problem illustrates the difficulty of coordinating processes Race conditions Deadlock / Livelock Starvation Atomic loads and stores make synchronization difficult (but not impossible) For two processes, the simplest correct solution is asymmetric For three or more processes, the bakery (or post office) algorithm requires auxiliary data structures 6

Criteria for Critical Sections A good solution to the critical section problem would have three properties Mutual exclusion Progress Bounded Waiting Cannot make any assumptions about the relative speeds of processes 7

Hardware Primitives Modern hardware provides better atomic operations than load/store Test-And-Set (TAS) Swap Compare-And-Swap (CAS) Load-Linked & Store-Conditional (LL/SC) 8

Test-And-Set void TAS(int *location) { int oldvalue = *location; The entire function is Atomic *location = 1; return oldvalue; You could implement this on hardware by keeping the bus locked for both a load and a store transaction. Simple primitive Makes programming critical sections easy 9

Critical Sections with TAS While(TAS(&lock) == 1) { /* do nothing */ critical section Lock = 0; Problem: busy-waiting for the entire duration of the critical section 10

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 V(s) (signal) and P(s) (wait). To access a critical section, you must: P(s) ;wait until semaphore is available; also known as wait() <critical section code> V(s) ;signal others to enter; also known as signal() 11

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. 12

Spinlocks typedef struct spinlock { int lock: Spinlock; void acquire(spinlock *s) { while(test_and_set(s->lock) == 1) /* do nothing, or yield */; void release(spinlock *s) { atomicclear(s->lock); Signal and Wait must be atomic 13

Semaphores typedef struct semaphore { int value: ProcessList L; Semaphore; void P(Semaphore *S) { S->value = S->value - 1; if (S.value < 0) { add this process to S.L; block(&s->lock); Signal and Wait must be atomic void V(S) { S->value = S->value + 1; if (S->value <= 0) { remove a process P from S.L; wakeup P 14

Semaphores typedef struct semaphore { int lock; int value: ProcessList L; Semaphore; void P(Semaphore *S) { while(test_and_set(&s->lock) == 1) /* do nothing */; S->value = S->value - 1; if (S.value < 0) { add this process to S.L; atomic_clear_and block(&s->lock); else atomicclear(&s->lock); Signal and Wait must be atomic void V(S) { while(test_and_set(&s->lock) == 1) /* do nothing */; S->value = S->value + 1; if (S->value <= 0) { remove a process P from S.L; wakeup P atomicclear(&s->lock); 15

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 counting semaphore represents a resource with many units available (as indicated by the count to which it is initialized). A counting semaphore lets a process pass as long as more instances are available. 16

Example: Mutual exclusion with Semaphores Semaphore *traysema = semacreate(1); void cook() { while(true) { Burger *burger = makeburger(); P(traysema); placeitemontray(burger); V(traysema); void customer() { while(true) { Burger *burger; P(traysema); burger = grabitemfromtray(burger); V(traysema); 17

Example: Waiting for a condition Semaphore *sema= semacreate(0); void Bob() { while(true) { /* Block until Abe is done with his construction */ P(sema); removecarfromassemblyline(); void Abe() { while(true) { /* Prepare a chassis this will take a while */ preparechassis(); placechassisonassemblyline(); V(sema); 18

Example: Mutual exclusion with Semaphores Semaphore *traysema = semacreate(1); Semaphore *trayfull = semacreate(0); Semaphore *trayempty = semacreate(1); void cook() { while(true) { Burger *burger; P(trayempty); burger = makeburger(); P(traysema); placeitemontray(burger); V(traysema); V(trayfull); void customer() { while(true) { Burger *burger; P(trayfull); P(traysema); burger = grabitemfromtray(burger); V(traysema); V(trayempty); 19

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. 20

Bounded Buffer Sema Implementation var mutex: semaphore = 1 empty: semaphore = n full: semaphore = 0 ;mutual exclusion to shared data ;count of empty buffers (all empty to start) ;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(mutex) ; done with pointers signal(empty) ; note there s an empty buffer <use the item> 21

Dining Philosophers Semaphore *chopsticks[nchopsticks]; Initialize() { for(i=0; I<NCHOPSTICKS; ++I) { chopstick[i] = semacreate(1); Philosopher() { while(true) { P(chopstick[i]); P(chopstick[(i+1) % NCHOPSTICKS]); eat(); V(chopstick[i]); V(chopstick[(i+1) % NCHOPSTICKS]); think(); 22

Dining Philosophers Deadlock! Allow at most N-1 philosophers at the table Pick up chopsticks in a global critical region Odd philosophers pick left, then right, even philosophers pick right, then left, chopstick 23

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? 24

Readers/Writers Sema Implementation var mutex: semaphore wrt: semaphore readcount: integer ; controls access to readcount ; control entry to a writer or first reader ; 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> wait(mutex) ; ensure exclusion readcount = readcount - 1 ; one fewer reader if readcount = 0 then signal(wrt) ; no more readers, allow a writer signal(mutex) 25

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. 26