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

Similar documents
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

Condition Variables. Dongkun Shin, SKKU

W4118 Operating Systems. Instructor: Junfeng Yang

Synchroniza+on II COMS W4118

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

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

[537] Semaphores. Tyler Harter

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

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

30. Condition Variables

Semaphores Semaphores: A Definition

Condition Variables. parent: begin child parent: end

[537] Concurrency Bugs. Tyler Harter

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Process Synchronization. studykorner.org

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Figure 30.1: A Parent Waiting For Its Child What we would like to see here is the following output:

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

Synchronization Mechanisms

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

Final review. From threads to file systems

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

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

KING FAHD UNIVERSITY OF PETROLEUM & MINERALS. Information and Computer Science Department. ICS 431 Operating Systems. Lab # 9.

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

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

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

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

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Dealing with Issues for Interprocess Communication

Synchronization Primitives

Operating System Labs. Yuanbin Wu

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

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

Process Synchronization(2)

Concurrency and Synchronisation

Dining Philosophers, Semaphores

Locks and semaphores. Johan Montelius KTH

Process Synchronization

Week 3. Locks & Semaphores

Concurrency and Synchronisation

Process Synchronization(2)

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

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

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

Chapter 7: Process Synchronization!

Locks and semaphores. Johan Montelius KTH

PROCESS SYNCHRONIZATION

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

Chapter 6: Process Synchronization

CS3502 OPERATING SYSTEMS

Process Synchronization(2)

Topics: Process (Thread) Synchronization (SGG, Ch 5 in 9th Ed Ch 6 in Old Ed) (USP, Chapter 13-14) CS 3733 Operating Systems

5 Classical IPC Problems

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

CMSC421: Principles of Operating Systems

Operating systems and concurrency (B08)

Chapter 6: Process Synchronization

Lecture 8: September 30

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

CS 153 Lab6. Kishore Kumar Pusukuri

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Introduction to Operating Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

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

Chapter 6 Process Synchronization

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

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

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

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

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Concurrency. Chapter 5

Process Synchronization

IV. Process Synchronisation

Condition Variables & Semaphores

Operating systems. Lecture 12

Interprocess Communication and Synchronization

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

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

Silberschatz and Galvin Chapter 6

Chapter 7: Process Synchronization. Background. Illustration

Multithreading Programming II

Process Synchronization

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

Synchronization Principles

Concurrency: a crash course

Synchronization Principles II

Process Synchronization

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

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

ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT

CHAPTER 6: PROCESS SYNCHRONIZATION

Concurrent Programming

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

Transcription:

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

Condition variables Many times a thread wants to check whether a condition is true before continuing execution A parent waiting on a child A consumer waiting on something to consume Spinning on a shared variable is, as we know, is inefficient Condition variables An explicit queue where threads can put themselves on when some state is not what they want (waiting on a change) Until some other thread changes the state and informs them of it, signaling on the condition pthread_cond_t c; pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m); pthread_cond_signal(pthraed_cond_t *c); 2

Waiting on your child Before an example, did you notice? pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m); Assumes mutex is locked before wait is called Wait must release it and put the thread to sleep, atomically When the thread wakes up, re-acquires the lock before returning All this to prevent race conditions when the thread is trying to put itself to sleep Back to parent and child int done = 0; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t c = PTHREAD_COND_INITIALIZER; int main(int argc, char *argv[]) { pthread_t p; printf( parent: begin\n ); pthread_crate(&p, NULL, child, NULL); thr_join(); printf( parent: done\n ); return 0; 3

Waiting on your child void thr_exit() { pthread_mutex_lock(&m); done = 1; pthread_cond_signal(&c); pthread_mutex_unlock(&m); void *child(void *arg) { printf( child\n ); thr_exit(); return 0; void thr_join() { pthread_mutex_lock(&m); while (done == 0) pthread_cond_wait(&c, &m); pthread_mutex_unlock(&m); That while doesn t seem strictly necessary, wouldn t an if do wait a bit Two cases to consider Parent creates the child and continue running Gets the lock, check if done and put itself to sleep Child eventually runs, gets the lock, sets done and signals the parent Parent returns from the wait with the lock held, unlocks it and is done If child runs first, sets done, signals (nobody is waiting) and returns; parent check child is done and returns 4

Non-working approaches void thr_exit() { pthread_mutex_lock(&m); pthread_cond_signal(&c); pthread_mutex_unlock(&m); void thr_join() { pthread_mutex_lock(&m); pthread_cond_wait(&c); pthread_mutex_unlock(&m) void thr_exit() { done = 1; pthread_cond_signal(&c); void thr_join() { if (done == 0) pthread_cond_wait(&c); Do you need done? If the child runs immediately, the signal will be lost Parent will go to sleep for ever Do you need that mutex? What would happen if the parent is interrupted after checking done but before going to sleep on wait? Child runs, signals nobody (parent is not there yet) and.. When parent continues it goes to sleep, for ever 5

Producer/consumer problem Producer-consumer problem, aka bounded buffer Two or more processes & one shared, fixed-size buffer Some put data times into a buffer, others one takes them out E.g., Web server with producers taken orders and consumer threads processing them int buff[max]; int fill = 0; int use = 0; int count = 0; void put(int value) { buff[fill] = value; fill = (fill + 1) % MAX; count++; int get() { int tmp = buffer[use]; use = (use + 1) & MAX; count--; return tmp; producer producer put() buff[max] get() consumer consumer 6

Producer/consumer problem Simple solution If buffer empty, producer goes to sleep to be awaken when the consumer has removed one or more items Similarly for the consumer cond_t cond; mutex_t mutex; void *producer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); if (count == MAX) pthread_cond_wait(&cond, &mutex); put(i); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); if (count == 0) pthread_cond_wait(&cond, &mutex); int tmp = get(i); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); 7

A while for an if cond_t cond; mutex_t mutex; void *producer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); if (count == MAX) pthread_cond_wait(&cond, &mutex); put(i); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); if (count == 0) pthread_cond_wait(&cond, &mutex); int tmp = get(i); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); 2 consumers/1 producer Consumer 1 tries to get item but finds buffer empty, signals producer Producer puts an item and signals this, moving C1 to ready queue Consumer 2 comes along and gets the one item Now C1 runs; just before returning from the wait it re-acquires the lock, returns and calls get to find an empty buffer 8

One condition variable is not enough cond_t cond; mutex_t mutex; void *producer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); while (count == MAX) pthread_cond_wait(&cond, &mutex); put(i); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); while (count == 0) pthread_cond_wait(&cond, &mutex); int tmp = get(i); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); 2 consumers/1 producer and lets MAX = 1 Both consumers try to get the item, find buffer empty and go to sleep Producer puts item, wakes up a consumer (1) and goes to sleep Consumer comes along and gets the one item and signals but who? Both producer and Consumer 2 are sleeping 9

Finally a solution cond_t empty, fill; mutex_t mutex; Simple solution two condition variables void *producer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); Producer waits on empty while (count == MAX) and signals fill pthread_cond_wait(&empty, &mutex); put(i); pthread_cond_signal(&fill); pthread_mutex_unlock(&mutex); void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { pthraed_mutex_lock(&mutex); while (count == 0) pthread_cond_wait(&fill, &mutex); int tmp = get(i); pthread_cond_signal(&empty); pthread_mutex_unlock(&mutex); Consumers do the opposite wait on fill and signal empty 10

Semaphores A synchronization primitive Higher level of abstraction than locks, also replacing condition variables Invented by Dijkstra in 68 as part of THE operating system Atomically manipulated by two operations sem_wait(sem_t *sem) / P / down(sem) sem_post(sem_t *sem) / V / up(sem) The initial value determine its behavior, so it must be first initialized sem_init(sem_t *sem, int pshared, unsigned int value); Ignored this for now, basically shared by al threads of a process (0) or by processes through shared memory (=0) 11

Blocking in semaphores Each semaphore has an associated queue of processes/threads sem_wait / P If sem was available (>0), decrement sem & let thread continue If sem was unavailable (<=0), decrement it by 1, place thread on associated queue P not really for proberen or passeer but for a made-up word prolaag try to reduce int sem_wait(sem_t *s){ wait in a queue of s until (s.value > 0); s.value--; } Atomic action 12

Semaphores sem_post / V If thread(s) are waiting on the queue, unblock one If no threads are waiting, increment sem The signal is remembered for next time up(sem) is called Might as well let the up-ing thread continue execution V verhogen increase in Dutch int sem_post(sem_t *s) { s.value++; if there are 1+ threads waiting wake one thread up; } Atomic action 13

Binary semaphores - locks sem_t m; sem_init(&m, 0, 1); sem_wait(&m); /* critical section */ sem_post(&m); Why 1? Look at the definition of wait and post int sem_wait(sem_t *s){ wait in a queue of s until (s.value > 0); s.value--; } int sem_post(sem_t *s) { s.value++; if there are 1+ threads waiting wake one thread up; } So, if m = 1 the first thread will go in and decrement its value, the following thread will wait until the thread inside increments it inside post 14

Semaphores as condition variables Waiting on a condition, as when parent waits for child to terminate sem_t s; void *child(void *arg) { printf( child\n ); sem_post(&s); return NULL; int main(int argc, char*argv[]) { Why 0? sem_init(&s, 0, 0); printf( parent: begin\n ); pthread_t c; pthread_crate(c, NULL, child, NULL); sem_wait(&s); printf( parent: end\n ); return 0; So, if m = 0 and parent runs, will wait until the child runs and sets value to 1; If child runs first, the value will be 1 and the parent will go on without waiting 15

Semaphores Producer/consumer v1 sem_t empty; sem_t full; sem_t mutex; void *producer(void *arg) { int i; Yeap, those are CSs for (i = 0; i < loops; i++) { sem_wait(&empty); put(i); sem_post(&full); void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { sem_wait(&full); int tmp = get(); sem_post(&empty); int main... sem_init(&empty, 0, MAX); /* MAX buffers are empty... */ sem_init(&full, 0, 0); /* and 0 are full */... void put(int value) { buff[fill] = value; fill = (fill + 1) % MAX; int get() { int tmp = buffer[use]; use = (use + 1) & MAX; return tmp; 16

Semaphores Producer/consumer sem_t empty; sem_t full; sem_t mutex; void *producer(void *arg) { int i; for (i = 0; i < loops; i++) { sem_wait(&empty); sem_wait(&mutex); Protect the critical section put(i); sem_post(&mutex); sem_post(&full); void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { sem_wait(&full); Protect the critical section sem_wait(&mutex); int tmp = get(); sem_post(&mutex); sem_post(&empty); int main... sem_init(&empty, 0, MAX); /* MAX buffers are empty... */ sem_init(&full, 0, 0); /* and 0 are full */ sem_init(&mutex, 0, 1); /* set to 1, it s a lock */... 17

Watch for deadlocks Semaphores solves most synchronization problems But, no control over their use, no guarantee of proper usage Minor change? void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { sem_wait(&full); sem_wait(&mutex); int tmp = get(); sem_post(&mutex); sem_post(&empty); void *producer(void *arg) { int i; for (i = 0; i < loops; i++) { sem_wait(&empty); sem_wait(&mutex); put(i); sem_post(&mutex); sem_post(&full); void *consumer(void *arg) { int i; for (i = 0; i < loops; i++) { sem_wait(&mutex); sem_wait(&full); int tmp = get(); sem_post(&empty); sem_post(&mutex); void *producer(void *arg) { int i; for (i = 0; i < loops; i++) { sem_wait(&mutex); sem_wait(&empty); put(i); sem_post(&full); sem_post(&mutex); Deadlock Consumer holds the mutex and goes to sleep, to wait for the producer to put something Producer can t put anything because consumer holds the lock 18

Readers-writers problem The need for a more flexible type of lock, imagine a database or a simple linked list Not problem with multiple readers allowed at once Only one writer allowed at a time If writers is in, nobody else is First reader blocks the writer from entering typedef struct _rwlock_t { sem_t lock; sem_t writelock; int readers; } rwlock_t; void rwlock_acquire_writelock(rwlock_t *rw) { sem_wait(&rw->writelock); void rwlock_release_writelock(rwlock_t *rw) { sem_post(&rw->writelock); Simple, only a single writer allowed void rwlock_acquire_readlock(rwlock_t *rw) { sem_wait(&rw->lock); rw->readers++; if (rw->readers == 1) sem_wait(&rw->writelock); sem_post(&rw->lock); void rwlock_release_readlock(rwlock_t *rw) { sem_wait(&rw->lock); rw->readers--; if (rw->readers == 0) sem_post(&rw->writelock); sem_post(&rw->lock); Last reader lets the writer in 19

Dining philosophers Another one by Dijkstra Philosophers eat/think To eat, a philosopher needs 2 chopsticks Picks one at a time How to prevent deadlock and starvation #define N 5 void philosopher(int i) { while (TRUE) { think(); take_chopstick(i); take_chopstick((i+1)%n); eat(); put_chopstick(i); put_chopstick((i+1)%n); Nonsolution Now: Everybody takes the left chopstick Why not just protect all this with a mutex? 20

Dining philosophers example state[] too keep track of philosopher s (eating, thinking, hungry) s[] array of semaphores, one per philosopher state void philosopher(int i) { while(true) { think(); take_chopstick(i); eat(); put_chopstick(i); void take_chopstick(int i) { sem_wait(&mutex); state[i] = HUNGRY; test(i); sem_post(&mutex); sem_wait(&s[i]); void put_chopstick(int i) { sem_wait(&mutex); state[i] = THINKING; test(left); test(right); sem_post(&mutex); void test(int i) { if ((state[i] == hungry && state[left] = eating && state[right] = eating) { state[i] = EATING; up(&s[i]); 21

Issues with semaphores Solves most synchronization problems, but: We have seen, no control over their use, no guarantee of proper usage (our deadlock example) Also Semaphores are essentially shared global variables Can be accessed from anywhere (bad software engineering) No connection bet/ the semaphore & the data controlled by it Used for both critical sections & for coordination (scheduling) 22

Monitors Monitors - higher level synchronization primitive A programming language construct Collection of procedures, variables and data structures Monitor s internal data structures are private Monitors and mutual exclusion Only one process active at a time - how? Synchronization code is added by the compiler (or the programmer using locks) To enforce sequences of events Condition variables Only accessed from within the monitor Three operations wait, signal & broadcast 23

Monitors Wait Atomically releases the lock Suspends execution of the calling thread, place it in the waiting queue When the calling thread is re-enable, it requires the lock before returning from the wait A thread that waits steps outside the monitor (to the associated wait queue) A condition variable is memoryless, it has no internal state (the shared object defines its own); so, wait is not a counter signal may get lost 24

Monitors Signal Takes one waiting thread off the condition variable s waiting queue and marks it as eligible to run Broadcast Like signal but for all threads waiting What happen after the signal? Hoare process awakened run, the other one is suspended Brinch Hansen process signaling must exit the monitor Mesa process signaling continues to run As a programmer always check the condition after being woken i.e., call within a loop while (predicateonstatevar( )) { wait(&lock); } 25

Monitors and semaphores Clear similarities between the two you can use one to implement the other A semaphore as a monitor monitor class Semaphore { int s; Semaphore(int value) { s = value; void wait() { while (s <= 0) wait(); s--; void post() { s++; signal(); }; Using it as a binary semaphore Semaphore s(1); s.wait(); /* Critical section */ s.post(); 26

Coming up Deadlocks How deadlock arise and what you can do about them 27