Introduction to Operating Systems

Size: px
Start display at page:

Download "Introduction to Operating Systems"

Transcription

1 Introduction to Operating Systems Lecture 4: Process Synchronization MING GAO (for course related communications) Mar. 18, 2015

2 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, / 37

3 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, / 37

4 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, / 37

5 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, / 37

6 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, / 37

7 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, / 37

8 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, / 37

9 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, / 37

10 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, / 37

11 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, / 37

12 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, / 37

13 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, / 37

14 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, / 37

15 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, / 37

16 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, / 37

17 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, / 37

18 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, / 37

19 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, / 37

20 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, / 37

21 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, / 37

22 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, / 37

23 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, / 37

24 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, / 37

25 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, / 37

26 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, / 37

27 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, / 37

28 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, / 37

29 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, / 37

30 classics Dining philosophers problem Dining philosophers problem The problem MING GAO Introduction to Operating Systems Mar. 18, / 37

31 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, / 37

32 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, / 37

33 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, / 37

34 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, / 37

35 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: // MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, / 37

36 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, / 37

37 Take-home msg. Acknowledgement Many slides are copied or adapted from: Slides provided by authors of the textbook ( Prof. Anthony D. Joseph s slides for course CS162 (2006) at EECS@UCBerkeley ( UCB) MING GAO (SE@ecnu) Introduction to Operating Systems Mar. 18, / 37

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

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6. Part Three - Process Coordination Chapter 6: Synchronization 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson s Solution 6.4 Synchronization Hardware 6.5 Mutex Locks 6.6 Semaphores 6.7 Classic

More information

Process Synchronization

Process Synchronization Process Synchronization Chapter 6 2015 Prof. Amr El-Kadi Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly

More information

Synchronization Principles

Synchronization Principles Synchronization Principles Gordon College Stephen Brinton The Problem with Concurrency Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms

More information

Process Synchronization

Process Synchronization CS307 Process Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2018 Background Concurrent access to shared data may result in data inconsistency

More information

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization Chapter 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Process Synchronization

Process Synchronization CSC 4103 - Operating Systems Spring 2007 Lecture - VI Process Synchronization Tevfik Koşar Louisiana State University February 6 th, 2007 1 Roadmap Process Synchronization The Critical-Section Problem

More information

Lesson 6: Process Synchronization

Lesson 6: Process Synchronization Lesson 6: Process Synchronization Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

CHAPTER 6: PROCESS SYNCHRONIZATION

CHAPTER 6: PROCESS SYNCHRONIZATION CHAPTER 6: PROCESS SYNCHRONIZATION The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. TOPICS Background

More information

Chapter 7: Process Synchronization!

Chapter 7: Process Synchronization! Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors 7.1 Background Concurrent access to shared

More information

Module 6: Process Synchronization

Module 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic

More information

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

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Process Synchronization, Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

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

Chapter 6: Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Synchronization, Silberschatz, Galvin and Gagne 2009 Outline Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang Process Synchronization CISC3595, Spring 2015 Dr. Zhang 1 Concurrency OS supports multi-programming In single-processor system, processes are interleaved in time In multiple-process system, processes execution

More information

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Operating System Concepts 9th Edition Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Chapter 7: Process Synchronization. Background. Illustration

Chapter 7: Process Synchronization. Background. Illustration Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Objectives Introduce Concept of Critical-Section Problem Hardware and Software Solutions of Critical-Section Problem Concept of Atomic Transaction Operating Systems CS

More information

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

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Process Synchronization

Process Synchronization TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Process Synchronization [SGG7] Chapter 6 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin

More information

Lecture 3: Synchronization & Deadlocks

Lecture 3: Synchronization & Deadlocks Lecture 3: Synchronization & Deadlocks Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating

More information

Process Synchronization

Process Synchronization Chapter 7 Process Synchronization 1 Chapter s Content Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors 2 Background

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 12 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ 2 Mutex vs Semaphore Mutex is binary,

More information

Process Synchronization

Process Synchronization Process Synchronization Daniel Mosse (Slides are from Silberschatz, Galvin and Gagne 2013 and Sherif Khattab) Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution

More information

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013! Chapter 5: Process Synchronization Background" The Critical-Section Problem" Petersons Solution" Synchronization Hardware" Mutex

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Module 6: Process Synchronization Chapter 6: Process Synchronization Background! The Critical-Section Problem! Peterson s Solution! Synchronization Hardware! Semaphores! Classic Problems of Synchronization!

More information

Chapter 7: Process Synchronization. Background

Chapter 7: Process Synchronization. Background Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris

More information

Chapter 6 Synchronization

Chapter 6 Synchronization Chapter 6 Synchronization Da-Wei Chang CSIE.NCKU Source: Abraham Silberschatz, Peter B. Galvin, and Greg Gagne, "Operating System Concepts", 9th Edition, Wiley. 1 Outline Background The Critical-Section

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

CSE Opera,ng System Principles

CSE Opera,ng System Principles CSE 30341 Opera,ng System Principles Synchroniza2on Overview Background The Cri,cal-Sec,on Problem Peterson s Solu,on Synchroniza,on Hardware Mutex Locks Semaphores Classic Problems of Synchroniza,on Monitors

More information

IV. Process Synchronisation

IV. Process Synchronisation IV. Process Synchronisation Operating Systems Stefan Klinger Database & Information Systems Group University of Konstanz Summer Term 2009 Background Multiprogramming Multiple processes are executed asynchronously.

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L11 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel feedback queue:

More information

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

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology Process Synchronization: Semaphores CSSE 332 Operating Systems Rose-Hulman Institute of Technology Critical-section problem solution 1. Mutual Exclusion - If process Pi is executing in its critical section,

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 11 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel Feedback Queue: Q0, Q1,

More information

Real-Time Operating Systems M. 5. Process Synchronization

Real-Time Operating Systems M. 5. Process Synchronization Real-Time Operating Systems M 5. Process Synchronization Notice The course material includes slides downloaded from: http://codex.cs.yale.edu/avi/os-book/ and (slides by Silberschatz, Galvin, and Gagne,

More information

Synchronization Principles II

Synchronization Principles II CSC 256/456: Operating Systems Synchronization Principles II John Criswell University of Rochester 1 Synchronization Issues Race conditions and the need for synchronization Critical Section Problem Mutual

More information

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

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on Chapter 6: Process Synchronization Operating System Concepts 9 th Edit9on Silberschatz, Galvin and Gagne 2013 Objectives To present the concept of process synchronization. To introduce the critical-section

More information

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

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University Frequently asked questions from the previous class survey CS 370: SYSTEM ARCHITECTURE & SOFTWARE [PROCESS SYNCHRONIZATION] Shrideep Pallickara Computer Science Colorado State University Semaphores From

More information

Lecture 5: Inter-process Communication and Synchronization

Lecture 5: Inter-process Communication and Synchronization Lecture 5: Inter-process Communication and Synchronization Real-Time CPU Scheduling Periodic processes require the CPU at specified intervals (periods) p is the duration of the period d is the deadline

More information

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

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture) EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture) Dept. of Computer Science & Engineering Chentao Wu wuct@cs.sjtu.edu.cn Download lectures ftp://public.sjtu.edu.cn User:

More information

Chapter 6: Synchronization

Chapter 6: Synchronization Chapter 6: Synchronization Module 6: Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

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

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution Race Condition Synchronization CSCI 315 Operating Systems Design Department of Computer Science A race occurs when the correctness of a program depends on one thread reaching point x in its control flow

More information

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

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } Semaphore Semaphore S integer variable Two standard operations modify S: wait() and signal() Originally called P() and V() Can only be accessed via two indivisible (atomic) operations wait (S) { while

More information

Process Synchronization

Process Synchronization Process Synchronization Concurrent access to shared data may result in data inconsistency Multiple threads in a single process Maintaining data consistency requires mechanisms to ensure the orderly execution

More information

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

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Process Synchronization, Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

Process Co-ordination OPERATING SYSTEMS

Process Co-ordination OPERATING SYSTEMS OPERATING SYSTEMS Prescribed Text Book Operating System Principles, Seventh Edition By Abraham Silberschatz, Peter Baer Galvin and Greg Gagne 1 PROCESS - CONCEPT Processes executing concurrently in the

More information

Process Coordination

Process Coordination Process Coordination Why is it needed? Processes may need to share data More than one process reading/writing the same data (a shared file, a database record, ) Output of one process being used by another

More information

CS420: Operating Systems. Process Synchronization

CS420: Operating Systems. Process Synchronization Process Synchronization James Moscola Department of Engineering & Computer Science York College of Pennsylvania Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne Background

More information

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

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling. Background The Critical-Section Problem Background Race Conditions Solution Criteria to Critical-Section Problem Peterson s (Software) Solution Concurrent access to shared data may result in data inconsistency

More information

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

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy Operating Systems Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : eaymanelshenawy@yahoo.com Reference

More information

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

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Thread Scheduling Operating Systems Examples Java Thread Scheduling Algorithm Evaluation CPU

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Chapter 6 Process Synchronization

Chapter 6 Process Synchronization Chapter 6 Process Synchronization Cooperating Process process that can affect or be affected by other processes directly share a logical address space (threads) be allowed to share data via files or messages

More information

Interprocess Communication By: Kaushik Vaghani

Interprocess Communication By: Kaushik Vaghani Interprocess Communication By: Kaushik Vaghani Background Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of execution depends on the

More information

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

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background Module 6: Process Synchronization Background Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization

More information

Concurrency. Chapter 5

Concurrency. Chapter 5 Concurrency 1 Chapter 5 2 Concurrency Is a fundamental concept in operating system design Processes execute interleaved in time on a single processor Creates the illusion of simultaneous execution Benefits

More information

PESIT Bangalore South Campus

PESIT Bangalore South Campus INTERNAL ASSESSMENT TEST II Date: 04/04/2018 Max Marks: 40 Subject & Code: Operating Systems 15CS64 Semester: VI (A & B) Name of the faculty: Mrs.Sharmila Banu.A Time: 8.30 am 10.00 am Answer any FIVE

More information

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019 CS370 Operating Systems Midterm Review Yashwant K Malaiya Spring 2019 1 1 Computer System Structures Computer System Operation Stack for calling functions (subroutines) I/O Structure: polling, interrupts,

More information

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Process Synchronisation (contd.) Operating Systems. Autumn CS4023 Operating Systems Autumn 2017-2018 Outline Process Synchronisation (contd.) 1 Process Synchronisation (contd.) Synchronization Hardware 6.4 (SGG) Many systems provide hardware support for critical section

More information

Module 6: Process Synchronization

Module 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1019 L12 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Critical section: shared

More information

Semaphores (by Dijkstra)

Semaphores (by Dijkstra) CSCI 4401 Principles of Operating Systems I Process Synchronization II: Classic Problems Vassil Roussev vassil@cs.uno.edu Semaphores (by Dijkstra) A higher-level way of doing synchronization between threads/processes

More information

Process Synchronization

Process Synchronization Process Synchronization Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Process Synchronization 1 / 28 Cooperating processes Reference: Section 4.4. Cooperating process: shares data with other

More information

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

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code Old Producer Process Code Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Our

More information

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems Synchronization CS 475, Spring 2018 Concurrent & Distributed Systems Review: Threads: Memory View code heap data files code heap data files stack stack stack stack m1 m1 a1 b1 m2 m2 a2 b2 m3 m3 a3 m4 m4

More information

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University 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

More information

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

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition Module 6: Process Synchronization 6.1 Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

CSE 4/521 Introduction to Operating Systems

CSE 4/521 Introduction to Operating Systems CSE 4/521 Introduction to Operating Systems Lecture 7 Process Synchronization II (Classic Problems of Synchronization, Synchronization Examples) Summer 2018 Overview Objective: 1. To examine several classical

More information

Process Management And Synchronization

Process Management And Synchronization Process Management And Synchronization In a single processor multiprogramming system the processor switches between the various jobs until to finish the execution of all jobs. These jobs will share the

More information

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

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2. Learning Outcomes Concurrency and Synchronisation Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Sections 2.3 & 2.4 Textbook 2 Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable 3 Inter- Thread and Process Communication

More information

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

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) Synchronization CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) 1 Outline Critical region and mutual exclusion Mutual exclusion using busy waiting Sleep and

More information

Dept. of CSE, York Univ. 1

Dept. of CSE, York Univ. 1 EECS 3221.3 Operating System Fundamentals No.5 Process Synchronization(1) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Background: cooperating processes with shared

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Learning Outcomes Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

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

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem. CSE 421/521 - Operating Systems Fall 2011 Lecture - X Process Synchronization & Deadlocks Roadmap Classic Problems of Synchronization Readers and Writers Problem Dining-Philosophers Problem Sleeping Barber

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

UNIT II PROCESS MANAGEMENT 9

UNIT II PROCESS MANAGEMENT 9 UNIT II PROCESS MANAGEMENT 9 Processes-Process Concept, Process Scheduling, Operations on Processes, Interprocess Communication; Threads- Overview, Multicore Programming, Multithreading Models; Windows

More information

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

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements HW #3 is coming, due Friday Feb. 25, a week+ from now PA #2 is coming, assigned about next Tuesday Midterm is tentatively

More information

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

Prof. Hui Jiang Dept of Computer Science and Engineering York University 0./ ' )-, ' ' # # 2 H; 2 7 E 7 2 $&% ( Prof. Hui Jiang ept of omputer Science and Engineering York University )+* Problems with the software solutions. Not easy to generalize to more complex synchronization

More information

CSC501 Operating Systems Principles. Process Synchronization

CSC501 Operating Systems Principles. Process Synchronization CSC501 Operating Systems Principles Process Synchronization 1 Last Lecture q Process Scheduling Question I: Within one second, how many times the timer interrupt will occur? Question II: Within one second,

More information

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

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University CSC 4103 - Operating Systems Fall 2009 Lecture - XI Deadlocks - II Tevfik Ko!ar Louisiana State University September 29 th, 2009 1 Roadmap Classic Problems of Synchronization Bounded Buffer Readers-Writers

More information

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

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko! CSC 4103 - Operating Systems Fall 2009 Lecture - XI Deadlocks - II Roadmap Classic Problems of Synchronization Bounded Buffer Readers-Writers Dining Philosophers Sleeping Barber Deadlock Prevention Tevfik

More information

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

$ %! 0,-./ + %/ 0/ C ( &() + A &B' 7! .+ N!! O8K + 8 N. (Monitors) 3+! "#! $ %! *+ &' & "-/ &+, :/!! &+ 8/!.!"!! > &, < &+ 7!! "-/ :/= +. :/= 0 0/ 7!@?+ C (" & + A &B' 7! G' 3/ 0"=' 7> H 0 4 0! &D E.! 0 n I"2 &+ % &B' 0!!! n 1 + counter J " &B' & K n

More information

Process Synchronization(2)

Process Synchronization(2) CSE 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Computer Science and Engineering York University Semaphores Problems with the software solutions. Not easy

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Objectives To present the concept of process synchronization. To introduce the critical-section problem, whose solutions can be used

More information

Process Synchronization. studykorner.org

Process Synchronization. studykorner.org Process Synchronization Semaphore Implementation Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time The main disadvantage of the semaphore definition

More information

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Processes Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Processes Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication Process Concept Early

More information

Silberschatz and Galvin Chapter 6

Silberschatz and Galvin Chapter 6 Silberschatz and Galvin Chapter 6 Process Synchronization CPSC 410--Richard Furuta 2/26/99 1 Topics discussed Process synchronization Mutual exclusion--hardware Higher-level abstractions Ð Semaphores Ð

More information

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

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008 Process Synchronization Mehdi Kargahi School of ECE University of Tehran Spring 2008 Producer-Consumer (Bounded Buffer) Producer Consumer Race Condition Producer Consumer Critical Sections Structure of

More information

Chapter 7 Process Synchronization

Chapter 7 Process Synchronization Contents 1. Introduction 2. Computer-System Structures 3. Operating-System Structures 4. Processes 5. Threads 6. CPU Scheduling 7. Process Synchronization 8. Deadlocks 9. Memory Management 10. Virtual

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst Clicker Question #1 If we have 100 threads and 100 processors, how often does Hello get printed? (A) 100 times per

More information

Process Synchronization

Process Synchronization Process Synchronization Reading: Silberschatz chapter 6 Additional Reading: Stallings chapter 5 EEL 358 1 Outline Concurrency Competing and Cooperating Processes The Critical-Section Problem Fundamental

More information

1. Motivation (Race Condition)

1. Motivation (Race Condition) COSC4740-01 Operating Systems Design, Fall 2004, Byunggu Yu Chapter 6 Process Synchronization (textbook chapter 7) Concurrent access to shared data in the data section of a multi-thread process, in the

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

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

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212 Operating Systems Spring 2009-2010 Outline Process Synchronisation (contd.) 1 Process Synchronisation (contd.) 2 Announcements Presentations: will be held on last teaching week during lectures make a 20-minute

More information