C09: Process Synchronization

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

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

Chapter 2 Processes and Threads

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

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

7: Interprocess Communication

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

PROCESS SYNCHRONIZATION

Running. Time out. Event wait. Schedule. Ready. Blocked. Event occurs

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

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Introduction to OS Synchronization MOS 2.3

Chapter 6: Process Synchronization

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Threading and Synchronization. Fahd Albinali

Concurrency and Synchronisation

Concurrency and Synchronisation

PROCESSES & THREADS. Charles Abzug, Ph.D. Department of Computer Science James Madison University Harrisonburg, VA Charles Abzug

Dealing with Issues for Interprocess Communication

Section I Section Real Time Systems. Processes. 1.4 Inter-Processes Communication (part 1)

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

CISC 7310X. C05: CPU Scheduling. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/1/2018 CUNY Brooklyn College

Introduction to Operating Systems

Interprocess Communication and Synchronization

Operating Systems. Synchronisation Part I

Synchronization I. To do

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

CISC 7310X. C10: Deadlocks. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 4/12/2018 CUNY Brooklyn College

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

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

IV. Process Synchronisation

Synchronization Principles I

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Operating systems. Lecture 3 Interprocess communication. Narcis ILISEI, 2018

More Types of Synchronization 11/29/16

Synchronization: semaphores and some more stuff. Operating Systems, Spring 2018, I. Dinur, D. Hendler and R. Iakobashvili

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Concurrency. Chapter 5

Synchronization I. Today. Next time. Race condition, critical regions and locks. Condition variables, semaphores and Monitors

Synchronization I. Today. Next time. Monitors. ! Race condition, critical regions and locks. ! Condition variables, semaphores and

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

Process & Thread Management II CIS 657

Operating Systems. User OS. Kernel & Device Drivers. Interface Programs. Interprocess Communication (IPC)

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

Synchronization I. Jo, Heeseung

Synchronization Principles

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

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

Concurrency: a crash course

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

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

Module 6: Process Synchronization

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

CISC 7310X. C08: Virtual Memory. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 3/22/2018 CUNY Brooklyn College

G52CON: Concepts of Concurrency

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Synchroniza+on II COMS W4118

Process Synchronization(2)

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

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

Lesson 6: Process Synchronization

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

C02: Interrupts and I/O

Global Environment Model

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

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

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

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

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

CS370 Operating Systems

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Synchronization COMPSCI 386

CSC501 Operating Systems Principles. Process Synchronization

Interprocess Communication By: Kaushik Vaghani

Modern Computers often do lots of things at the same time (web servers, accessing disks, background processes waiting for s,...).

CS 153 Design of Operating Systems Winter 2016

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

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

Synchronization for Concurrent Tasks

CS370 Operating Systems

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Locks. Dongkun Shin, SKKU

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

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

Programming in Parallel COMP755

Process Synchronization(2)

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

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Management And Synchronization

CHAPTER 6: PROCESS SYNCHRONIZATION

Transcription:

CISC 7310X C09: Process Synchronization Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/29/2018 CUNY Brooklyn College 1

Outline Race condition and critical regions The bounded buffer problem Process coordination mutual exclusion synchronization Examples 3/29/2018 CUNY Brooklyn College 2

Race Condition Process A and B writes to the same slot [Figure 2-21 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 3

Mutual Exclusion and Critical Region Mutual exclusion Disable allow multiple processes read and write at the same time to a shared resource Critical region (or critical section) The part of the program where the shared memory is accessed. 3/29/2018 CUNY Brooklyn College 4

Requirements to Avoid Race Conditions 1. No two processes may be simultaneously inside their critical regions. 2. No process running outside its critical region may block other processes. 3. No process should have to wait forever to enter its critical region. 4. No assumptions may be made about speeds or the number of CPUs. 3/29/2018 CUNY Brooklyn College 5

Mutual Exclusion using Critical Section [Figure 2-22 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 6

Mutual Exclusion with Busy Waiting Disable interrupts Strict alternation Peterson s solution Synchronization hardware TSL or XCHG instructions 3/29/2018 CUNY Brooklyn College 7

Disable Interrupts Disable all interrupts just after entering critical section, and reenable them just before leaving No clock interrupt occur, no switching process before read and write completes Applicable to only single processor single core system Generally, not appropriate to user programs Otherwise all user programs must be placed in kernel mode 3/29/2018 CUNY Brooklyn College 8

Strict Alternation Busy waiting or spin lock Can a process be blocked by others while it is in non-critical section? [Figure 2-23 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 9

Peterson s Algorithm [Figure 2-24 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 10

Special instruction Instruction with exclusive access to memory bus TSL XCHG 3/29/2018 CUNY Brooklyn College 11

TSL Instruction Test-Set-Lock instruction [Figure 2-25 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 12

XCHG Instruction [Figure 2-26 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 13

Questions Busy-waiting solutions? 3/29/2018 CUNY Brooklyn College 14

Non-Busy-Waiting Locking Approaches Producer-consumer problem with bounded buffer Sleep and wake-up Semaphores Mutex Monitors Message Passing Barriers 3/29/2018 CUNY Brooklyn College 15

Priority Inversion Problem Busy-waiting, waste CPU cycles Priority inversion problem Assumptions Two processes, H with high priority, L with low priority Run H whenever it is in READY state Consider sequence L enters critical section H becomes ready to run (which state is L in?) H enters busy-waiting cycle L never gets CPU cycle to leave its critical section, H loops forever in busy-waiting cycle 3/29/2018 CUNY Brooklyn College 16

Producer-consumer problem Consider a bounded circular buffer shared by two processes N slots Producer adds item to the buffer, the item occupies a slot Consumer consumes item from the buffer, free a slot Counter counts items in the buffer counter == 0, empty buffer, consumer must wait counter == N, full buffer, producer must wait 3/29/2018 CUNY Brooklyn College 17

Sleep and Wake-up Sleep A system call that causes the caller to block (suspends the calling process, until another process wakes it up) Wakeup A system call that wakes up another process 3/29/2018 CUNY Brooklyn College 18

Producer 3/29/2018 CUNY Brooklyn College 19

Consumer [Figure 2-27 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 20

Can a race occur? Depends on checking condition Example Producer: if (count == N) sleep(); Consumer: if (counter ==0) sleep(); How about Producer: counter = counter + 1; Consumer: counter = counter 1; How many instructions needed to compute the above? 3/29/2018 CUNY Brooklyn College 21

Semaphores A variable with two atomic operations Down( semaphore *V) { } if (V->value > 0) V->value --; else if (V->value == 0) { add this process to S->list; Sleep(); // remains in Down before waking up } Up (semaphore *V) { } V->value ++; if (V->list is not empty) { remove a process P from V->list; Wake(P); } These operations are designed to be atomic with the help of Either interrupts (single processor & single core) Or TSL or XCHG instruction to implement a spin locks (on multi-processor or multi-core system) 3/29/2018 CUNY Brooklyn College 22

Producer-Consumer: Semaphore In total, three semaphores, producer uses two(empty & mutex), and consumer also two (full & mutex) empty: whether buffer is empty, if empty, consumer should sleep full: whether buffer is full, if full, producer should sleep mutex: whether other is in critical section, if so, sleep empty & full are for synchronization (coordination) mutex is for achieving mutual exclusion 3/29/2018 CUNY Brooklyn College 23

Producer with Two Semaphores [Figure 2-28 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 24

Consumer with Two Semaphores [Figure 2-28 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 25

Mutexes (Mutex Locks) When counting is not needed, we semaphore can be made simpler A semaphore with two states Locked (corresponding to down) Locked processes are sleeping Unlocked (corresponding to up) Unlocking a process is to wake up a sleeping process 3/29/2018 CUNY Brooklyn College 26

Mutex: Implementation [Figure 2-29 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 27

Mutex in Pthreads [Figures 2-30, 2-31 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 28

Producer-Consumer: PThreads A Simple implementation in PThreads What would you comment about it? How is it easy to make mistake? 3/29/2018 CUNY Brooklyn College 29

PThread: Producer [Figures 2-32 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 30

PThread: Consumer [Figures 2-32 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 31

PThread: Creating Threads [Figures 2-32 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 32

Questions? Priority inversion problem Producer-consumer problem Semaphore Mutex lock PThread example 3/29/2018 CUNY Brooklyn College 33

Monitor: Concept Semaphore In practice, easily use it wrongly Timing error Monitor: a high-level language construct that helps achieve mutual exclusion Build on top of semaphore Ensures that only process at a time is active within the monitor 3/29/2018 CUNY Brooklyn College 34

Monitors Producer and consumer are mutually exclusive [Figures 2-33 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 35

Monitor: Producer-Consumer [Figures 2-34 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 36

Monitor: In Java Java realizes monitor using synchronized methods 3/29/2018 CUNY Brooklyn College 37

Message Passing Monitor does not work well for distributed systems Message passing send(destination, &message) receive(source, &message) Can be blocking 3/29/2018 CUNY Brooklyn College 38

Message Passing: Producer [Figures 2-36 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 39

Message Passing: Consumer [Figures 2-36 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 40

Barriers A synchronization mechanism [Figures 2-37 in Tanenbaum & Bos, 2014] 3/29/2018 CUNY Brooklyn College 41

Questions Message passing and barriers 3/29/2018 CUNY Brooklyn College 42

Assignment Practice assignment 3/29/2018 CUNY Brooklyn College 43