Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Similar documents
Chapter 6: Process Synchronization

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

Lesson 6: Process Synchronization

Process Management And Synchronization

Chapter 6: Process Synchronization

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

CSC501 Operating Systems Principles. Process Synchronization

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

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

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

CHAPTER 6: PROCESS SYNCHRONIZATION

Synchronization Principles

Dealing with Issues for Interprocess Communication

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization

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

Process Synchronization

Operating Systems ECE344

CS3502 OPERATING SYSTEMS

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

Introduction to Operating Systems

Opera&ng Systems ECE344

Process Synchronization. studykorner.org

Chapter 5: Process Synchronization

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

Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CS370 Operating Systems

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

Concurrency. Chapter 5

Interprocess Communication By: Kaushik Vaghani

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

Synchronization Principles II

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

Concurrency and Synchronisation

Process Synchronization Mechanisms

Process Synchronization

CS370 Operating Systems

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Lecture 6 (cont.): Semaphores and Monitors

Concept of a process

Semaphores (by Dijkstra)

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

Interprocess Communication and Synchronization

PROCESS SYNCHRONIZATION

Process Synchronization

Process Coordination

Process Synchronization(2)

Chapter 6: Process Synchronization

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

Concurrency and Synchronisation

Synchronization for Concurrent Tasks

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

Process Synchronization

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

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

Chapter 7: Process Synchronization!

More Types of Synchronization 11/29/16

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

Concurrency and Synchronisation. Leonid Ryzhyk

Chapter 5: Process Synchronization

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

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

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

Synchronization Basic Problem:

Process Synchronization(2)

Concurrency: Deadlock and Starvation. Chapter 6

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

CS370 Operating Systems

CSE 4/521 Introduction to Operating Systems

Module 6: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

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

CS 153 Design of Operating Systems Winter 2016

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

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

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

Chapter 6: Process Synchronization

Process Synchronization

Chapter 5: Process Synchronization

Chapter 6 Synchronization

Process Co-ordination OPERATING SYSTEMS

Chapter 6: Process Synchronization

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

IV. Process Synchronisation

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

OS Process Synchronization!

Chapter 6 Synchronization

Process Synchronization(2)

Lecture 8: September 30

Classic Problems of Synchronization

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

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

Chapter 7: Process Synchronization. Background

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

Dept. of CSE, York Univ. 1

Transcription:

Semaphores Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu)

Synchronization Types Mutual exclusion Only one thread in a critical section at a time Waiting for events One thread waits for another to complete some action before it continues Producer/consumer Multiple producers, multiple consumers Pipeline A series of producer and consumer Defer work with background thread Non-critical work in the background when CPU is idle EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 2

Higher-level Synchronization Spinlocks and disabling interrupts are not enough Useful only for very short and simple critical sections Need to block threads when lock is held by others (mutexes) Need to block threads until a certain condition is met Higher-level synchronization mechanisms Semaphores Simple, yet powerful Hard to program with Mutexes and condition variables Used in Pthreads EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 3

Semaphores A synchronization primitive higher level than locks Invented by Dijkstra in 1968, as part of the THE OS Does not require busy waiting A semaphore is an object with an integer value (state) State cannot be directly accessed by user program, but it determines the behavior of semaphore operations Manipulated atomically through two operations Wait(): decrement the value, and wait until the value is >= 0 = P() (after Dutch word for test), down(), or sem_wait() Signal(): increment the value, then wake up a single waiter = V() (after Dutch word for increment), up(), or sem_post() EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 4

Implementing Semaphores typedef struct { int value; struct process *Q; semaphore; void wait (semaphore *S) { S->value--; if (S->value < 0) { add this process to S->Q; block (); void signal (semaphore *S) { S->value++; if (S->value <= 0) { remove a process P from S->Q; wakeup (P); wait() / signal() are critical sections! Hence, they must be executed atomically with respect to each other. HOW?? EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 5

Types of Semaphores Binary semaphore ( mutex) Semaphore value is initialized to 1 Guarantees mutually exclusive access to resource Only one thread allowed entry at a time Counting semaphore Semaphore value is initialized to N Represents a resource with many units available Allows threads to enter as long as more units are available EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 6

Bounded Buffer Problem (1) Producer/consumer problem There is a set of resource buffers shared by producers and consumers Producer inserts resources into the buffer Output, disk blocks, memory pages, etc. Consumer removes resources from the buffer Whatever is generated by the producer Producer and consumer execute in different rates No serialization of one behind the other Tasks are independent The buffer allows each to run without explicit handoff pipes: single producer, single consumer EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 7

Bounded Buffer Problem (2) No synchronization Producer Consumer void produce (data) { while (count==n); buffer[in] = data; in = (in+1) % N; count++; int count; struct item buffer[n]; int in, out; in out void consume (data) { while (count==0); data = buffer[out]; out = (out+1) % N; count--; EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 8

Bounded Buffer Problem (3) Implementation with semaphores void produce (data) { Producer wait (&empty); wait (&mutex); buffer[in] = data; in = (in+1) % N; signal (&mutex); signal (&full); Semaphore mutex = 1; empty = N; full = 0; struct item buffer[n]; int in, out; in out void consume (data) { Consumer wait (&full); wait (&mutex); data = buffer[out]; out = (out+1) % N; signal (&mutex); signal (&empty); EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 9

Readers-Writers Problem (1) Sharing resource among multiple readers and writers An object is shared among several threads Some threads only read the object, others only write it We can allow multiple readers at a time We can only allow one writer at a time Implementation with semaphores readcount: # of threads reading object mutex: control access to readcount rw: exclusive writing or reading EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 10

Readers-Writers Problem (2) // number of readers int readcount = 0; // mutex for readcount Semaphore mutex = 1; // mutex for reading/writing Semaphore rw = 1; void Writer () { wait (&rw); Write signal (&rw); void Reader () { wait (&mutex); readcount++; if (readcount == 1) wait (&rw); signal (&mutex); Read wait (&mutex); readcount--; if (readcount == 0) signal (&rw); signal (&mutex); EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 11

Readers-Writers Problem (3) If there is a writer The first reader blocks on rw All other readers will then block on mutex Once a writer exits, all readers can fall through Which reader gets to go first? The last reader to exit signals waiting writer Can new readers get in while writer is waiting? When a writer exits, if there is both a reader and writer waiting, which one goes next is up to scheduler EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 12

Dining Philosophers Problem (1) A classic synchronization problem by Dijkstra, 1965 Modeled after the lives of five philosophers sitting around a round table Each philosopher repeats forever: Thinking Pick up two forks Eating Put down two forks Pick one fork at a time EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 13

Dining Philosophers Problem (2) A simple solution // initialized to 1 Semaphore forks[n]; #define L(i) (i) #define R(i) ((i + 1) % N) void philosopher (int i) { while (1) { think (); pickup (i); eat (); putdown (i); void pickup (int i) { wait (&forks[l(i)]); wait (&forks[r(i)]); void putdown (int i) { signal (&forks[l(i)]); signal (&forks[r(i)]); EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 14

Dining Philosophers Problem (3) A deadlock-free solution // initialized to 1 Semaphore forks[n]; #define L(i) (i) #define R(i) ((i + 1) % N) void philosopher (int i) { while (1) { think (); pickup (i); eat (); putdown (i); void pickup (int i) { if (i == (N-1)) { wait (&forks[r(i)]); wait (&forks[l(i)]); else { wait (&forks[l(i)]); wait (&forks[r(i)]); void putdown (int i) { signal (&forks[l(i)]); signal (&forks[r(i)]); EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 15

Semaphores Pros One primitive can be used for both critical sections (mutual exclusion) and coordination among threads (scheduling) Cons They are essentially shared global variables; can be accessed from anywhere (bad software engineering) There is no connection between the semaphore and the data being controlled by it No control over their use, no guarantee of proper usage; thus, hard to use and prone to bugs EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) 16