Operating Systems. VII. Synchronization

Similar documents
Lecture 4: Process Management

Real Time Operating Systems

Real Time Operating Systems

Safety and Liveness. Thread Synchronization: Too Much Milk. Critical Sections. A Really Cool Theorem

Deadlock. CSE 2431: Introduction to Operating Systems Reading: Chap. 7, [OSC]

Shared resources. Sistemi in tempo reale. Giuseppe Lipari. Scuola Superiore Sant Anna Pisa -Italy

CIS 4930/6930: Principles of Cyber-Physical Systems

Improper Nesting Example

An Automotive Case Study ERTSS 2016

CSC501 Operating Systems Principles. Deadlock

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

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Lab Course: distributed data analytics

Lock using Bakery Algorithm

CPU scheduling. CPU Scheduling

INF Models of concurrency

Lecture Note #6: More on Task Scheduling EECS 571 Principles of Real-Time Embedded Systems Kang G. Shin EECS Department University of Michigan

CS 152 Computer Architecture and Engineering. Lecture 17: Synchronization and Sequential Consistency

Last class: Today: Synchronization. Deadlocks

CSE 380 Computer Operating Systems

INF 4140: Models of Concurrency Series 3

UC Santa Barbara. Operating Systems. Christopher Kruegel Department of Computer Science UC Santa Barbara

CPU Scheduling. CPU Scheduler

INF Models of concurrency

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Correctness of Concurrent Programs

CS 550 Operating Systems Spring CPU scheduling I

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

Scheduling I. Today Introduction to scheduling Classical algorithms. Next Time Advanced topics on scheduling

CPU Scheduling Exercises

CPU Scheduling. Heechul Yun

1 Lamport s Bakery Algorithm

INF Models of concurrency

TDDI04, K. Arvidsson, IDA, Linköpings universitet CPU Scheduling. Overview: CPU Scheduling. [SGG7] Chapter 5. Basic Concepts.

Clock-driven scheduling

Time. To do. q Physical clocks q Logical clocks

Lecture 9: Cri,cal Sec,ons revisited, and Reasoning about Programs. K. V. S. Prasad Dept of Computer Science Chalmers University Monday 23 Feb 2015

process arrival time CPU burst time priority p1 0ms 25ms 3 p2 1ms 9ms 1 p3 20ms 14ms 4 p4 32ms 4ms 2

Abstractions and Decision Procedures for Effective Software Model Checking

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

CS 152 Computer Architecture and Engineering. Lecture 17: Synchroniza<on and Sequen<al Consistency. Last Time, Lecture 16: GPUs. NOW Handout Page 1

Solutions to the second midterm. COSC 4330/6310 Summer 2013

CHAPTER 5 - PROCESS SCHEDULING

LSN 15 Processor Scheduling

Uniprocessor real-time scheduling

Resource Sharing in an Enhanced Rate-Based Execution Model

Scheduling I. Today. Next Time. ! Introduction to scheduling! Classical algorithms. ! Advanced topics on scheduling

Overview: Synchronous Computations

CS/IT OPERATING SYSTEMS

Introduction to Model Checking. Debdeep Mukhopadhyay IIT Madras

DMP. Deterministic Shared Memory Multiprocessing. Presenter: Wu, Weiyi Yale University

Deadlock Ezio Bartocci Institute for Computer Engineering

1 st Semester 2007/2008

Process Scheduling. Process Scheduling. CPU and I/O Bursts. CPU - I/O Burst Cycle. Variations in Bursts. Histogram of CPU Burst Times

TDDB68 Concurrent programming and operating systems. Lecture: CPU Scheduling II

Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

Deadlock (2) Dave Eckhardt Brian Railing Roger Dannenberg

Andrew Morton University of Waterloo Canada

Module 5: CPU Scheduling

Chapter 6: CPU Scheduling

Time. Today. l Physical clocks l Logical clocks

Real-Time Scheduling. Real Time Operating Systems and Middleware. Luca Abeni

CEC 450 Real-Time Systems

Exercises Solutions. Automation IEA, LTH. Chapter 2 Manufacturing and process systems. Chapter 5 Discrete manufacturing problems

ECE290 Fall 2012 Lecture 22. Dr. Zbigniew Kalbarczyk

2IN35 VLSI Programming Lab Work Communication Protocols: A Synchronous and an Asynchronous One

Scheduling Slack Time in Fixed Priority Pre-emptive Systems

CPU SCHEDULING RONG ZHENG

Simulation of Process Scheduling Algorithms

Administrivia. Course Objectives. Overview. Lecture Notes Week markem/cs333/ 2. Staff. 3. Prerequisites. 4. Grading. 1. Theory and application

Selected solutions for. TDDD07 Real-time Systems

Real-Time Software Transactional Memory: Contention Managers, Time Bounds, and Implementations

Distributed Systems Principles and Paradigms

Real-Time Scheduling and Resource Management

Lecture 13. Real-Time Scheduling. Daniel Kästner AbsInt GmbH 2013

Distributed Systems Principles and Paradigms. Chapter 06: Synchronization

Outline F eria AADL behavior 1/ 78

INF Models of concurrency

Simple Instruction-Pipelining (cont.) Pipelining Jumps

Last class: Today: Threads. CPU Scheduling

Do we have a quorum?

Real-time operating systems course. 6 Definitions Non real-time scheduling algorithms Real-time scheduling algorithm

Parallel Polynomial Evaluation

Verification of a Dynamic Channel Model using the SPIN Model Checker

Verifying Concurrent Memory Reclamation Algorithms with Grace

Bilateral Proofs of Safety and Progress Properties of Concurrent Programs (Working Draft)

CMP N 301 Computer Architecture. Appendix C

Algorithmic verification

Atomic m-register operations

Shedding the Shackles of Time-Division Multiplexing

Periodic scheduling 05/06/

Design of Real-Time Software

Efficient TDM-based Arbitration for Mixed-Criticality Systems on Multi-Cores

Socket Programming. Daniel Zappala. CS 360 Internet Programming Brigham Young University

Formal Verification Techniques. Riccardo Sisto, Politecnico di Torino

Applied C Fri

Automata-Theoretic Model Checking of Reactive Systems

Distributed Systems Part II Solution to Exercise Sheet 10

Clocks in Asynchronous Systems

Barrier. Overview: Synchronous Computations. Barriers. Counter-based or Linear Barriers

Transcription:

Operating Systems VII. Synchronization Ludovic Apvrille ludovic.apvrille@telecom-paristech.fr Eurecom, office 470 http://soc.eurecom.fr/os/ @OS Eurecom

Outline Synchronization issues 2/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Why is Synchronization Necessary? Know for a process / thread at which execution point is another process / thread Ensure shared data consistency Where is my money?! 3/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Critical Section Problem: Critical code sections must satisfy the following requirements: 1. Mutual exclusion (or safety condition): At most one process at a time is allowed to execute code inside a critical section of code 2. Machine independence: No assumptions should be made about speeds or the number of CPUs 3. Progress: Process running outside a critical section may not block other processes 4. Bounded waiting (or liveness): Process should be guaranteed to enter a critical section within a finite time Critical-sections are: Used when resources are shared between different processes Supported by many programming mechanisms 4/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Mutual Exclusion Using Critical Sections 5/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

The Deadlock Issue Problematic Use of shared resources: request, use, release Deadlock = situation in which a process waits for a resource that will never be available Handling deadlocks: Use a protocol to ensure that the system will never enter a deadlock state Deadlock prevention: Restraining how requests can be made Deadlock avoidance: More information from user on the use of resources 6/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

The Deadlock Issue (Cont.) Handling deadlocks: Allow the system to enter a deadlock state and then recover Process termination Resource preemption Ignore the problem (i.e., assume deadlocks never occur in the system) Most OS, including Linux 7/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Software Approaches Disabling Interrupts / Enabling interrupts Unwise to empower user processes to turn off interrupts! Lock variables Procedure Reads the value of a shared variable If 0, sets it to 1 and enters the critical section If 1, waits until the variable equals to 0 There is a major flaw related to scheduling Can you guess why? 8/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Software Approaches (Cont.) Strict alternation Busy waiting (waste of CPU) Violates the progress requirement of critical-sections Can you guess why? Process 0 While (TRUE){ w h i l e ( t u r n!= 0 ) ; / b e g i n c r i t i c a l s e c t i o n /... t u r n = 1 ; / end c r i t i c a l s e c t i o n / Process 1 While (TRUE){ w h i l e ( t u r n!= 1 ) ; / b e g i n c r i t i c a l s e c t i o n /... t u r n = 0 ; / end c r i t i c a l s e c t i o n / 9/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Software Approaches (Cont.) Dekker s and Peterson s solution 1965, 1981 Alternation + lock variables Process 0 w h i l e ( t r u e ) { f l a g [ 0 ] = t r u e ; t u r n = 0 ; w h i l e ( f l a g [ 1 ] && ( t u r n ==0)); / C r i t i c a l s e c t i o n /... f l a g [ 0 ] = f a l s e ; / End c r i t i c a l s e c t i o n / Process 1 w h i l e ( t r u e ) { f l a g [ 1 ] = t r u e ; t u r n = 1 ; w h i l e ( f l a g [ 0 ] && ( t u r n ==1)); / C r i t i c a l s e c t i o n /... f l a g [ 1 ] = f a l s e ; / End c r i t i c a l s e c t i o n / 10/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Hardware Approaches The Test and Set Lock (TSL) Instruction Special assembly instruction which is atomic TSL Rx, LOCK Reads the content of the memory at address lock, stores it in register Rx, and sets the value at address lock to 1 Assembly code to enter / leave critical sections E n t e r c r i t i c a l s e c t i o n : TSL r e g i s t e r, LOCK C o p i e s l o c k to r e g i s t e r and s e t l o c k to 1 CMP r e g i s t e r, #0 Was l o c k e q u a l to 0? JNE E n t e r c r i t i c a l s e c t i o n I f!= 0 > l o c k was s e t > l o o p RET E n t e r s c r i t i c a l s e c t i o n L e a v e c r i t i c a l s e c t i o n : MOVE LOCK, #0 Stores 0 in lock RET Q u i t s c r i t i c a l s e c t i o n 11/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Limits of Peterson s and TSL solutions Busy waiting & Priority inversion problem If a lower priority process is in critical section and a higher priority process busy waits to enter this critical section, the lower priority process never gains CPU Higher priority processes can never enter critical section Solution: sleep/wake-up Sleep(): Caller is blocked on a given address until another process wakes it up Wakeup(): Caller wakes up all processes waiting on a given address 12/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Outline Objects for ensuring mutual exclusion Example: using mutexes and condition variables Synchronization issues Objects for ensuring mutual exclusion Example: using mutexes and condition variables 13/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Semaphores Objects for ensuring mutual exclusion Example: using mutexes and condition variables A semaphore is a counter shared by multiple processes Processes can increment or decrement this counter in an atomic way Mainly used to protect access to shared resources Different APIs to use semaphores IPC System V semget(),semclt(), semop() POSIX semopen(),sempost(), semwait(),... 14/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Mutex Objects for ensuring mutual exclusion Example: using mutexes and condition variables Mutual Exclusion A mutex has two states: locked, unlocked Only one thread / process at a time can lock a mutex When a mutex is locked, other processes / threads block when they try to lock the same mutex: Locking stops when the mutex is unlocked One of the waiting process / thread succeeds in locking the mutex 15/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Objects for ensuring mutual exclusion Example: using mutexes and condition variables Mutex: Main functions (pthread Library ) Initialize a mutex p t h r e a d m u t e x t mymutex ; p t h r e a d m u t e x i n i t (&mymutex, NULL ) ; Lock the mutex Waits for the lock p t h r e a d m u t e x l o c k (&mymutex ) ; Returns immediately if mutex is locked p t h r e a d m u t e x t r y l o c k (&mymutex ) ; Unlock the mutex p t h r e a d m u t e x u n l o c k (&mymutex ) ; 16/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Objects for ensuring mutual exclusion Example: using mutexes and condition variables Condition Variables Used to signal a condition has changed To wait on a condition Put lock on mutex Wait on that condition Automatic release of the lock To signal a change on a condition Put lock on mutex Signal that condition 17/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Use of Condition Variables Objects for ensuring mutual exclusion Example: using mutexes and condition variables 18/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Objects for ensuring mutual exclusion Example: using mutexes and condition variables Producer/Consumer Example (pthread lib.) #i n c l u d e <s t d l i b. h> #i n c l u d e <pthread. h> #d e f i n e N THREADS PROD 3 #d e f i n e N THREADS CONS 4 void produce ( void ) ; void producedata ( i n t id ) ; void consume ( void ) ; void consumedata ( i n t id ) ; i n t data = 0 ; i n t maxdata = 5 ; pthread mutex t mymutex ; pthread cond t f u l l, empty ; i n t main ( v o i d ) { i n t i ; p t h r e a d t t i d p [ N THREADS PROD ] ; p t h r e a d t t i d c [ N THREADS CONS ] ; pthread mutex init (&mymutex, NULL ) ; f o r ( i =0; i<n THREADS PROD ; i ++) { p t h r e a d c r e a t e (& t i d p [ i ], NULL, produce, ( v o i d ) i ) ; f o r ( i =0; i<n THREADS CONS ; i ++) { p t h r e a d c r e a t e (& t i d c [ i ], NULL, consume, ( v o i d ) i ) ; f o r ( i = 0 ; i < N THREADS PROD ; i ++) { p t h r e a d j o i n ( t i d p [ i ], NULL ) ; f o r ( i = 0 ; i < N THREADS CONS ; i ++) { p t h r e a d j o i n ( t i d c [ i ], NULL ) ; r e t u r n ( 0 ) ; 19/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Objects for ensuring mutual exclusion Example: using mutexes and condition variables Producer/Consumer Example (Cont.) v o i d produce ( v o i d a r g ) { i n t myid = ( i n t ) a r g ; w h i l e ( 1 ) { producedata ( myid ) ; s l e e p ( random ( ) % 5 ) ; v o i d consume ( v o i d a r g ) { i n t myid = ( i n t ) a r g ; p r i n t f ( "I am the consumer #%d\n", myid ) ; w h i l e ( 1 ) { consumedata ( myid ) ; s l e e p ( random ( ) % 5 ) ; 20/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Objects for ensuring mutual exclusion Example: using mutexes and condition variables Producer/Consumer Example (Cont.) v o i d producedata ( i n t i d ) { pthread mutex lock (&mymutex ) ; i f ( data == maxdata ) { p r i n t f ( "#%d is waiting for less data ; data = %d\n", id, data ) ; p t h r e a d c o n d w a i t (& f u l l, &mymutex ) ; data ++; p r i n t f ( "#%d is producing data ; data = %d\n", id, data ) ; p t h r e a d c o n d s i g n a l (&empty ) ; pthread mutex unlock (&mymutex ) ; v o i d consumedata ( i n t i d ){ pthread mutex lock (&mymutex ) ; i f ( data == 0) { p r i n t f ( "#%d is waiting for more data ; data = %d\n", id, data ) ; pthread cond wait (&empty, &mymutex ) ; data ; p r i n t f ( "#%d is consuming data ; data = %d\n", id, data ) ; p t h r e a d c o n d s i g n a l (& f u l l ) ; pthread mutex unlock (&mymutex ) ; Actually, this code is wrong... but where??? 21/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization

Objects for ensuring mutual exclusion Example: using mutexes and condition variables Producer/Consumer Example: Execution $gcc l p t h r e a d prod p r o d c o n s. c $prod #3 i s waiting f o r more data ; data = 0 #1 i s waiting f o r more data ; data = 0 #2 i s producing data ; data = 1 #2 i s producing data ; data = 2 #0 i s consuming data ; data = 1 #2 i s consuming data ; data = 0 #2 i s waiting f o r more data ; data = 0 #3 i s waiting f o r more data ; data = 0 #0 i s producing data ; data = 1 #1 i s consuming data ; data = 0 #2 i s producing data ; data = 1 #2 i s consuming data ; data = 0 #1 i s producing data ; data = 1 #3 i s consuming data ; data = 0 #0 i s waiting f o r more data ; data = 0 #2 i s waiting f o r more data ; data = 0 #0 i s producing data ; data = 1 #0 i s consuming data ; data = 0 #0 i s waiting f o r more data ; data = 0 #3 i s waiting f o r more data ; data = 0 #1 i s waiting f o r more data ; data = 0 #2 i s producing data ; data = 1 #2 i s producing data ; data = 2 #2 i s consuming data ; data = 1 #0 i s consuming data ; data = 0 #1 i s producing data ; data = 1 #3 i s consuming data ; data = 0 #2 i s producing data ; data = 1 #2 i s producing data ; data = 2 #1 i s consuming data ; data = 1 #0 i s producing data ; data = 2 #0 i s producing data ; data = 3 #2 i s consuming data ; data = 2 #2 i s producing data ; data = 3 #2 i s producing data ; data = 4 #1 i s producing data ; data = 5 #0 i s consuming data ; data = 4 #1 i s consuming data ; data = 3 #3 i s consuming data ; data = 2 #1 i s producing data ; data = 3 #1 i s producing data ; data = 4 #0 i s producing data ; data = 5 #0 i s w a i t i n g f o r l e s s data ; data = 5 #2 i s consuming data ; data = 4 #0 i s producing data ; data = 5 #1 i s consuming data ; data = 4 #2 i s producing data ; data = 5 #0 i s consuming data ; data = 4 #1 i s producing data ; data = 5 #0 i s w a i t i n g f o r l e s s data ; data = 5 #2 i s w a i t i n g f o r l e s s data ; data = 5 #3 i s consuming data ; data = 4... 22/22 Fall 2017 Institut Mines-Telecom Operating Systems - Synchronization