Process synchronization

Size: px
Start display at page:

Download "Process synchronization"

Transcription

1 Process synchronization Frédéric Haziza Department of Computer Systems Uppsala University Spring 2008

2 Shared Resource Remark Sequential program use shared variables Convenience for Global data structure Necessity Concurrent program MUST use shared components The only way to solve a problem: Communicate The only way to communicate: One writes into something which the other one reads. Communication Reading and Writing shared variables Sending and Receiving messages 2 OSKomp 08 Process synchronization

3 Synchronisation Communication Synchronisation Synchronisation Mutual Exclusion Condition synchronisation Example (Mutual Exclusion) Whenever 2 processes need to take runs accessing shared objects Example (Condition synchronisation) Whenever one process needs to wait for another 3 OSKomp 08 Process synchronization

4 On the way to atomicity A Bank account example Balance b with initially 100 sek.. Person A wants to withdraw 70 sek, if possible. Person B wants to withdraw 50 sek, if possible. Balance should not be negative. int b = 100; initially 100 sek on the bank account Person A tries to withdraw 70 sek if (b 70 > 0) { b = b 70; Person B tries to withdraw 50 sek if (b 50 > 0) { b = b 50; Can anything go wrong? b < 0?? 4 OSKomp 08 Process synchronization

5 Atomicity Another bank account example Balance b = 0 sek. Person A does a deposit of 100 sek. Person B does a deposit of 200 sek Balance should be 300 sek. A Balance b B 0 load R 4, b load R 2, b 0 add R 2,#100 store b, R add R 4,# store b, R 4 Solution: Synchronisation (Locks,Semaphores,Monitors,Retry loops,...) 5 OSKomp 08 Process synchronization

6 The programmer s nightmare begins The programmer must implement correct synchronization! Example (lock S and Q) P 1 P 2 lock(s) lock(q) lock(q) lock(s) unlock(q) unlock(s) unlock(s) unlock(q) Leads to deadlock: Both P 1 and P 2 are waiting for each other Additionally, bad implementation can lead to starvation. 6 OSKomp 08 Process synchronization

7 Atomicity Another example sum:=0 while (sum < N) while (sum < N) sum:=sum+1 while (sum < N) sum:=sum+1 while (sum < N) sum:=sum+1 sum:=sum+1 How many additions? Anything between N and N*4 7 OSKomp 08 Process synchronization print(sum) What is printed? Anything between N and N+3

8 Outline 1 Introduction 2 Critical Section Problem 3 Locks 4 Semaphores 5 Monitors 8 OSKomp 08 Process synchronization

9 Critical Section Problem Critical Section Problem LOCK(bank account) if (can withdraw) { Do the withdrawal UNLOCK(bank account) Wait for your turn, only one can update Release the lock 9 OSKomp 08 Process synchronization

10 Critical Section CO [Process i = 1 to n] { while (true) { LOCK(resource); Do critical section work; UNLOCK(resource); Do NON-critical section work; Assumption A process that enters its CS will eventually exit A process may only terminate in its NON-critical section 10 OSKomp 08 Process synchronization

11 Say stop at entry section... The main issue is How do we make a thread wait? A solution: Busy waiting Check repeatedly a condition until it becomes true. Another solution: Blocking Waiting threads are de-scheduled High overhead Allows processor to do other things Hybrid methods: Busy-wait a while, then block 11 OSKomp 08 Process synchronization

12 Challenge Task Design the LOCK and UNLOCK routines. Ensuring: Mutual Exclusion No deadlocks No unnecessary delays Eventual Entry 12 OSKomp 08 Process synchronization

13 Reformulation Let in1 and in2 be boolean variables. in1 is true if Process 1 is in its CS, false otherwise in2 is true if Process 2 is in its CS, false otherwise Avoid that both in1 and in2 are true MUTEX: (in1 in2) A solution: wait_until(!in2) and then in1 = true; //ATOMICALLY!! <wait_until(!in2) and then in1 = true;> 13 OSKomp 08 Process synchronization

14 Coarse-grained solution bool in1 = false, in2 = false; MUTEX: (in1 in2) Process 1 while (true) { < wait_until(!in2) and then in1 = true; > Do critical section work in1=false; Do NON-critical section Process 2 while (true) { < wait_until(!in1) and then in2 = true; > Do critical section work in2=false; Do NON-critical section But n processes n variables OSKomp 08 Process synchronization

15 Coarse-grained solution Only 2 interesting states: locked and unlocked 1 variable is enough bool lock = false; while (true) { Process 1 < wait_until(!lock) and then lock = true; > Do critical section work lock=false; Do NON-critical section while (true) { Process 2 < wait_until(!lock) and then lock = true; > Do critical section work lock=false; Do NON-critical section 15 OSKomp 08 Process synchronization

16 Outline 1 Introduction 2 Critical Section Problem 3 Locks 4 Semaphores 5 Monitors 16 OSKomp 08 Process synchronization

17 How to? < await(!lock) and then lock = true; > Read-Modify-Write atomic primitives Test and Set (TAS): Value at Mem[lock_addr] loaded in a specified register. Constant 1 atomically stored into Mem[lock_addr] Swap: Atomically swaps the value of REG with Mem[lock_addr] Compare and Swap (CAS): Swaps if Mem[lock_addr]==REG2 Fetch and Add (FA): Increments a value by a given constant and returns the old value 17 OSKomp 08 Process synchronization

18 Test And Set bool TAS(bool lock) { < bool initial = lock; Save the initial value lock = true; Set lock return initial; > Return initial value 18 OSKomp 08 Process synchronization

19 Spin Lock bool TAS(bool lock) { < bool initial = lock; Save the initial value lock = true; Set lock return initial; > Return initial value lock(lock_variable) { while(tas(lock_variable)==true){; Bang on the lock until free unlock(lock_variable) { lock_variable:=false; Reset to the initial value 19 OSKomp 08 Process synchronization

20 Test and Test and Set lock(lock_variable) { while(tas(lock_variable)==true){; lock(lock_variable) { More optimistic solution while (true) { if(tas(lock_variable)==false) break; Bang on the lock once while(lock_variable==true){; 20 OSKomp 08 Process synchronization

21 Tie Breaker Petersson s algorithm Remember who had the lock latest! bool in1 = false, in2 = false; int last = 1; Process 1 while (true) { in1=true, last = 1; while(in2 and last==1){; Do critical section work in1=false; Do NON-critical section work Process 2 while (true) { in2=true, last = 2; while(in1 and last==2){; Do critical section work in2=false; Do NON-critical section work 21 OSKomp 08 Process synchronization

22 Ticket-based lock CO [Process i = 1 to n] { while (true) { <turn[i] = number; number = number+1;> < await(turn[i] == number);> Do critical section < next = next+1;> Do NON-critical section Fetch and Add (FA) Increments a value by a given constant and returns the old value CO [Process i = 1 to n] { while (true) { turn[i] = FA(number,1); while(turn[i]!= next){; Can even have a back-off Do critical section next = next+1; Is that safe? Do NON-critical section 22 OSKomp 08 Process synchronization

23 So locks...? Are busy-waiting protocols complex? No clear distinction between variables used: for synchronization for computing results Busy-waiting is often inefficient Usually more processes/threads than processors Processor executing a spinning process can be more productively employed to execute another process Semaphores First synchronisation tool (and remains one of the most important). Easy to protect critical sections. Included in (almost) all parallel programming libraries. 23 OSKomp 08 Process synchronization

24 Outline 1 Introduction 2 Critical Section Problem 3 Locks 4 Semaphores 5 Monitors 24 OSKomp 08 Process synchronization

25 Semaphore Shared variable with 2 atomic methods: P(Semaphore s) { Probeer (try) / Passeren (pass) / Pakken (grab) < wait until c > 0, then c := c-1; > must be atomic once c > 0 is detected V(Semaphore s) { Verhoog (increase) < c = c + 1; > must be atomic Init(Semaphore s, Integer i){ c := i; 25 OSKomp 08 Process synchronization

26 Semaphores, what for? Critical section: Mutual exclusion Barriers: Signaling events Producers and Consumers Bounded buffers: Resource counting 26 OSKomp 08 Process synchronization

27 Critical section sem mutex; Init(sem,1); 1 0 while (true) { Process 1 P(mutex); Critical section V(mutex); NON-Critical section while (true) { Process 2 P(mutex); Critical section V(mutex); NON-Critical section 27 OSKomp 08 Process synchronization

28 Barriers: Signaling events sem arrive1, arrive2; Init(arrive1,0); Init(arrive2,0); 0 0 Barrier Section A Section B... process 1 in section A V(arrive1); signal arrival P(arrive2); Wait for the other process... process 1 in section B... process 2 in section A V(arrive2); signal arrival P(arrive1); Wait for the other process... process 2 in section B 28 OSKomp 08 Process synchronization

29 Semaphores: Procuders and Consumers Producer Consumer Shared Resource 29 OSKomp 08 Process synchronization

30 Semaphores: Procuders and Consumers put Empty Full 1 0 take Producer Consumer Buffer Split Binary Semaphore Both are binary semaphores 32 OSKomp 08 Process synchronization

31 Semaphores: Procuders and Consumers typet buf; Buffer of some type T sem empty, full; Init(empty,1); Init(full,0); Empty 1 Full 0 while (true) {... P(empty); buf = data; V(full);... Producer while (true) {... P(full); result = buf; V(empty);... Consumer 33 OSKomp 08 Process synchronization

32 In the last example... Single communication buffer No waiting if data are produced and consumed at the same rate But in general, producer/consumer execution is bursty Example producer produces several items in a quick succession does more computation produces another set of items Solution: Increase the buffer capacity 34 OSKomp 08 Process synchronization

33 Bounded buffer: Resource counting put Empty n Full 0 take Producer Consumer Buffer 35 OSKomp 08 Process synchronization

34 The Buffer... rear front Put: buf[rear] = data; rear = (rear + 1) %n Take: result = buf[front]; front = (front + 1) %n 36 OSKomp 08 Process synchronization

35 Bounded buffer: Resource counting typet buf[n]; Array of some type T int front=0, rear=0; sem empty, full; Init(empty,n); Init(full,0); Empty n sem mutexp, mutext; Init(mutexP,1), Init(mutexT,1); Full 0 while (true) { Producer... P(empty); P(mutexP); buf[rear] = data; rear = (rear+1) %n; V(mutexP); V(full);... while (true) { Consumer... P(full); P(mutexT); result = buf[front]; front = (front+1) %n; V(mutexT); V(empty); OSKomp 08 Process synchronization

36 Semi-Conclusion Empty Full 1 0 n Critical Section Blocking semaphore Barriers/Signaling Split Binary semaphore Resource Counting 38 OSKomp 08 Process synchronization

37 Dining Philosophers Problem Five philosophers sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the center of the table is a large patter of spaghetti. Because the spaghetti is long and tangled and the philosophers are not mechanically adept a philosopher must use two forks to eat a helping. Unfortunately, the philosophers can afford only five forks. One fork is placed between each pair of philosophers. And they agree that each will use only the forks to the immediate left and right. The problem is to write a program to simulate the behavior of the philosophers. The program must avoid the unfortunate (and eventually fatal) situation in which all philosophers are hungry but non is able to acquire both forks for example, each holds one fork and refuses to give it up. 39 OSKomp 08 Process synchronization

38 Dining Philosophers Problem Philopher 1 Philopher 2 Spaghetti Philopher 0 Philopher 3 Philopher 4 while (true) { Philosopher i think; acquire forks; eat; release forks; sem fork[5] = {1,1,1,1,1 while (true) { Philosopher 0,1,2,3,4 think; P(fork[i]);P(fork[i+1]); eat; V(fork[i]);V(fork[i+1]); 40 OSKomp 08 Process synchronization

39 Dining Philosophers Problem See lab 2 41 OSKomp 08 Process synchronization

40 Readers/Writers Shared Resource Readers Writer Writer Example of selective mutual exclusion Example of general condition synchronization 42 OSKomp 08 Process synchronization

41 Readers/Writers as an Exclusion Problem First Solution: 1 Overconstrain the problem 2 Relax the constraints Let rw be a mutual exclusion semaphore Init(rw,1); Readers 1,..,M Writers 1,..,N while (true) {... P(rw); grab exclusive access lock Read the database V(rw); release the lock... while (true) {... P(rw); grab exclusive access lock Write the database V(rw); release the lock... Readers as a group need to lock out writers but only the first needs to grab the lock (i.e. P(rw)) Subsequent readers can directly access the database 43 OSKomp 08 Process synchronization

42 Relaxing constraints int nr = 0; number of active readers sem rw; Init(rw,1); lock for reader/writer exclusion sem mutexr; Init(mutexR,1); lock for reader access to nr Readers 1,..,M while (true) {... P(mutexR); nr = nr + 1; if (nr == 1) P(rw); if first, get lock V(mutexR); Read the database P(mutexR); nr = nr - 1; if (nr == 0) V(rw); if last, release lock V(mutexR);... Writers 1,..,N while (true) {... P(rw); grab exclusive access lock Write the database V(rw); release the lock OSKomp 08 Process synchronization

43 Readers/Writers using Condition Synchronization Second Solution: 1 Count the number of each kind of processes trying to access the database 2 Constrain the values of the counters Let nr and nw be nonnegative counters; BAD: (nr > 0 nw > 0 ) nw > 1 Symmetrically, good states, RW = BAD RW: (nr == 0 nw == 0 ) nw 1 45 OSKomp 08 Process synchronization

44 Coarse-grained solution using Condition Synchronization int nr = 0, nw = 0; RW: (nr == 0 nw == 0) nw 1 Readers 1,..,M while (true) {... < await(nw == 0) nr = nr + 1; > Read the database < nr = nr - 1; >... Writers 1,..,N while (true) {... < await(nr == 0 and nw == 0) nw = nw + 1; > Write the database < nw = nw - 1; > OSKomp 08 Process synchronization

45 So... semaphores, really? Common use in programming languages that do not intrinsically support other forms of synchronization. They are the primitive synchronization mechanism in many operating systems. The trend in programming language development, though, is towards more structured forms of synchronization, such as monitors. Inadequacies in dealing with (multi-resource) deadlocks Do not protect the programmer from the easy mistakes of taking a semaphore that is already held by the same process, and forgetting to release a semaphore that has been taken. 47 OSKomp 08 Process synchronization

46 Outline 1 Introduction 2 Critical Section Problem 3 Locks 4 Semaphores 5 Monitors 48 OSKomp 08 Process synchronization

47 Monitors My old harddrive crashed, where the source code was. I just have the PDF file in handout mode (go to slide 30) 49 OSKomp 08 Process synchronization

Deadlocks. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Deadlocks. Frédéric Haziza Spring Department of Computer Systems Uppsala University Deadlocks Frédéric Haziza Department of Computer Systems Uppsala University Spring 2008 Outline 1 Quick recall on Synchronisation 2 Characterizing Deadlocks 3 Handling Deadlocks 2 OSKomp

More information

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34 Semaphores INF4140 13.09.12 Lecture 3 0 Book: Andrews - ch.04 (4.1-4.4) INF4140 (13.09.12 ) Semaphores Lecture 3 1 / 34 Overview Last lecture: Locks and Barriers (complex techniques) No clear difference

More information

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO Semaphores A shared integer variable, s, initialized to init, and manipulated only by two operations: pass (proberen): P(s) df = await(s > 0)s = s 1 1 Fairness: V may release a waiting process Weakly fair,

More information

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i Semaphores Derived Inference Rules A shared integer variable, s, initialized to i, and manipulated only by two operations: declare: sem s := i # i 0.Default is i =0. pass (proberen): P (s), hawait(s >0)

More information

Threading and Synchronization. Fahd Albinali

Threading and Synchronization. Fahd Albinali Threading and Synchronization Fahd Albinali Parallelism Parallelism and Pseudoparallelism Why parallelize? Finding parallelism Advantages: better load balancing, better scalability Disadvantages: process/thread

More information

Operating Systems ECE344

Operating Systems ECE344 Operating Systems ECE344 Ding Yuan Announcement & Reminder Lab 0 mark posted on Piazza Great job! One problem: compilation error I fixed some for you this time, but won t do it next time Make sure you

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XXVI April 27 th, 2006 1 Roadmap Shared Memory Synchronization Spin Locks Barriers Semaphores Monitors 2 1 Memory Architectures Distributed Memory Shared Memory

More information

Lecture 3: Intro to Concurrent Processing using Semaphores

Lecture 3: Intro to Concurrent Processing using Semaphores Lecture 3: Intro to Concurrent Processing using Semaphores Semaphores; The Prucer-Consumer problem; The Dining Philosophers problem; The Readers-Writers Problem: Readers Preference Passing the Baton Ballhausen

More information

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University Synchronization Design, Spring 2011 Department of Computer Science Synchronization Basic problem: Threads are concurrently accessing shared variables The access should be controlled for predictable result.

More information

Opera&ng Systems ECE344

Opera&ng Systems ECE344 Opera&ng Systems ECE344 Lecture 6: Synchroniza&on (II) Semaphores and Monitors Ding Yuan Higher- Level Synchroniza&on We looked at using locks to provide mutual exclusion Locks work, but they have some

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

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

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

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

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

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

Topic 4: Synchronization with Semaphores

Topic 4: Synchronization with Semaphores CS 414 : Operating Systems UNIVERSITY OF VIRGINIA Department of Computer Science Fall 2005 Topic 4: Synchronization with Semaphores Readings for this topic: Sections 6.1-6.6 The too-much-milk solution

More information

Concurrent Computing

Concurrent Computing Concurrent Computing Introduction SE205, P1, 2017 Administrivia Language: (fr)anglais? Lectures: Fridays (15.09-03.11), 13:30-16:45, Amphi Grenat Web page: https://se205.wp.imt.fr/ Exam: 03.11, 15:15-16:45

More information

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

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

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

Introduction to Operating Systems

Introduction to Operating Systems 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

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

Semaphore Use In Synchronization

Semaphore Use In Synchronization Lesson 7 Semaphore Use In Synchronization Ch 6 [BenA 06] Consumer Producer Revisited Readers and Writers Baton Passing Private Semaphores Resource Management 1 Synchronization with Semaphores R1 R2 R3

More information

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1 Deadlock Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1 Recap: Synchronization Race condition A situation when two or more threads read and write shared

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

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1 Deadlock Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1 Recap: Synchronization Race condition A situation when two or more threads read and write shared

More information

Interprocess Communication and Synchronization

Interprocess Communication and Synchronization Chapter 2 (Second Part) Interprocess Communication and Synchronization Slide Credits: Jonathan Walpole Andrew Tanenbaum 1 Outline Race Conditions Mutual Exclusion and Critical Regions Mutex s Test-And-Set

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

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems CSE 153 Design of Operating Systems Winter 19 Lecture 7/8: Synchronization (1) Administrivia How is Lab going? Be prepared with questions for this weeks Lab My impression from TAs is that you are on track

More information

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

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28) Lecture Topics Today: Concurrency (Stallings, chapter 5.1-5.4, 5.7) Next: Exam #1 1 Announcements Self-Study Exercise #5 Project #3 (due 9/28) Project #4 (due 10/12) 2 Exam #1 Tuesday, 10/3 during lecture

More information

Concept of a process

Concept of a process Concept of a process In the context of this course a process is a program whose execution is in progress States of a process: running, ready, blocked Submit Ready Running Completion Blocked Concurrent

More information

2 Threads vs. Processes

2 Threads vs. Processes 9 2 Threads vs. Processes A process includes an address space (defining all the code and data pages) a resource container (OS resource and accounting information) a thread of control, which defines where

More information

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

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

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

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other No efficient solution Involve conflicting

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

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

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

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Process Synchronization

Process Synchronization Process Synchronization Part II, Modified by M.Rebaudengo - 2013 Silberschatz, Galvin and Gagne 2009 Classical Problems of Synchronization Consumer/Producer with Bounded-Buffer Problem s and s Problem

More information

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 8 Semaphores II Natasha Alechina School of Computer Science nza@cs.nott.ac.uk Outline of this lecture problem solving with semaphores solving Producer-Consumer problems

More information

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

CS 4410 Operating Systems. Review 1. Summer 2016 Cornell University CS 4410 Operating Systems Review 1 Summer 2016 Cornell University 1 A modern computer system keyboard disks mouse printer monitor CPU Disk controller USB controller Graphics adapter memory OS device driver

More information

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1 Synchronization Disclaimer: some slides are adopted from the book authors slides 1 Recap Synchronization instructions test&set, compare&swap All or nothing Spinlock Spin on wait Good for short critical

More information

EECS 482 Introduction to Operating Systems

EECS 482 Introduction to Operating Systems EECS 482 Introduction to Operating Systems Winter 2018 Harsha V. Madhyastha Recap Multi-threaded code with monitors: Locks for mutual exclusion Condition variables for ordering constraints Every thread

More information

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading Threads Figure 1: Multi-Threading Figure 2: Multi-Threading Concurrency What it is 1. Two or more threads of control access a shared resource. Scheduler operation must be taken into account fetch-decode-execute-check

More information

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1 Synchronization Disclaimer: some slides are adopted from the book authors slides 1 Recap Synchronization instructions test&set, compare&swap All or nothing Spinlock Spin on wait Good for short critical

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

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

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

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

Critical Section Problem: Example

Critical Section Problem: Example Why?! Examples Semaphores Monitors Synchronization: Recap Reading: Silberschatz, Ch. 6 Critical Section Problem: Example Insertion of an element into a list. new curr 1. void insert(new, curr) { /*1*/

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

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 11: Semaphores I" Brian Logan School of Computer Science bsl@cs.nott.ac.uk Outline of this lecture" problems with Peterson s algorithm semaphores implementing semaphores

More information

Semaphores. Otto J. Anshus University of {Tromsø, Oslo}

Semaphores. Otto J. Anshus University of {Tromsø, Oslo} Semaphores Otto J. Anshus University of {Tromsø, Oslo} Input sequence f Output sequence g Concurrency: Double buffering /* Fill s and empty t concurrently */ Get (s,f) s Put (t,g) t /* Copy */ t := s;

More information

CS 153 Design of Operating Systems Winter 2016

CS 153 Design of Operating Systems Winter 2016 CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization Administrivia Homework 1 Due today by the end of day Hopefully you have started on project 1 by now? Kernel-level threads (preemptable

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

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

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo CSE 120 Principles of Operating Systems Fall 2007 Lecture 6: Semaphores Keith Marzullo Announcements Homework #2 out Homework #1 almost graded... Discussion session on Wednesday will entertain questions

More information

Critical Section Problem: Example

Critical Section Problem: Example Synchronization: Recap Why? Example The Critical Section Problem (recap!) Hardware Support for Synchronization Lock-free operations Semaphores Monitors Reading: Doeppner, Ch. 2.2.3 Critical Section Problem:

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

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

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4 Temporal relations CSE 451: Operating Systems Autumn 2010 Module 7 Synchronization Instructions executed by a single thread are totally ordered A < B < C < Absent synchronization, instructions executed

More information

More Types of Synchronization 11/29/16

More Types of Synchronization 11/29/16 More Types of Synchronization 11/29/16 Today s Agenda Classic thread patterns Other parallel programming patterns More synchronization primitives: RW locks Condition variables Semaphores Message passing

More information

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018 Lecture In the lecture on March 13 we will mainly discuss Chapter 6 (Process Scheduling). Examples will be be shown for the simulation of the Dining Philosopher problem, a solution with monitors will also

More information

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011 Synchronization CS61, Lecture 18 Prof. Stephen Chong November 3, 2011 Announcements Assignment 5 Tell us your group by Sunday Nov 6 Due Thursday Nov 17 Talks of interest in next two days Towards Predictable,

More information

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data Lecture 4 Monitors Summary Semaphores Good news Simple, efficient, expressive Passing the Baton any await statement Bad news Low level, unstructured omit a V: deadlock omit a P: failure of mutex Synchronisation

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

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

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour Course CS350 - Operating Systems Sections 01 (11:30), 02 (16:00), 03 (8:30) Instructor Ashraf Aboulnaga & Borzoo Bonakdarpour Date of Exam October 25, 2011 Time Period 19:00-21:00 Duration of Exam Number

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

Synchronization. Erik Hagersten Uppsala University Sweden. Components of a Synchronization Even. Need to introduce synchronization.

Synchronization. Erik Hagersten Uppsala University Sweden. Components of a Synchronization Even. Need to introduce synchronization. Synchronization sum := thread_create Execution on a sequentially consistent shared-memory machine: Erik Hagersten Uppsala University Sweden while (sum < threshold) sum := sum while + (sum < threshold)

More information

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

More information

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019 CS 31: Introduction to Computer Systems 22-23: Threads & Synchronization April 16-18, 2019 Making Programs Run Faster We all like how fast computers are In the old days (1980 s - 2005): Algorithm too slow?

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

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

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

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2 Plan Demos Project: Due» Demos following week (Wednesday during class time) Next Week» New phase of presentations» Deadlock, finish synchronization Course Progress:» History, Structure, Processes, Threads,

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

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

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings Chapter 6 Concurrency: Deadlock and Starvation Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Deadlock

More information

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions Chapter 2 Processes and Threads [ ] 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling 85 Interprocess Communication Race Conditions Two processes want to access shared memory at

More information

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors CSE 451: Operating Systems Spring 2017 Module 10 Semaphores, Condition Variables, and Monitors John Zahorjan 2017 Gribble, Lazowska, Levy, Zahorjan, Zbikowski 1 Semaphores Semaphore = a synchronization

More information

SFDV3006 Concurrent Programming

SFDV3006 Concurrent Programming SFDV3006 Concurrent Programming Lecture 6 Deadlocks, livelocks, Starvation Introduction Last week we covered semaphore and how to use them for both synchronization and condition synchronization This week

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

Synchronization problems with semaphores

Synchronization problems with semaphores Synchronization problems with semaphores Lecture 4 of TDA384/DIT391 (Principles of Concurrent Programming) Carlo A. Furia Chalmers University of Technology University of Gothenburg SP3 2017/2018 Today

More information

CS3502 OPERATING SYSTEMS

CS3502 OPERATING SYSTEMS CS3502 OPERATING SYSTEMS Spring 2018 Synchronization Chapter 6 Synchronization The coordination of the activities of the processes Processes interfere with each other Processes compete for resources Processes

More information

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T Operating Systems Operating Systems Summer 2017 Sina Meraji U of T More Special Instructions Swap (or Exchange) instruction Operates on two words atomically Can also be used to solve critical section problem

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 10: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded d Systems II Objectives: Lab 3: Windows Threads (win32 threading API) Convert serial

More information

Paralleland Distributed Programming. Concurrency

Paralleland Distributed Programming. Concurrency Paralleland Distributed Programming Concurrency Concurrency problems race condition synchronization hardware (eg matrix PCs) software (barrier, critical section, atomic operations) mutual exclusion critical

More information

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

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004 Lecture 8: Semaphores, Monitors, & Condition Variables 8.0 Main Points: Definition of semaphores Example of use

More information

Operating Systems. Synchronization

Operating Systems. Synchronization Operating Systems Fall 2014 Synchronization Myungjin Lee myungjin.lee@ed.ac.uk 1 Temporal relations Instructions executed by a single thread are totally ordered A < B < C < Absent synchronization, instructions

More information

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads

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

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani Synchronization Heechul Yun Disclaimer: some slides are adopted from the book authors and Dr. Kulkani 1 Synchronization Spinlock Recap Implement using h/w instructions (e.g., test-and-set) Mutex Sleep

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

Synchronization I. Jo, Heeseung

Synchronization I. Jo, Heeseung Synchronization I Jo, Heeseung Today's Topics Synchronization problem Locks 2 Synchronization Threads cooperate in multithreaded programs To share resources, access shared data structures Also, to coordinate

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

Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo}

Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo} Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo} The Wa (Wawa) at Princeton See the too much milk problem last week Wawa http://www.wawa.com/ http://www.absoluteastronomy.com/encyclopedia/w/wa/wawa_food_markets.htm

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 6: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded Systems II Based heavily on slides by Dr. Roger Walker More Task Decomposition: Dependence

More information