Ken Birman 1. Refresher: Dekers Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J]

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Module 6: Process Synchronization
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
 Read about Therac-25 at  [  [
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
Secure Operating Systems Lesson 5: Shared Objects.
Critical Sections with lots of Threads. Announcements CS 414 Homework due today.
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 Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
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.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Games Development 2 Concurrent Programming CO3301 Week 9.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CS 3204 Operating Systems Godmar Back Lecture 7. 12/12/2015CS 3204 Fall Announcements Project 1 due on Sep 29, 11:59pm Reading: –Read carefully.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Critical Sections with lots of Threads. Refresher: Deker’s Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true;
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Chapter 6: Process Synchronization
Lecture 22 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Chapter 7: Synchronization Examples
Chapter 6: Process Synchronization
Kernel Synchronization II
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Ken Birman 1

Refresher: Dekers Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J] && turn == J) continue; } CSExit(int i) { inside[i] = false; } 2

Can we generalize to many threads? Obvious approach wont work: Issue: notion of whos turn is next for breaking ties CSEnter(int i) { inside[i] = true; for(J = 0; J < N; J++) while(inside[J] && turn == J) continue; } CSExit(int i) { inside[i] = false; } 3

Bakery idea Think of the (very popular) pastry shop in Montreals Marché Atwater People take a ticket from a machine If nobody is waiting, tickets dont matter When several people are waiting, ticket order determines sequence in which they can place their order 4

Bakery Algorithm: Take 1 int ticket[n]; int next_ticket; CSEnter(int i) { ticket[i] = ++next_ticket; for(k = 0; k < N; k++) while(ticket[k] && ticket[k] < ticket[i]) continue; } Oops… access to next_ticket is a problem! CSExit(int i) { ticket[i] = 0; } 5

Bakery Algorithm: Take 2 int ticket[n]; CSEnter(int i) { ticket[i] = max(ticket[0], … ticket[N-1])+1; for(k = 0; k < N; k++) while(ticket[k] != 0 && ticket[k] < ticket[i]) continue; } CSExit(int i) { ticket[i] = 0; } Clever idea: just add one to the max. Oops… two could pick the same value! 6

Bakery Algorithm: Take 3 If i, k pick same ticket value, ids break tie: (ticket[k] < ticket[i]) || (ticket[k]==ticket[i] && k<i) Notation: (B,J) < (A,i) to simplify the code: (B<A || (B==A && k<i)), e.g.: (ticket[k],k) < (ticket[i],i) 7

Bakery Algorithm: Take 4 int ticket[N]; boolean picking[N] = false; CSEnter(int i) { ticket[i] = max(ticket[0], … ticket[N-1])+1; for(k = 0; k < N; k++) while(ticket[k] && (ticket[k],k) < (ticket[i],i)) continue; } Oops… i could look at k when k is still storing its ticket, and yet k could have the lower ticket number! CSExit(int i) { ticket[i] = 0; } 8

Bakery Algorithm: Almost final int ticket[N]; boolean choosing[N] = false; CSEnter(int i) { choosing[i] = true; ticket[i] = max(ticket[0], … ticket[N-1])+1; choosing[i] = false; for(k = 0; k < N; k++) { while(choosing[k]) continue; while(ticket[k] && (ticket[k],k) < (ticket[i],i)) continue; } CSExit(int i) { ticket[i] = 0; } 9

Bakery Algorithm: Issues? What if we dont know how many threads might be running? The algorithm depends on having an agreed upon value for N Somehow would need a way to adjust N when a thread is created or one goes away Also, technically speaking, ticket can overflow! Solution: Change code so that if ticket is too big, set it back to zero and try again. 10

Eliminating overflow do { ticket[i] = 0; choosing[i] = true; ticket[i] = max(ticket[0], … ticket[N-1])+1; choosing[i] = false; } while(ticket[i] >= MAXIMUM); 11

Adjusting N This wont happen often Simplest: brute force! Disable threading temporarily Then change N, reallocate array of tickets, initialize to 0 Then restart the threads package Sometimes a crude solution is the best way to go… 12

Bakery Algorithm: Final int ticket[N]; /* Important: Disable thread scheduling when changing N */ boolean choosing[N] = false; CSEnter(int i) { do { ticket[i] = 0; choosing[i] = true; ticket[i] = max(ticket[0], … ticket[N-1])+1; choosing[i] = false; } while(ticket[i] >= MAXIMUM); for(k = 0; k < N; k++) { while(choosing[k]) continue; while(ticket[k] && (ticket[k],k) < (ticket[i],i)) continue; } CSExit(int i) { ticket[i] = 0; } 13

Bakery Algorithm is really theory... A lesson in thinking about concurrency 14

Synchronization in real systems Few real systems actually use algorithms such as the bakery algorithm In fact we learned because it helps us think about synchronization in a clear way Real systems avoid that style of busy waiting although, with multicore machines, it may be coming back 15

Critical Sections with Hardware Hardware (multicore) platforms demand some kind of synchronization down in the O/S kernel Needs to map directly to machine instructions Usually exploits some form of test and set instruction This kind of instruction is also available in user code, but user-level applications would rarely employ it In applications users build, there is usually some kind of language-level support for synchronization 16

Critical Sections with Atomic Hardware Primitives Process i While(test_and_set(&lock)); Critical Section lock = false; Share: int lock; Initialize: lock = false; Problem: Does not satisfy liveness (bounded waiting) (see book for correct solution) Assumes that test_and_set is compiled to a special hardware instruction that sets the lock and returns the OLD value (true: locked; false: unlocked) 17

Higher level constructs Even with special instructions available, many O/S designers prefer to implement a synchronization abstraction using the special instructions Why? Makes the O/S more portable (not all machines use the same set of instructions) Helps us think about synchronization in higer-level terms, rather than needing to think about hardware 18

Mutex variables A special kind of variable Mutex x = new Mutex(); Implemented as a semaphore initialized to 1 (next slide) Two operations: x.acquire() wait until x > 0, then set x = x-1 and continue x.release() x = x+1 stmts performs statements atomically 19

Semaphores In Java, a semaphore is a form of Mutex initialized to some integer value greater than 1 Semaphore max_readers = new Semaphore(3); … max_reader.acquire();// counts down, then blocks … max_reader.release(); 20

Side remark Dijkstra was first to introduce semaphores with operations P(x) – passeren V(x) – verhogan Book calls them x.wait() X.signal() Were focusing on Java because you are more likely to use Java in your career 21

Definition: atomically Means this code must (somehow) execute without interruptions O/S implementer would need to find a way to implement the atomic portion Perhaps using special instructions Perhaps by disabling interrupts (if there is just one core) Perhaps some other tricky scheme… Idea is to separate the behavior required from the best way of supporting that behavior on a particular CPU 22

Mutex and Critical Sections Mutex mutex; CSEnter() { mutex.acquire();} CSExit() { mutex.release(); } 23

Attempt In Java, you can attempt to acquire a mutex or semaphore With no timeout, either your attempt succeeds, or it throws an exception There is also a timer variation, where you can specify an amount of time your code is willing to wait This is used to avoid getting stuck waiting forever, in complex programs where many people implemented different parts of the code 24

Java also has synchronized Under the covers, the real Java synchronization mechanism is a kind of built-in lock on objects public synchronized void myProcedure( …) Can also synchronize on a variable public synchronized(x) void myProcedure( …) Or even use as a statement synchronized(x) { …. code …. } 25

But synchronized is tricky… Based on slide by Felber, who based his on one by Grossman void deposit(…) { synchronized(this) { … } } void withdraw(…) { synchronized(this) { … } } int balance(…) { synchronized(this) { … } } void transfer(account from, int amount) { if (from.balance() >= amount) { from.withdraw(amount); this.deposit(amount); } No concurrency control: race! 26

void deposit(…) { synchronized(this) { … } } void withdraw(…) { synchronized(this) { … } } int balance(…) { synchronized(this) { … } } void transfer(account from, int amount) { synchronized(this) { if (from.balance() >= amount) { from.withdraw(amount); this.deposit(amount); } Race! But synchronized is tricky… Based on slide by Felber, who based his on one by Grossman 27

void deposit(…) { synchronized(this) { … } } void withdraw(…) { synchronized(this) { … } } int balance(…) { synchronized(this) { … } } void transfer(account from, int amount) { synchronized(this) { synchronized(from) { if (from.balance() >= amount) { from.withdraw(amount); this.deposit(amount); } Deadlock! But synchronized is tricky… Based on slide by Felber, who based his on one by Grossman 28

Yet additional options Every Java object also has built-in methods: Obj.wait(): not semaphores! Calling thread always blocks Obj.notify(): wakes up one blocked thread (FIFO) Obj.notifyAll(): wakes up all blocked threads These are used to support monitors (next lecture) 29

Main take away Java has many options for locking things Mutex (binary semaphores): like locks General semaphores Synchronized classes Object.wait/notify Too many choices! What we really need to understand is how to use these to obtain correct solutions… 30