Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1

Similar documents
Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

5 Classical IPC Problems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

Last Class: Synchronization Problems!

PROCESS SYNCHRONIZATION

Programming Languages

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1

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

Process Management And Synchronization

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Process Synchronization

CS370: Operating Systems [Spring 2017] Dept. Of Computer Science, Colorado State University

Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

CS450 OPERATING SYSTEMS FINAL EXAM ANSWER KEY

The deadlock problem

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Principles of Operating Systems

Last Class: Monitors. Real-world Examples

Resources. Lecture 4. Deadlocks. Resources (2) Resources (1) Four Conditions for Deadlock. Introduction to Deadlocks

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

Process Synchronization

OPERATING SYSTEMS. Deadlocks

Deadlock Prevention, Avoidance, and Detection

Process Synchronization. studykorner.org

CS 10: Problem solving via Object Oriented Programming Winter 2017

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

Chapter 6: Process Synchronization

Deadlock CS 241. March 19, University of Illinois

Concurrency: Deadlock and Starvation. Chapter 6

Frequently asked questions from the previous class survey

IV. Process Synchronisation

Concurrency: Deadlock and Starvation. Chapter 6

CS370 Operating Systems

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

More on Synchronization and Deadlock

CS370: Operating Systems [Spring 2016] Dept. Of Computer Science, Colorado State University

CSCE Operating Systems Deadlock. Qiang Zeng, Ph.D. Fall 2018

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Deadlock

Chapter 6: Process Synchronization

Deadlock Risk Management

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

DEADLOCKS M O D E R N O P E R A T I N G S Y S T E M S C H A P T E R 6 S P R I N G

Interprocess Communication By: Kaushik Vaghani

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

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

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

CS370 Operating Systems

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

Interprocess Communication and Synchronization

Principles of Operating Systems

Quiz Answers. CS 537 Lecture 9 Deadlock. What can go wrong? Readers and Writers Monitor Example

Liveness properties. Deadlock

6. Concurrency: Deadlock

Operating Systems and Networks Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Deadlock. Concepts to discuss. A System Model. Deadlock Characterization. Deadlock: Dining-Philosophers Example. Deadlock: Bridge Crossing Example

Operating Systems. Operating Systems Sina Meraji U of T

Chapter 3. Deadlocks

Deadlocks. Today. Next Time. ! Resources & deadlocks! Dealing with deadlocks! Other issues. ! Memory management

Process Synchronization

EECS 482 Introduction to Operating Systems

Chapters 5 and 6 Concurrency

ECS 150 (Operating Systems) Goal To examine what causes deadlock, and what to do about it. Spring Quarter

Deadlocks. Today. Next Time. Resources & deadlocks Dealing with deadlocks Other issues. I/O and file systems

The Deadlock Problem (1)

Consistency: Strict & Sequential. SWE 622, Spring 2017 Distributed Software Engineering

PESIT Bangalore South Campus

CS 153 Design of Operating Systems Winter 2016

Dealing with Issues for Interprocess Communication

CMPT 300 Introduction to Operating Systems

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

CS370 Operating Systems

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem!

1. Motivation (Race Condition)

Concurrency. Chapter 5

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Lesson 6: Process Synchronization

The Deadlock Lecture

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

CS153: Deadlock. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Process Synchronization

Synchronization Principles

Introduction to Operating Systems

Dining Philosophers, Semaphores

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

CSC501 Operating Systems Principles. Process Synchronization

CS450/550 Operating Systems

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

Midterm Exam. October 20th, Thursday NSC

Transcription:

Classical Synchronization Problems 1 1

This lecture Goals: Topics Introduce classical synchronization problems Producer-Consumer Problem Reader-Writer Problem Dining Philosophers Problem Sleeping Barber s Problem 2

3. The Sleeping Barber N customer chairs (waiting chairs) One barber who can cut one customer s hair at any time No waiting customer => barber sleeps Customer enters => If all waiting chairs full, customer leaves Otherwise, if barber asleep, wake up barber and make him work Otherwise, barber is busy wait in a chair 3

Sleeping Barber solution (1) [From Tanenbaum, Modern Operating Systems] 4

Sleeping Barber solution, plus code comments Note: down means sem_wait up means sem_post [From Tanenbaum, Modern Operating Systems] 5

Sleeping Barber solution, plus code comments void customer(void) { if(waiting < CHAIRS) { get_haircut(); } else { // shop full, leave } [From Tanenbaum, Modern Operating Systems] } 6

4. Dining Philosophers: an intellectual game N philosophers and N forks Philosophers eat/think Eating needs 2 forks Pick one fork at a time 8

4. Dining Philosophers Kravets 9

Does this solve the problem? A non-solution to the dining philosophers problem Deadlock: everyone picks up their left fork first, [From Tanenbaum, Modern Operating Systems] then Copyright waits : University for of Illinois right CS 241 Staff fork 10

Necessary and sufficient conditions for deadlock Mutual exclusion: Exclusive use of chopsticks Hold and wait: Hold 1 chopstick, wait for next No preemption: cannot force another to release held resource Circular wait: Each waits for next neighbor to put down chopstick 11

Dining Philosophers solution [From Tanenbaum, Modern Operating Systems] 12

Dining Philosophers solution [From Tanenbaum, Modern Operating Systems] 13

What if... Made picking up both left and right chopsticks an atomic operation? That works (i.e., prevents deadlock) This is essentially what we just did! Or, N philosophers & N+1 chopsticks? That works too! And we ll see another solution later... 14

Summary Classical synchronization problems Producer-Consumer Problem Reader-Writer Problem Sleeping Barber s Problem Dining Philosophers Problem 15

Deadlocks Copyright Copyright : Nahrstedt, : University Angrave, of Illinois Abdelzaher, CS 241 Kravets, Staff Gupta 16 1

Deadlock 17

Deadlock Definition A process is deadlocked if it is waiting for an event that will never occur. Typically, but not necessarily, more than one process will be involved together in a deadlock Is deadlock the same as starvation (i.e., indefinitely postponed)? A process is indefinitely postponed if it is delayed repeatedly over a long period of time while the attention of the system is given to other processes. (Logically the process may proceed but the system never gives it the CPU.) 18

Necessary Conditions for Deadlock Mutual exclusion Processes claim exclusive control of the resources they require Hold-and-wait (a.k.a. wait-for) condition Processes hold resources already allocated to them while waiting for additional resources No preemption condition Resources cannot be removed from the processes holding them until used to completion Circular wait condition A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain 19

Dining Philosophers had it all Mutual exclusion Exclusive use of chopsticks Hold and wait condition Hold 1 chopstick, wait for next No preemption condition Cannot force another to undo their hold Circular wait condition Each waits for next neighbor to put down chopstick 20

Mutual Exclusion Processes claim exclusive control of the resources they require How to break it? Grant non-exclusive access only (e.g., read-only) 21

Hold and Wait Condition Processes hold resources already allocated to them while waiting for additional resources How to break it? Allow processes to either access all its required resources at once, or none of them 22

No Preemption Condition Resources cannot be removed from the processes holding them until used to completion How to break it? Allow processes to be pre-empted and forced to abort themselves or release held resources 23

Circular Wait Condition A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain How to break it? Allow processes to access resources only in increasing order of resource id 24

Resource Allocation Graph Nodes Arcs Processes Resources From resource to process = resource assigned to process From process to resource = process requests (and is waiting for) resource 25

Resource Allocation Graph assign request (a) resource R assigned to process A (b) process B is requesting/waiting for resource S (c) process C and D are in deadlock over resources T and U 26

Dining Philosophers resource allocation graph If we use the trivial broken solution... void philosopher(i) { while true { take left fork; take right fork; eat(); put left fork; put right fork; } Kravets } 27

Dining Philosophers resource allocation graph If we use the trivial broken solution... One node per philosopher and per fork 1. Everyone tries to pick up left fork (request edges) Kravets 28

Dining Philosophers resource allocation graph If we use the trivial broken solution... One node per philosopher and per fork 1. Everyone tries to pick up left fork (request edges) Everyone succeeds! (request edges become assignment edges) Everyone tries to pick up right fork (request edges) Cycle => deadlock 29 Kravets

Summary Definition of deadlock 4 conditions for deadlock to happen How to tell when circular wait condition happens: cycle in resource allocation graph Next time: How to deal with deadlock 30