Introduction to Operating Systems

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

Chapter 6: Process Synchronization

Process Synchronization

Synchronization Principles

Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization

Lesson 6: Process Synchronization

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 7: Process Synchronization!

Module 6: Process Synchronization

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

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Chapter 5: Process Synchronization

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

Chapter 7: Process Synchronization. Background. Illustration

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

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

Process Synchronization

Lecture 3: Synchronization & Deadlocks

Process Synchronization

CS370 Operating Systems

Process Synchronization

Chapter 5: Process Synchronization

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization. Background

Chapter 6 Synchronization

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

Chapter 5: Process Synchronization

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

Chapter 6: Process Synchronization

CSE Opera,ng System Principles

IV. Process Synchronisation

CS370 Operating Systems

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

CS370 Operating Systems

Real-Time Operating Systems M. 5. Process Synchronization

Synchronization Principles II

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

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Lecture 5: Inter-process Communication and Synchronization

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

Chapter 6: Synchronization

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

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

Process Synchronization

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

Process Co-ordination OPERATING SYSTEMS

Process Coordination

CS420: Operating Systems. Process Synchronization

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

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

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

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

Chapter 6 Process Synchronization

Interprocess Communication By: Kaushik Vaghani

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Concurrency. Chapter 5

PESIT Bangalore South Campus

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Module 6: Process Synchronization

CS370 Operating Systems

Semaphores (by Dijkstra)

Process Synchronization

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

CSE 4/521 Introduction to Operating Systems

Process Management And Synchronization

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

Concurrency and Synchronisation

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

Dept. of CSE, York Univ. 1

Concurrency and Synchronisation

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Process Synchronization(2)

UNIT II PROCESS MANAGEMENT 9

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Prof. Hui Jiang Dept of Computer Science and Engineering York University

CSC501 Operating Systems Principles. Process Synchronization

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

$ %! 0,-./ + %/ 0"/ C (" &() + A &B' 7! .+ N!! O8K + 8 N. (Monitors) 3+!

Process Synchronization(2)

Process Synchronization(2)

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

Process Synchronization. studykorner.org

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Silberschatz and Galvin Chapter 6

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Chapter 7 Process Synchronization

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Process Synchronization

1. Motivation (Race Condition)

Dealing with Issues for Interprocess Communication

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

Transcription:

Introduction to Operating Systems Lecture 4: Process Synchronization MING GAO SE@ecnu (for course related communications) mgao@sei.ecnu.edu.cn Mar. 18, 2015

Outline 1 The synchronization problem 2 A roadmap of solutions 3 Semaphore 4 Classic problems Bounded buffer problem Readers-writers problem Dining philosophers problem More classic and non-classic problems MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 2 / 37

problem The synchronization problem Producer and consumer problem: given a buffer of size N, Producer: producer should not try to produce if buffer is full; Consumer: consumer should not try to consume if buffer is empty. Producer while (true) { /* produce an item and put in nextproduced */ while (count == BUF_SIZE) ; // do nothing buffer [in] = nextproduced; in = (in + 1) % BUF_SIZE; count++; Consumer while (true) { while (count == 0) ; // do nothing nextconsumed = buffer[out]; out = (out + 1) % BUF_SIZE; count--; /* consume the item in nextconsumed */ MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 3 / 37

problem Race condition count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count++ could be implemented as register2 = count register2 = register2-1 count = register2 Consider this execution interleaving with count = 5 initially S0: producer execute register1 = count { register1 = 5 S1: producer execute register1 = register1 + 1 { register1 = 6 S2: consumer execute register2 = count { register2 = 5 S3: consumer execute register2 = register2-1 { register2 = 4 S4: producer execute count = register1 { count = 6 S5: consumer execute count = register2 { count = 4 Race Condition A race condition occurs when two or more processes (threads) can access the same shared source and the accessed sequence of the source is significant. MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 4 / 37

problem Critical section (Critical Region) Critical section Piece of code that only one thread can execute at once. Only one thread at a time will get into this section of code. Critical section is the result of mutual exclusion Critical section and mutual exclusion are two ways of describing the same thing MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 5 / 37

problem Requirements for solving the critical section problem Mutual Exclusion (Mutex) If process P i is executing in its critical section, then no other processes can be executing in their critical sections Progress (Non-deadlock) If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely Bounded Waiting (Starvation) A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 6 / 37

roadmap The roadmap of solution Busy waiting Lock variable Strict alternation Peterson s solution Hardware solution Disable interrupt Test & set lock (TSL) Sleep & wakeup (Study offline) Semaphore Blocking queue Monitor Message passing Barrier MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 7 / 37

roadmap It s non-trivial! A naive solution if (!lock) { lock = true; // critical section lock = false; Context switch after the testing and before the locking Really hard to debug! MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 8 / 37

roadmap Second try... Lock before testing? How? Use locks with label locka = true; if (!lockb) { // critical section locka = false; lockb = true; if (!locka) { // critical section lockb = false; Context switch after the locking and before the testing Really hard to debug! MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 9 / 37

roadmap Third try... Lock before testing? How? locka = true; while (lockb); //X // critical section locka = false; lockb = true; if (!locka) { //Y /* critical section */ lockb = false; Not symmetric! at X: if B is not locked, enter CS; otherwise wait; at Y: if A is not locked, enter CS; otherwise leave Do we solve the critical section problem? Mutex ( ); Progress ( ); Bounded waiting ( ). MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 10 / 37

roadmap Strict Alternation n processes share a common variable turn(0 or 1 for two processes). If turn = i, P i is permitted to enter the critical section. Process 0 int turn = 0; while (true) { while (turn!=0); //critical section turn = 1; \ldots Process 1 int turn = 0; while (true) { while (turn!=1); //critical section turn = 0; \ldots Process i int turn = 0; while (true) { while (turn!= i); // critical section turn=(i+1)\% n; \ldots Do we solve the critical section problem? Mutex ( ); Progress ( ); Bounded waiting ( ). MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 11 / 37

roadmap Peterson s algorithm Consider two processes, the shared variables are If turn is 0, P 0 is permitted to enter the critical section, otherwise P 1. lock[i] is true when P i ready to enter its critical section. Process 0 while (true) { lock[0]=true; turn=1; while (lock[1] && turn==1); // critical section lock[0]=false; Process 1 while (true) { lock[1]=true; turn=0; while(lock[0] && turn==0); // critical section lock[1]=false; Do we solve the critical section problem? Mutex ( ); Progress ( ); Bounded waiting ( ). MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 12 / 37

roadmap Disable interrupts Naive approach LockAcquire() {disable ints; LockRelease() {enable ints; Can t let users do this! Doesn t work in multi-processing! MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 13 / 37

roadmap Test & set lock Use test& set lock init: lock = false;... while (true) { while (TestAndSet (&lock)) ; /* do nothing */ // critical section lock = false; // remainder section The instruction boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: Do we solve the critical section problem? Mutex ( ); Progress ( ); Bounded waiting ( ). MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 14 / 37

roadmap swap instruction Use swap instruction init: lock = false;... while (true) { key = true; while (key == true) Swap (&lock, &key); // critical section lock = false; // remainder section The instruction void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: Do we solve the critical section problem? Mutex ( ); Progress ( ); Bounded waiting ( ). MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 15 / 37

roadmap Busy waiting Busy waiting Thread consumes cycles while waiting: while (TestAndSet (&lock)); while (key == true) Swap (&lock, &key); Pros. Machine can receive interrupts User code can use this lock Works on a multiprocessor Cons. This is very inefficient because the busy-waiting thread will consume cycles waiting Waiting thread may take cycles away from thread holding lock (no one wins!) Priority Inversion: If busy-waiting thread has higher priority than thread holding lock, no progress! MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 16 / 37

semaphore Semaphore Semaphores are a kind of generalized lock First defined by Dijkstra in late 60s Main synchronization primitive used in original UNIX A Semaphore has a non-negative integer value and supports the following two operations: Notes P(): an atomic operation that waits for semaphore to become positive, then decrements it by 1 (the wait() operation) V(): an atomic operation that increments the semaphore by 1, waking up a waiting P, if any (the signal() operation) Note that P() stands for proberen (to test) and V() stands for verhogen (to increment) in Dutch MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 17 / 37

semaphore Semaphore as general synchronization tool Counting semaphore integer value can range over an unrestricted domain Binary semaphore integer value can range only between 0 and 1; can be simpler to implement Also known as mutex locks Can implement a counting semaphore S as a binary semaphore or a synchronized semaphore How? Provides synchronization semaphore S = 0; For process P1, S1; signal (S); For process P2, wait (S); S2; Provides mutual exclusion Semaphore S = 1; wait (S); // Critical Section signal (S); MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 18 / 37

semaphore Semaphore implementation Requirement Must guarantee that no two processes can execute wait() and signal() on the same semaphore at the same time Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section. Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Notes Note that applications may spend lots of time in critical sections and therefore this is not a good solution. MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 19 / 37

semaphore Semaphore impl. w/o busy waiting With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: value (of type integer) pointer to next record in the list Two operations block place the process invoking the operation on the appropriate waiting queue. wakeup remove one of processes in the waiting queue and place it in the ready queue. MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 20 / 37

semaphore Semaphore impl. w/o busy waiting Implementation of semaphore with block/wakeup wait (S) { value--; if (value < 0) { /* add this process to waiting queue */ block(); signal (S) { value++; if (value <= 0) { /* remove a process P from the waiting queue */ wakeup(p); MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 21 / 37

semaphore Deadlocks and starvation Deadlock Two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes P0 wait (S); wait (Q);... P1 wait (Q); wait (S);... signal (S); signal (Q); signal (Q); signal (S); Starvation Indefinite blocking: A process may never be removed from the semaphore queue in which it is suspended. MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 22 / 37

Outline classics Bounded buffer problem 1 The synchronization problem 2 A roadmap of solutions 3 Semaphore 4 Classic problems Bounded buffer problem Readers-writers problem Dining philosophers problem More classic and non-classic problems MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 23 / 37

classics Bounded buffer problem Bounded buffer problem The bounded buffer problem N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N. Producer while (true) { // produce an item wait (empty); wait (mutex); // add it to the buffer signal (mutex); signal (full); Consumer while (true) { wait (full); wait (mutex); // remove one from buffer signal (mutex); signal (empty); // consume the removed item MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 24 / 37

Outline classics Readers-writers problem 1 The synchronization problem 2 A roadmap of solutions 3 Semaphore 4 Classic problems Bounded buffer problem Readers-writers problem Dining philosophers problem More classic and non-classic problems MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 25 / 37

classics Readers-writers problem Readers-writers problem A data set is shared among a number of concurrent processes Readers only read the data set; they do not perform any updates Writers can both read and write. The readers-writers problem Allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time. Shared Data Data set Semaphore mutex initialized to 1. Semaphore wrt initialized to 1. Integer readcount initialized to 0. MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 26 / 37

classics Readers-writers problem Readers-writers problem A solution Writer while (true) { wait (wrt); // writing is performed signal (wrt); Reader while (true) { wait (mutex); readcount ++; if (readcount == 1) wait (wrt); signal (mutex) // reading is performed wait (mutex); readcount --; if (readcount == 0) signal (wrt); signal (mutex) ; MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 27 / 37

classics Readers-writer problem discussion Readers-writers problem Writers may starve when too many readers! Homework Write a program using semaphore for readers-writers problem, in which reader must wait when some writer is waiting for the wrt semaphore. MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 28 / 37

Outline classics Dining philosophers problem 1 The synchronization problem 2 A roadmap of solutions 3 Semaphore 4 Classic problems Bounded buffer problem Readers-writers problem Dining philosophers problem More classic and non-classic problems MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 29 / 37

classics Dining philosophers problem Dining philosophers problem The problem MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 30 / 37

classics Dinining philosophers problem Dining philosophers problem Solution A while (true) { wait (chopstick [i]); wait (chopstick [(i + 1) % 5]); // eat signal (chopstick [i] ); signal (chopstick [(i + 1) % 5]); // think Solution B while (true) { wait(mutex); wait (chopstick [i]); wait (chopstick [(i + 1) % 5]); signal(mutex); // eat signal (chopstick [i] ); signal (chopstick [(i + 1) % 5]); // think MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 31 / 37

classics Dining philosophers problem Dinining philosophers problem A solution with monitor monitor DP { enum (THINKING, HUNGRY, EATING) state [5]; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i]!= EATING) self[i].wait; void putdown (int i) { state[i] = THINKING; /* test left and right neighbors */ test((i + 4) % 5); test((i + 1) % 5); void test (int i) { if ( (state[(i + 4) % 5]!= EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5]!= EATING)) { state[i] = EATING; self[i].signal (); initialization_code() { for (int i=0; i < 5; i++) state[i] = THINKING; MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 32 / 37

classics Dining philosophers problem Dining philosophers problem A solution with monitor dp.pickup (i); // eat dp.putdown (i); MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 33 / 37

Outline classics More classic and non-classic problems 1 The synchronization problem 2 A roadmap of solutions 3 Semaphore 4 Classic problems Bounded buffer problem Readers-writers problem Dining philosophers problem More classic and non-classic problems MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 34 / 37

classics More classic and non-classic problems More classic and non-classic problems Cigarette smokers problem The dining savages problem The barbershop problem Hilzers Barbershop problem The Santa Claus problem... A reference Allen B. Downey: The Little Book of Semaphores. http: //www.greenteapress.com/semaphores/downey08semaphores.pdf MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 35 / 37

classics More classic and non-classic problems Monitor implementation using semaphore semaphore mutex; // init: 1 semaphore next; // init: 0 int next_count = 0; Each funtion F will be replaced by wait (mutex); // body of F; if (next_count > 0) signal (next) else signal (mutex); For each condition variable x semaphore x_sem; // init: 0 int x_count = 0; For operations of x.wait and x.signal x_count++; if (next_count > 0) signal (next); else signal (mutex); wait (x_sem); x_count--; if (x_count>0) { next_count++; signal (x_sem); wait (next); next_count--; MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 36 / 37

Take-home msg. Acknowledgement Many slides are copied or adapted from: Slides provided by authors of the textbook (http://codex.cs.yale.edu/avi/os-book/os7/) Prof. Anthony D. Joseph s slides for course CS162 (2006) at EECS@UCBerkeley (http://inst.eecs.berkeley.edu/~cs162) (Copyright @ 2008 UCB) MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, 2015 37 / 37