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

Similar documents
[537] Semaphores. Tyler Harter

[537] Concurrency Bugs. Tyler Harter

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

Condition Variables. parent: begin child parent: end

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks

Semaphores Semaphores: A Definition

Condition Variables. Dongkun Shin, SKKU

Announcements. Class feedback for mid-course evaluations Receive about survey to fill out until this Friday

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Condition Variables. Dongkun Shin, SKKU

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Condition Variables. Figure 29.1: A Parent Waiting For Its Child

Condition Variables & Semaphores

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

CS 25200: Systems Programming. Lecture 26: Classic Synchronization Problems

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

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

Process Synchronization. studykorner.org

Semaphores. attention to the previous chapters. And even remembering a few things. You have, right?!

W4118 Operating Systems. Instructor: Junfeng Yang

[537] Locks and Condition Variables. Tyler Harter

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

Concurrency. Chapter 5

Concept of a process

Lecture 13 Condition Variables

Concurrency: Deadlock and Starvation. Chapter 6

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

Midterm Exam. October 20th, Thursday NSC

Concurrency: Signaling and Condition Variables

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

CS3733: Operating Systems

30. Condition Variables

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1

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

Chapter 6: Process Synchronization

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem.

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

PROCESS SYNCHRONIZATION

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

Synchroniza+on II COMS W4118

Concurrency, Thread. Dongkun Shin, SKKU

Interprocess Communication and Synchronization

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

Concurrency: Deadlock and Starvation

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

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

Process Synchronization(2)

OS Process Synchronization!

Process Synchronization(2)

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

Chapters 5 and 6 Concurrency

Last Class: Monitors. Real-world Examples

Achieving Synchronization or How to Build a Semaphore

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1

Process Synchronization(2)

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

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

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Interprocess Communication By: Kaushik Vaghani

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

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

CS533 Concepts of Operating Systems. Jonathan Walpole

C09: Process Synchronization

POSIX / System Programming

Dealing with Issues for Interprocess Communication

Chapter 2 Processes and Threads

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Dining Philosophers, Semaphores

Synchronization: Advanced

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

ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT

Concurrent Programming

Operating systems. Lecture 12

Semaphores Semaphores: A Definition

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

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

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

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

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

CPS 310 first midterm exam, 10/6/2014

CONCURRENCY: DEADLOCK. Shivaram Venkataraman CS 537, Spring 2019

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

Chapter 6 Concurrency: Deadlock and Starvation

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

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

CS 261 Fall Mike Lam, Professor. Threads

Process Synchronization

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

CS 318 Principles of Operating Systems

Problem Set 2. CS347: Operating Systems

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

Recitation 14: Proxy Lab Part 2

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

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

Synchronization Principles

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

Transcription:

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

Review: Sync Terminology Worksheet 2

Review: Semaphores 3

Semaphores o Motivation: Avoid busy waiting by blocking a process execution until some condition is satisfied o Two operations are defined on a semaphore variable s: sem_wait(s) (also called P(s) or down(s)) sem_post(s) (also called V(s) or up(s)) 4

Semaphore as a General Synchronization Tool o Semaphores provide a general synchronization mechanism beyond the critical section problem o Example problem: Execute B in P j only after A executed in P i o Use semaphore flag initialized to 0 o Code: P i : P j :!! A sem_wait(flag) sem_post(flag) B 5

Join with Semaphore sem_t s; void *child(void *arg) { printf( child\n ); sem_post(&s); } int main(int argc, char *argv[]) { sem_init(&s, 0); pthread_t c; printf( parent: begin\n ); pthread_create(c, NULL, child, NULL); sem_wait(&s); printf( parent: end\n ); return 0; } 6

Classical Problems of Synchronization o Producer-Consumer Problem Semaphore version Condition Variable A CV-based version o Readers-Writers Problem o Dining-Philosophers Problem 7

Producer-Consumer Problem o o o o The bounded-buffer producer-consumer problem assumes that there is a buffer of size N The producer process puts items to the buffer area The consumer process consumes items from the buffer The producer and the consumer execute concurrently........ producer consumer 8

Example: UNIX Pipes o A pipe may have many writers and readers o Internally, there is a finite-sized buffer o Writers add data to the buffer o Readers remove data from the buffer 9

Example: UNIX Pipes start Buffer end 10

Example: UNIX Pipes start Write Buffer end 11

Example: UNIX Pipes start Buffer end 12

Example: UNIX Pipes start Write Buffer end 13

Example: UNIX Pipes start Buffer end 14

Example: UNIX Pipes start Read Buffer end 15

Example: UNIX Pipes start Buffer end 16

Example: UNIX Pipes start Write Buffer end 17

Example: UNIX Pipes start Buffer end 18

Example: UNIX Pipes Read start Buffer end 19

Example: UNIX Pipes Read start Buffer end 20

Example: UNIX Pipes Read start Buffer Note: reader must wait end 21

Example: UNIX Pipes Write start Buffer end 22

Example: UNIX Pipes Write start Buffer end 23

Example: UNIX Pipes Write start Buffer Note: writer must wait end 24

Example: UNIX Pipes o Implementation Reads/writes to buffer require locking When buffers are full, writers (producers) must wait When buffers are empty, readers (consumers) must wait 25

Example: UNIX Pipes % ps aux less Demo Pipe % cat file grep <str> Pipe 26

Producer-Consumer Model: o Shared data: sem_t full, empty; Parameters o Initially: full = 0 /* The number of full buffers */ empty = MAX /* The number of empty buffers */ 27

First Attempt: MAX = 1 Put and Get routines 28

First Attempt: MAX = 10? Put and Get routines 29

First Attempt: MAX = 10? fill = 0 empty = 10 Producer 0: Running Producer 1: Runnable 30

First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Running Producer 1: Runnable void put(int value) { buffer[fill] = value; fill = (fill + 1) % MAX; } 31

First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Running Producer 1: Runnable void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } 32

First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Sleeping Producer 1: Runnable void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } 33

First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Runnable Producer 1: Running void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } 34

First Attempt: MAX = 10? fill = 0 Overwrite! empty = 8 Producer 0: Runnable Producer 1: Running void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } void put(int value) { buffer[fill] = value; fill = (fill + 1) % MAX; } 35

One More Parameter: A mutex lock o Shared data: sem_t full, empty; o Initially: full = 0; /* The number of full buffers */ empty = MAX; /* The number of empty buffers */ mutex = 1; /* Semaphore controlling the access to the buffer pool */ 36

Add Mutual Exclusion 37

Add Mutual Exclusion What if consumer gets to run first?? 38

Adding Mutual Exclusion Producer 0: Runnable Consumer 0: Running 39

Adding Mutual Exclusion Producer 0: Runnable Consumer 0: Runnable Consumer 0 is waiting for full to be greater than 0 40

Adding Mutual Exclusion Producer 0: Running Consumer 0: Runnable Consumer 0 is waiting for full to be greater than 0 41

Adding Mutual Exclusion Deadlock!! Producer 0: Running Consumer 0: Runnable Producer 0 gets stuck at acquiring mutex which has been locked by Consumer 0! Consumer 0 is waiting for full to be greater than 0 42

Deadlocks o A set of threads are said to be in a deadlock state when every thread in the set is waiting for an event that can be caused only by another thread in the set A typical deadlock dependency graph 43

Conditions for Deadlock o Mutual exclusion Threads claim exclusive control of resources that require (e.g., a thread grabs a lock) o Hold-and-wait Threads hold resources allocated to them while waiting for additional resources o No preemption Resources cannot be forcibly removed from threads that are holding them o Circular wait There exists a circular chain of threads such that each holds one or more resources that are being requests by next thread in chain 44

Correct Mutual Exclusion Mutex wraps just around critical section! Mutex wraps just around critical section! 45

Producer-Consumer Solution o Make sure that 1. The producer and the consumer do not access the buffer area and related variables at the same time 2. No item is made available to the consumer if all the buffer slots are empty 3. No slot in the buffer is made available to the producer if all the buffer slots are full 46

Condition Variables 47

Condition Variables A parent waiting for its child 48

Spin-based Approach Using a shared variable, parent spins until child set it to 1 49

Spin-based Approach Using a shared variable, parent spins until child set it to 1 What s the problem of this approach? 50

Condition Variables (CV) o Definition: An explicit queue that threads can put themselves when some condition is not as desired (by waiting on the condition) Other thread can wake one of those waiting threads to allow them to continue (by signaling on the condition) o Pthread CV 51

CV-based Approach???? 52

Broken Implementation 1 53

Broken Implementation 1 If parent comes after child, parent sleeps forever 54

Broken Implementation 2 55

Broken Implementation 2 No mutual exclusion, hence child may signal before parent calls cond_wait(). In this case, parent sleeps forever! 56

Trap 1 When Using CV Condition Variable wait wait thread thread 57

Trap 1 When Using CV thread signal Condition Variable wait wait thread thread 58

Trap 1 When Using CV Condition Variable wait thread 59

Trap 1 When Using CV Condition Variable wait thread Only one thread gets a signal 60

Trap 2 When Using CV Condition Variable 61

Trap 2 When Using CV thread signal Condition Variable 62

Trap 2 When Using CV Condition Variable 63

Trap 2 When Using CV Condition Variable wait thread 64

Trap 2 When Using CV Condition Variable wait thread waits forever 65

Trap 2 When Using CV Condition Variable wait thread waits forever Signal lost if nobody waiting at that time 66

Guarantee Upon signal, there has to be at least one thread waiting; If there are threads waiting, at least one thread will wake thread signal Condition Variable wait wait thread thread 67

CV-based Parent-wait-for-child Approach 68

CV-based Parent-wait-for-child Approach Good Rule of Thumb Always do 1. wait and 2. signal while holding the lock To prevent lost signal 69