System Software Assignment 8 Deadlocks

Similar documents
Resource management. Real-Time Systems. Resource management. Resource management

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

OpenMP and more Deadlock 2/16/18

CS4411 Intro. to Operating Systems Exam 1 Fall points 9 pages

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

IV. Process Synchronisation

SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores

Chapter 6. Deadlock. (A Quick Introduction)

Performance Throughput Utilization of system resources

COMP346 Winter Tutorial 4 Synchronization Semaphores

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

C340 Concurrency: Semaphores and Monitors. Goals

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

SFDV3006 Concurrent Programming

College of Computer & Information Science Spring 2010 Northeastern University 26 January 2010

Midterm Exam #2 Solutions October 25, 2016 CS162 Operating Systems

Concurrent Programming TDA381/DIT390

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Models of concurrency & synchronization algorithms

CS 318 Principles of Operating Systems

Concurrency: State Models & Design Patterns

Operating Systems ECE344. Ding Yuan

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Synchronization in Concurrent Programming. Amit Gupta

COMP 346 WINTER Tutorial 5 MONITORS

Last Class: Deadlocks. Today

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks"

CMSC 132: Object-Oriented Programming II

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

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

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Process Synchronization

Question Points Score Total 100

The University of Texas at Arlington

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

SSC - Concurrency and Multi-threading Advanced topics about liveness

What's wrong with Semaphores?

Lecture 9: Midterm Review

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

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

Week 3. Locks & Semaphores

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Threads and Parallelism in Java

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Question Points Score Total 100

Concurrent Programming

Introduction to OS Synchronization MOS 2.3

ECE519 Advanced Operating Systems

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

Chapter 6. Deadlocks

Page 1. Goals for Today" Recap: Monitors" CS162 Operating Systems and Systems Programming Lecture 7. Semaphores, Conditional Variables, Deadlocks"

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

Homework #3. CS 318/418/618, Fall Handout: 10/09/2017

The deadlock problem

CS 31: Intro to Systems Deadlock. Kevin Webb Swarthmore College April 21, 2015

MCS-378 Intraterm Exam 1 Serial #:

CPS 110 Problem Set #1

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Deadlock 1 Chapter 6

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

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

THE UNIVERSITY OF WESTERN AUSTRALIA SAMPLE EXAM QUESTIONS 2007 WITH SOLUTIONS SCHOOL OF COMPUTER SCIENCE CITS3213 CONCURRENT PROGRAMMING (PART II)

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

CSE 153 Design of Operating Systems

Administrivia. Assignments 0 & 1 Class contact for the next two weeks Next week. Following week

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

Deadlock Prevention. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Threads Synchronization

This exam paper contains 8 questions (12 pages) Total 100 points. Please put your official name and NOT your assumed name. First Name: Last Name:

Introduction to Operating Systems

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

Writing Parallel Programs COMP360

Concurrency: Mutual Exclusion and Synchronization

!! What is a deadlock? !! What causes a deadlock? !! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

Dealing with Issues for Interprocess Communication

Resources. Chapter 3. Deadlocks. Resources. Resources

Concurrency Issues. Past lectures: What about coordinated access across multiple objects? Today s lecture:

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

CS 318 Principles of Operating Systems

Concurrency: Mutual Exclusion and Synchronization. Concurrency

Advanced concurrent programming in Java Shared objects

Chapter 6. Deadlock. DM519 Concurrent Programming

COMP 300E Operating Systems Fall Semester 2011 Midterm Examination SAMPLE. Name: Student ID:

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

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

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

CS18000: Programming I

User Space Multithreading. Computer Science, University of Warwick

Threads Questions Important Questions

Spinlocks. Spinlocks. Message Systems, Inc. April 8, 2011

Operating Systems 2010/2011

Transcription:

System Software Assignment 8 Deadlocks Exercise 1: Barrier Objects Create a barrier object in Java, Active Oberon or the language of your choice that is able to synchronize a set of predefined threads or active objects respectively. The barrier object should provide two methods: initialize (e.g., init) and synchronize (e.g., sync). The initialize method is to register a number of threads of a certain group. Calling the synchronize method should lead to a synchronization of the given threads, that means that the statement next to this call in a thread should not be called until all other threads have called synchronize as well. Java: public class Barrier { /** Number of threads in the group */ private static int N; /** Current number of waiting threads */ private static int count; /** Constructor (barrier initialization */ public Barrier(int n) { N = n; count = 0; /** Called to synchronize with the other blocked threads */ public void synchronized sync() { count++; if (count == N) { /* we reached the barrier: release the lock */ notifyall(); count = 0; else { wait(); 1

Active Oberon: MODULE Barriers; TYPE Barrier = OBJECT VAR n: LONGINT; (* Number of threads in the group *) N: LONGINT; (* Current number of waiting threads *) (* Called to synchronize with the other blocked threads *) PROCEDURE Sync*; VAR i: LONGINT; BEGIN {EXCLUSIVE i := n DIV N; INC(n); AWAIT (i < n DIV N) END Enter; (* Constructor (barrier initialization) *) PROCEDURE & Init (nofprocs: LONGINT); BEGIN N := nofprocs; n := 0 END Init; END Barrier; END Barriers. 2

Exercise 2: Deadlocks Assume an electronic bank transfer system with hundreds of identical processes, each of them working as follows: Read an input line specifying the amount of money, the from-account and the to-account. Try to lock both accounts. Transfer the money. Due to the huge amount of processes it might happen that some process having a lock on an account x is not able to get the lock on an account y because the account y is locked by a process waiting for x. Develop a scheme that prevents deadlocks so that no lock has to be released when a second lock cannot be assigned. There are four conditions that can generate a lock: mutual exclusion: two or more processes access a common resource one at a time. hold and wait: processes can get access to resources and wait for some other resources. no resource preemption: a resource cannot be removed from a process. circular wait: there is a cyclic chain of locks. Since we cannot introduce resource preemption we try to make cyclic locks impossible by defining a locking order. One option is to define a global ordering of the account locks (which results in a graph without cycles). 3

Exercise 3: Deadlocks Assume a gorge with a rope spanning from one side to the other so that baboons can pass. Several baboons can cross the gorge at the same time as long as they move in the same direction. If, at some point, there are baboons moving both to the west and some baboons moving to the east, a deadlock occurs (the baboons get stuck). Every baboon willing to cross the gorge has to be sure that no other baboon is crossing the gorge in the opposite direction at that point in time. 1. Describe a solution which prevents deadlocks, using the concept of semaphores (in this first part don t consider starvation) 2. Solve the problem avoiding starvation 4

Solution using semaphores (in pseudocode): Semaphore rope = 1, mutex_east = 1, mutex_west = 1, turnstyle = 1; int east = 0; west = 0; BaboonFromEast: turnstyle.p(); mutex_east.p(); east++; if (east == 1) rope.p(); mutex_east.v(); turnstyle.v(); cross(); mutex_east.p(); east--; if (east == 0) rope.v(); mutex_east.v(); BaboonFromWest: turnstyle.p(); mutex_west.p(); west++; if (west == 1) rope.p(); mutex_west.v(); turnstyle.v(); cross(); mutex_west.p(); west--; if (west == 0) rope.v(); mutex_west.v(); For this example to avoid confusion with the wait Java keyword we use the original Dijkstra notation: V Verhogen: signals the resource is free. The value of a semaphore is the number of units of the resource which are free). P Proberen: waits for a resource (busy-wait). 5

A ticketing system could also solve the problem (something similar to the tickets you get at the post office...) 6