Concurrency Race Conditions and Deadlocks

Similar documents
Interprocess Communication By: Kaushik Vaghani

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

Dealing with Issues for Interprocess Communication

CHAPTER 6: PROCESS SYNCHRONIZATION

Introduction to OS Synchronization MOS 2.3

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

CS370 Operating Systems

Concurrency. Chapter 5

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

Process Synchronization

Chapter 6: Process Synchronization

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

Synchronization for Concurrent Tasks

Dept. of CSE, York Univ. 1

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Introduction to Real-Time Operating Systems

More on Synchronization and Deadlock

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

CS370 Operating Systems

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

C09: Process Synchronization

Concurrency: Deadlock and Starvation. Chapter 6

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

Locks. Dongkun Shin, SKKU

Last Class: Monitors. Real-world Examples

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

Concurrency: Deadlock and Starvation

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

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

CSE 4/521 Introduction to Operating Systems

Mutual Exclusion and Synchronization

Concurrency, Thread. Dongkun Shin, SKKU

Lesson 6: Process Synchronization

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Computer Systems Engineering: Spring Quiz I Solutions

CMSC421: Principles of Operating Systems

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

Chapter 6: Process Synchronization

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

Concurrent and Distributed Systems Introduction

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Chapter 6 Concurrency: Deadlock and Starvation

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Operating Systems. Synchronisation Part I

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

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

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

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

Atomic Transac1ons. Atomic Transactions. Q1: What if network fails before deposit? Q2: What if sequence is interrupted by another sequence?

Timers 1 / 46. Jiffies. Potent and Evil Magic

Comp 310 Computer Systems and Organization

Computer Systems Assignment 4: Scheduling and I/O

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

Threading and Synchronization. Fahd Albinali

Chapters 5 and 6 Concurrency

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

Process & Thread Management II CIS 657

CS420: Operating Systems. Process Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

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

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

CPS 110 Midterm. Spring 2011

CS533 Concepts of Operating Systems. Jonathan Walpole

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Real-Time Programming

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Remaining Contemplation Questions

Linux Synchronization Mechanisms in Driver Development. Dr. B. Thangaraju & S. Parimala

Chapter 6: Process Synchronization

CS 111. Operating Systems Peter Reiher

Chapter 2 Processes and Threads

CS330: Operating System and Lab. (Spring 2006) I/O Systems

1. Motivation (Race Condition)

Concurrency: a crash course

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Operating Systems. Synchronization

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Lecture 9: Midterm Review

Experience with Processes and Monitors in Mesa. Arvind Krishnamurthy

Synchronization COMPSCI 386

Chapter 5: Synchronization 1

Prof. Dr. Hasan Hüseyin

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

Concurrency: Mutual Exclusion and Synchronization. Concurrency

Reducing Priority Inversion in Interprocessor Synchronization on a Fixed-Priority Bus

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

Synchronization Principles

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

1995 Paper 10 Question 7

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

2 Threads vs. Processes

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

PROCESS SYNCHRONIZATION

What are they? How do we represent them? Scheduling Something smaller than a process? Threads Synchronizing and Communicating Classic IPC problems

EE458 - Embedded Systems Lecture 8 Semaphores

Interprocess Communication and Synchronization

CS 153 Design of Operating Systems Winter 2016

Transcription:

Concurrency Race Conditions and Deadlocks Kartik Gopalan Chapters 2 (2.3) and 6 Tanenbaum s Modern OS

Sequential Loosely, doing many things, but one after another E.g. Finish one assignment, then another For example, two tasks executed on one CPU one after another. Concurrent Loosely, concurrency is juggling many things within a time window. E.g. switching your attention back-andforth between two different assignments. For example, two tasks share a single CPU over time. Parallel Loosely, parallelism is doing many things simultaneously. E.g. working on the computer and chewing gum at the same time. Parallelism is a subset of concurrency. All parallelism is concurrency But not all concurrency is parallelism. For example, two threads executing on two different CPUs simultaneously. CPU 1 CPU 1 CPU 1 CPU 2 Task 1 Task 1 Task 1 Task 2 Time Task 1 Task 2 Task 1 Task 2 Task 2 Task 1 Task 2 Task 2

Concurrency and Synchronization Concurrent tasks may either execute independently Or, concurrent tasks may need to synchronize (communicate) now and then Synchronization requires access to shared resources Shared memory (buffers) Pipes Signals, etc 3

Critical Section Also called critical region. A section of code in a concurrent task that modifies or accesses a resource shared with another task. Examples A piece of code that reads from or writes to a shared memory region Or a code that modifies or traverses a shared linked list.

Race Condition and Deadlocks Race Condition Incorrect behavior of a program due to concurrent execution of critical sections by two or more threads. E.g. if thread 1 deletes an entry in a linked list while thread 2 is accessing the same entry. Deadlocks When two or more processes stop making progress indefinitely because they are all waiting for each other to do something. E.g. If process A waits for process B to release a resource, and Process B is waiting for process A to release another resource at the same time. In this case, neither A not B can proceed because both are waiting for the other to proceed.

Race Conditions and Locking Race Condition: Incorrect behavior of a program due to concurrent execution of critical sections by two or more threads.

Solution: Mutual Exclusion Don t allow two or more processes to execute their critical sections concurrently (on the same resource). Steps 1. Acquire Lock 2. Critical Section 3. Release Lock

Conditions for correct mutual exclusion 1. No two processes are simultaneously in the critical region 2. No assumptions are made about speeds or numbers of CPUs 3. No process running outside its critical region may block another process running in the critical region 4. No process must wait forever to enter its critical region Waiting forever indicates a deadlock (1) and (2) are enforced by the operating system s implementation of locks Programmers assume that locks satisfy (1) and (2) (3) and (4) have to be ensured by the programmer using the locks. OS cannot enforce these.

Mutual Exclusion among Readers and Writers General rule If any thread is writing to a shared resource, other threads are disallowed from reading or writing to the same resource. Thread 1 Thread 2 Allowed/Disallowed Read Read Allowed Read Write Disallowed Write Read Disallowed Write Write Disallowed Exceptions may be allowed for special types of lockless data structures. 9

Blocking Locks Give up CPU till lock is available while(lock unavailable) yield CPU to others; // or block till lock available return success; Usage: Lock(resource); // Claim a shared resource Execute Critical Section;//access or modify the shared resource Unlock(resource); // unclaim shared resource Advantage: Simple to use. Locking always succeeds ultimately. Disadvantage: Blocking may be indefinite. Process is moved out of Running state to Blocked state. Also, too much overhead in getting back to running state if lock may be available soon. running >blocked >ready >running 10

Non-blocking locks Don t block if lock is unavailable if(lock unavailable) return failure; else return success Usage if(trylock(resource) == success) Execute Critical Section; Unlock(resource); else Do something else; // plan B Advantage: No unbounded blocking Disadvantage: Need a plan B to handle locking failure 11

Spinlocks Don t block. Instead, constantly poll the lock for availability. while (lock is unavailable) continue; // try again return success; Usage: Just like blocking locks SpinLock(resource); Execute Critical Section; SpinUnlock(resource); Advantage Very efficient with short critical sections if you expect a lock to be released quickly Disadvantage Doesn t yield the CPU and burns CPU cycles Bad if critical sections are long. Efficient only if machine has multiple CPUs. Counterproductive on uni-processor machines 12

Best practices for locking 1. Associate locks with shared resources, NOT code. E.g. a lock is for protecting a linked list NOT for protecting insert() and remove() functions. That way, you can use the same critical sections to operate on different shared resources having different locks. 2. Guard each shared resource by a separate lock to improve concurrency E.g. Linked list 1 should be guarded by Lock 1 Linked List 2 by Lock 2 and so on. OS cannot enforce these properties Up to the programmer to ensure these properties 13

Deadlocks When two or more processes stop making progress indefinitely because they are all waiting for each other to do something.

Deadlock when using multiple locks Say you have two processes P1 and P2 Both need to acquire two locks L1 and L2 to access a resource. Problem: Deadlock P1 acquires L1 P2 acquires L2 P1 tries to acquire L2 and blocks P2 tries to acquire L1 and blocks We have a deadlock! Solution: Lock Ordering Sort the locks in a fixed order (say L1 followed by L2) Always acquire locks in the sorted order. Lock ordering example: P1 acquires L1 P2 tries to acquire L1 and blocks P1 acquires L2 P1 executes critical section P1 releases L2 P1 releases L1 P2 wakes up P2 acquires L1 P2 acquires L2 P2 executes critical section P2 releases L2 P2 releases L1 No deadlock! 15

Generalizing the lock-ordering solution Given N Locks: L1, L2,, LN K Processes: P1, P2,, Pk A process must acquire any subset of locks in sorted order A process doesn t need to acquire ALL the locks. But whatever locks it needs, it MUST acquire in sorted order. E.g. Assume N=10, i.e. you have 10 Locks (Allowed) Pi acquires L1, then L5, then L10 (Allowed) Pj acquires L1, then L3, then L10 (NOT Allowed) Pk acquires L5, then L2, then L1 16

Priority Inversion Say there are three processes using priority based scheduling. Ph High priority Pm Medium priority Pl Low priority Pl acquires a lock L Pl starts executing critical section Ph tries to acquire lock L and blocks Pm becomes ready and preempts Pl from the CPU. Pl might never exit critical section if Pm keeps preempting Pl So Ph might never enter critical section Problem: Priority Inversion A high priority process Ph is blocked waiting for a low priority process Pl Pl cannot proceed because a medium priority process Pm is executing. Solution: Priority Inheritance Temporarily increase the priority of Pl to HIGH PRIORITY Pl will be scheduled and will exit critical section quickly Then Ph can execute. 17

Interrupts and Deadlocks Problem Interrupts invoke interrupt service routines (ISR) in the kernel. ISR must process the interrupt quickly and return. So ISRs must never block or spin on a lock. But what if ISRs need a lock to process the interrupt? The problem: A kernel thread T acquires lock L An interrupt fires and ISR preempts T ISR tries to acquire L and blocks (or spins) T is also blocked because ISR cannot return Deadlock! 18

Interrupts and Deadlocks Solutions 1. Don t lock in ISR! Defer any locking work to thread context (softirqs in Linux) 2. If you must, use try_lock() instead of lock() in ISR try_lock() = if lock is available then get it, else return with error. Be ready to handle error return 3. Or disable interrupts in thread T before locking If ISR cannot run when lock is acquired by T, then there s no deadlock. When ISR runs, it assumes that T doesn t have the lock. But, disabling interrupts too long is also not a good idea. 4. Or, on multi-cpu systems, use spinlocks, but carefully ISRs on different CPUs can compete for locks without blocking Threads must use interrupt disabling versions of spinlock (spinlock_irqsave). Why? 19