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

Similar documents
CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

More on Synchronization and Deadlock

CS 318 Principles of Operating Systems

CS 450 Exam 2 Mon. 4/11/2016

Concept of a process

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

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

C09: Process Synchronization

Resource management. Real-Time Systems. Resource management. Resource management

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

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

semaphores Vaibhav Bajpai

Chapter 6: Process Synchronization

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

System Software Assignment 8 Deadlocks

Chapters 5 and 6 Concurrency

Concurrency: a crash course

Concurrency: Deadlock and Starvation. Chapter 6

Part II Process Management Chapter 6: Process Synchronization

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Concurrent & Distributed Systems Supervision Exercises

Synchronization for Concurrent Tasks

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

CONCURRENCY: DEADLOCK. Shivaram Venkataraman CS 537, Spring 2019

PROCESS SYNCHRONIZATION

More Types of Synchronization 11/29/16

The University of Texas at Arlington

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

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

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

The University of Texas at Arlington

Programming Languages

Last Class: Deadlocks. Today

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

University of Waterloo Midterm Examination Model Solution CS350 Operating Systems

Resource Sharing & Management

Lecture 5: Synchronization w/locks

Synchronization Spinlocks - Semaphores

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

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

Intertask Communication

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Concurrency: Principles of Deadlock. Processes and resources. Concurrency and deadlocks. Operating Systems Fall Processes need resources to run

Last Class: Monitors. Real-world Examples

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

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

Introduction to Parallel Programming Part 4 Confronting Race Conditions

Operating Systems ECE344. Ding Yuan

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

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

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

Concurrency: Deadlock and Starvation

Models of concurrency & synchronization algorithms

EECS 482 Introduction to Operating Systems

Performance Throughput Utilization of system resources

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

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

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

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

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

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

CS 153 Design of Operating Systems Winter 2016

Midterm Exam March 13, 2013 CS162 Operating Systems

Process Synchronization

SSC - Concurrency and Multi-threading Advanced topics about liveness

Today: Synchronization. Recap: Synchronization

Process Management And Synchronization

EECS 482 Introduction to Operating Systems

Concurrency. Chapter 5

Chapter 6 Concurrency: Deadlock and Starvation

CS370 Operating Systems

Process/Thread Synchronization

EE458 - Embedded Systems Lecture 8 Semaphores

POSIX Threads: a first step toward parallel programming. George Bosilca

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

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

Concurrent Processes Rab Nawaz Jadoon

Student Name: University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Science

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

Announcements. Cond Var: Review

Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

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

Achieving Synchronization or How to Build a Semaphore

OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL. Chap. 2.2 Interprocess Communication

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Deadlock. A Bit More on Synchronization. The Deadlock Problem. Deadlock Characterization. Operating Systems 2/7/2005. CSC 256/456 - Spring

Chapter 2 Processes and Threads

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

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

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

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

Process & Thread Management II CIS 657

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

Transcription:

College of Computer & Information Science Spring 2010 Northeastern University 26 January 2010 CS 7600: Intensive Computer Systems Scribe: Eric Miles In this lecture, we covered some of the (unwanted) behavior that synchronization constructs can exhibit; namely, race conditions and deadlock 1 Race conditions Consider the following setup There is a group of graduate students (GS) and a group of undergraduates (UG) There is a room in which a study will be conducted One GS and one UG go into the room at a time; the GS observes the UG performing a task for some period of time; the UG leaves; the GS leaves; finally, the GS writes a report about what he or she observed This setup has some similarities with multiple threads running in an operating system: if only a GS shows up to the room, he or she must wait until a UG arrives (and vice-versa) the room is an exclusive resource, as only two people can use it at a time the behavior of the two groups must be synchronized The room represents the monitor synchronization primitive The following picture illustrates the setup Grad Students Cg Crm GS Room UG GS leaves write report (non exclusive) Undergrads UG leaves Cu

We can implement this system in pseudocode: int g, u; // count of GSs and UGs // at least one must be 0 bool g in rm, u in rm; grad: ugrad: if (u > 0) if (g > 0) signal C u ; signal C g ; else { else { g++; u++; wait C g ; wait C u ; g--; u--; } } if (g in rm) if (u in rm) wait C rm ; wait C rm ; g in rm = T; u in rm = T; wait U done ; <perform task> signal U done ; g in rm = F; u in rm = F; signal C rm ; signal C rm ; <write report> return; return; In fact, a race condition exists in this example, as shown by the following sequence of events: G1 and U1 are already in the room; G2 and U2 are waiting to enter U1 leaves, signals C rm G2 enters, sets g in rm to True G1 leaves, sets g in rm to False U2 enters After this sequence, the room has one GS and one UG, but the flags indicate that it only has a UG This race condition has a simple fix: make two conditions C g rm and C u rm to control entrance the room, one for graduate students and one for undergraduates 2

Another type of synchronization primitive is the rendezvous; it is used by the Ada programming language, and illustrated in the following picture A B val = rmeet(5) 10 rmeet(10) > val 5 When the first process calls rmeet(), it waits until the second does also, and then they exchange values A rendezvous could be implemented as follows: condition C; int val1, val2; bool second; meet(x): 1 if (not(second)) { 2 second = T; 3 val1 = x; 4 wait C; 5 return val2; } else { 6 val2 = x; 7 second = F; 8 signal C; 9 return val1; } A race condition could also occur with this primitive Assume that A and B are the first two threads to run (in that order), and that we have two additional threads called C and D If C and D are both waiting when B finishes line 9, and they both run before A is allowed to execute line 5, then A will return the val2 set by D rather than the one set by B This issue can be fixed with a counting semaphore, used to ensure that only two threads are running at any given time 3

2 Deadlock Deadlock occurs when two (or more) processes are waiting for resources, and neither can proceed until the other releases its resources Here are the two most basic threads with which this could happen (m1 and m2 are mutexes) A B 1 1 ------ lock(m1); ------ lock(m2); ------ 2 2 ------ lock(m2); ------ lock(m1); ------ 3 3 ------ unlock(m1,m2); ------ unlock(m2,m1); ------ 1 1 If B s first line runs between A s first and second lines, both processes will wait forever at their second line Using the numbered regions in the code above, the following diagram illustrates the possible transitions As you can see, there is no way out of the (2,2) state A B 1 2 3 1 2 (1, 1) m2 (1, 2) m2, m1 (1, 3) m1 m2, m1 (2, 1) m1, m2 (2, 2) (2, 3) 3 (3, 1) m1, m2 (3, 2) (3, 3) There are four conditions that are necessary for deadlock to occur: 1 mutual exclusion (there are a limited number of resources, and they can t be shared) 2 hold and wait (threads don t release their resources when they have to wait for more) 3 no preemption (a thread can t be forced to release its resources) 4 a circular chain in the graph of resource dependencies exists at some time during execution 4

A classic example of deadlock in an operating system is illustrated by the following pseudocode getbuffer: spinlock(l) syscall: <remove> spinunlock(l) interrupt handler: getbuffer() getbuffer() Here, deadlock would occur if an interrupt occurred in the <remove> section of getbuffer Another less obvious example could occur when a system is low on memory In this case, what should happen is that some dirty pages from memory be written off somewhere else so they can be freed However, it could be possible that the very act of writing the dirty pages requires more memory than is available, causing the system to freeze up Two common ways of preventing deadlock are to (a) design your threads so that they will not be competing for the same resources, or (b) impose an ordering on resources, and require that threads only lock resources in increasing order, and only unlock them in decreasing order 5