308-203A Introduction to Computing II Lecture 18: Concurrency Issues Fall Session 2000.

Slides:



Advertisements
Similar presentations
Chapter 7 - Resource Access Protocols (Critical Sections) Protocols: No Preemptions During Critical Sections Once a job enters a critical section, it cannot.
Advertisements

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.
Operating Systems Mehdi Naghavi Winter 1385.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Deadlocks Operating Systems (234123) Spring 2013 Deadlocks Dan Tsafrir (13/5/2013) Partially based on slides by Hagit Attiya OS (234123) - spring 2013.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Chapter 6: Process Synchronization
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
SPL/2010 Liveness And Performance 1. SPL/2010 Performance ● Throughput - How much work can your program complete in a given time unit? ● Example: HTTP.
Deadlock CS Introduction to Operating Systems.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Classic Synchronization Problems
Classical Problems of Concurrency
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Process Synchronization
Monitors CSCI 444/544 Operating Systems Fall 2008.
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.
Definitions Process – An executing program
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Operating System Concepts and Techniques Lecture 16 Deadlock and starvation-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques,
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
Synchronization Deadlocks and prevention
Multithreading / Concurrency
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
2.4 Classic IPC Problems Dining philosophers Readers and writers
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Multithreading.
Liveness And Performance
Multithreading.
Multithreading in java.
Threads and Multithreading
CENG334 Introduction to Operating Systems
CSE 542: Operating Systems
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

A Introduction to Computing II Lecture 18: Concurrency Issues Fall Session 2000

From last time We’ve seen how it is often natural to write programs with multiple “threads” running at the same time. This introduced some issues: Race conditions: threads may execute at different speeds Data hazards: threads which share data must “synchronize” accesses to that data to avoid potential data corruption These things happen in an indeterminite, spontaneous and unpredictable way, governed only by Murphy’s Law

Taking Control of Threads We can make this process less haphazard by letting threads control their execution. One way to do this in Java is with the sleep( ) method, which causes the calling thread to stop running for a fixed period of time in msec: e.g. sleep(5000) ; // wake up in 5 seconds

Taking Control of Threads A smoother way to allow other threads to run without specifying a specific time to wait is yield( ). Example two threads running the following code: run( ) { for (int j=0; j < 5; j++) { System.out.println(j); yield( ); }

Taking Control of Threads A smoother way to allow other threads to run without specifying a specific time to wait is yield( ). Example two threads running the following code: run( ) { for (int j=0; j < 5; j++) { System.out.println(j); yield( ); } Most likely output: 1 2 3

Taking Control of Threads Another way to control thread execution is with priorities: public final static int MIN_PRIORITY = 1; public final static int NORM_PRIORITY = 5; public final static int MAX_PRIORITY = 10; The paradigmatic example is a flight-control system with two tasks: Don’t-Crash-Into-The-Mountain task (high priority) Copilot’s-Tetris-Game task (low priority)

Priorities Introduce Issues Q. Should high priority tasks always run over lower priority tasks, or should it be merely preferential?

Priorities Introduce Issues Q. Should high priority tasks always run over lower priority tasks, or should it be merely preferential? A. In Java, priority is strict, and a higher-priority thread cannot even give precedence to lower-priority threads by yield-ing The consequences of a priority policy can sometimes be devastating…

Starvation Let’s say the “Don’t-Crash-Into-The-Mountain” task never sleeps… The “Copilot’s-Tetris-Game” task will never run. Result: It is very important for higher priority tasks to intentionally sleep or otherwise release the processor periodically.

From last time Data hazards were resolved by “mutually exclusive” access to methods labelled as synchronized: Sychronized code is effectively executed as a unit, such that other threads cannot simultaneously enter potentially dangerous “critical sections” simultaneously This also may be dangerous…

The Dining Philosophers A group of philosophers are seated around a table, each with a bowl or rice. Between every two philosophers is one chopstick. Most of the time the philosophers contemplate, but periodically they grow hungry and decide to eat. To eat, a philospher must have both neighboring chopsticks. Only one chopstick may be picked up at a time. Once a philosopher picks up a chopstick, she keeps it until finished eating

The Dining Philosophers

The Problem Suppose each philosopher simultaneously grabs the chopstick to her left… Now everyone has ONE chopstick but no one can eat!

The Dining Philosophers

Possible solutions: Leave one chair empty Pick up both chopsticks simultaneously Have some philosophers always pick up the left chopstick first while others always pick up the right one first

Deadlock This phenomenon is generically refered to as Deadlock It can occur when concurrent threads compete to acquire exclusive resources of various kinds. In general, deadlocks correspond to cycles in wait-graphs, e.g. “A” waits for “B” waits for “C” waits for “A” A B C wait-for

From Dining Philosohers to “Synchronized” Methods Unfortunately, “synchronized” methods can do exactly that: class Foo { synchronized int f( Foo otherFoo ) { return otherFoo.g( ); } synchronized int g( ) { return 0; } }

From Dining Philosohers to “Synchronized” Methods Unfortunately, “synchronized” methods can do exactly that: Let there be: Foo x = new Foo( ) ; Foo y = new Foo( ) ; And two threads which simultaneously do the following: THREAD A: x.f(y) THREAD B:y.f(x)

From Dining Philosohers to “Synchronized” Methods Unfortunately, “synchronized” methods can do exactly that: 1. THREAD A: x.f(y) => Thread A locks x 2. THREAD B:y.f(x) => Thread B locks y 3. THREAD A: We can’t call y.g( ) without the lock for y 4. THREAD B: We can’t call x.g( ) without the lock for x

The Worst of Both Worlds Combining priorities with mutual-exclusion can lead to hard-to-debug phenomena, like the case of the priority inversion bug in the Mars Pathfinder, For details, check out

Any questions?