Using Semaphores CS 241. March 14, University of Illinois

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

CSCE Operating Systems Synchronization: Producer-Consumer Problem. Qiang Zeng, Ph.D. Fall 2018

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

PROCESS SYNCHRONIZATION

Achieving Synchronization or How to Build a Semaphore

Process Synchronization. studykorner.org

Process Synchroniza/on

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

W4118 Operating Systems. Instructor: Junfeng Yang

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

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Synchronization Classic Problems

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

Process Synchronization(2)

CSC501 Operating Systems Principles. Process Synchronization

Synchroniza+on II COMS W4118

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

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

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

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Carnegie Mellon Synchronization: Advanced

Final review. From threads to file systems

Multithreading Programming II

ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT

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

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

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

Process Synchronization(2)

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Week 3. Locks & Semaphores

Chapters 5 and 6 Concurrency

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

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

Chapter 6: Process Synchronization

Process Synchronization(2)

Deadlock CS 241. March 19, University of Illinois

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Midterm Exam. October 20th, Thursday NSC

CS370 Operating Systems

An Introduction to Parallel Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

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

CS 153 Design of Operating Systems Winter 2016

Operating systems. Lecture 12

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

CSE 4/521 Introduction to Operating Systems

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

Process Synchronization

Concept of a process

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

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

CS 4410 Operating Systems. Review 1. Summer 2016 Cornell University

Chapter 2 Processes and Threads

Concurrency. Chapter 5

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

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

EECS 482 Introduction to Operating Systems

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

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

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

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

Process Management And Synchronization

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

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

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

Synchronization problems with semaphores

Concurrency and Synchronisation

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning

Last Class: Monitors. Real-world Examples

CS 153 Lab6. Kishore Kumar Pusukuri

Concurrency: Deadlock and Starvation. Chapter 6

Lecture 8: September 30

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CS3502 OPERATING SYSTEMS

Process Synchronization

Introduction to Operating Systems

CS 153 Design of Operating Systems Winter 2016

Lecture #24 Synchronization

CS370 Operating Systems

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

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

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

Semaphores. A semaphore is an object that consists of a. methods (e.g., functions): signal and wait. semaphore. method signal. counter.

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Process Synchronization

CS370 Operating Systems

Concurrency and Synchronisation

Interprocess Communication By: Kaushik Vaghani

Synchronization for Concurrent Tasks

C09: Process Synchronization

IV. Process Synchronisation

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

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

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

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

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

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

Transcription:

Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O Hallaron, Computer Systems: A Programmer's Perspective, 2/E 1

Announcements MP6 released Today A few midterm problems Using semaphores: the producer-consumer problem Using semaphores: the readers-writers problem 2

Midterm problem discussion 3

Using Semaphores 4

Before: Basic use of semaphores void * worker( void *ptr ) { int i; for (i = 0; i < ITERATIONS_PER_THREAD; i++) { sem_wait(&cnt_mutex); cnt++; sem_post(&cnt_mutex); } } 5

Today: Advanced use of semaphores [Monty Python s Flying Circus] 6

Using semaphores: The Producer-Consumer Problem 7

Producer-consumer problem Chefs cook items and put them on a conveyer belt Waiters pick items off the belt 8

Producer-consumer problem Now imagine many chefs!...and many waiters! 9

Producer-consumer problem A potential mess! 10

Producer-consumer problem inserts items removes items Shared resource: bounded buffer Efficient implementation: circular fixed-size buffer 11

Shared buffer 12

Shared buffer insertptr removeptr What does the chef do with a new pizza? Where does the waiter take a pizza from? 13

Shared buffer insertptr insertptr removeptr Insert pizza 14

Shared buffer insertptr removeptr Insert pizza 15

Shared buffer insertptr removeptr Insert pizza 16

Shared buffer insertptr removeptr Remove pizza removeptr 17

Shared buffer insertptr Insert pizza removeptr 18

Shared buffer Insert pizza insertptr removeptr 19

Shared buffer BUFFER FULL: Producer must wait! Insert pizza insertptr removeptr 20

Shared buffer removeptr insertptr Remove pizza 21

Shared buffer removeptr insertptr Remove pizza 22

Shared buffer removeptr insertptr Remove pizza 23

Shared buffer removeptr insertptr Remove pizza 24

Shared buffer removeptr insertptr Remove pizza 25

Shared buffer removeptr insertptr Remove pizza 26

Shared buffer removeptr insertptr Remove pizza 27

Shared buffer Buffer empty: Consumer must be blocked! removeptr insertptr Remove pizza 28

Designing a solution Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available What synchronization do we need? 29

Designing a solution Wait for empty slot Insert item Signal item arrival Mutex (shared buffer) Wait for item arrival Remove item Signal empty slot available What synchronization do we need? 30

Designing a solution Wait for empty slot Insert item Signal item arrival Semaphore (# empty slots) Wait for item arrival Remove item Signal empty slot available What synchronization do we need? 31

Designing a solution Wait for empty slot Insert item Signal item arrival Semaphore (# filled slots) Wait for item arrival Remove item Signal empty slot available What synchronization do we need? 32

Producer-Consumer Code Critical Section: move insert pointer buffer[ insertptr ] = data; insertptr = (insertptr + 1) % N; Critical Section: move remove pointer result = buffer[removeptr]; removeptr = (removeptr +1) % N; 33

Producer-Consumer Code Block if there are no free slots Counting semaphore check and decrement the number of free slots sem_wait(&slots); mutex_lock(&mutex); buffer[ insertptr ] = data; insertptr = (insertptr + 1) % N; mutex_unlock(&mutex); sem_post(&items); Done increment the number of available items Counting semaphore check and decrement the number of available items sem_wait(&items); mutex_lock(&mutex); result = buffer[removeptr]; removeptr = (removeptr +1) % N; mutex_unlock(&mutex); sem_post(&slots); Done increment the number of free slots Block if there are no items to take 34

Consumer Pseudocode: getitem() sem_wait(&items); pthread_mutex_lock(&mutex); result = buffer[removeptr]; removeptr = (removeptr +1) % N; pthread_mutex_unlock(&mutex); sem_signal(&slots); Error checking/eintr handling not shown 35

Producer Pseudocode: putitem(data) sem_wait(&slots); pthread_mutex_lock(&mutex); buffer[ insertptr ] = data; insertptr = (insertptr + 1) % N; pthread_mutex_unlock(&mutex); sem_signal(&items); Error checking/eintr handling not shown 36

Readers-Writers Problem 37

Readers-Writers Problem Generalization of the mutual exclusion problem Problem statement: Reader threads only read the object Writer threads modify the object Writers must have exclusive access to the object Unlimited number of readers can access the object Occurs frequently in real systems, e.g., Online airline reservation system Multithreaded caching Web proxy 38

Variants of Readers-Writers Favor readers No reader waits unless a writer is already in critical section A reader that arrives after a waiting writer gets priority over writer Favor writers Once a writer is ready to write, it performs its write as soon as possible A reader that arrives after a writer must wait, even if the writer is also waiting Starvation (thread waits indefinitely) possible in both cases Q: How could we fix this? 39

Solution favoring readers Shared: int readcnt; /* Initially = 0 */ sem_t mutex, w; /* Both initially = 1 */ Writers: void writer(void) { while (1) { sem_wait(&w); /* Critical section */ /* Writing here */ sem_post(&w); } } 40

Solution favoring readers Readers: void reader(void) { while (1) { sem_wait(&mutex); readcnt++; if (readcnt == 1) /* First reader in */ sem_wait(&w); /* Lock out writers */ sem_post(&mutex); /* Main critical section */ /* Reading would happen here */ sem_wait(&mutex); readcnt--; if (readcnt == 0) /* Last out */ sem_post(&w); /* Let in writers */ sem_post(&mutex); } } (full code online) 41

Summary Synchronization: more than just locking a critical section Semaphores useful for counting available resources sem_wait(): wait for resource only if none available sem_post(): signal availability of another resource Multiple semaphores / mutexes can work together to solve complex problems 42