Synchronization: semaphores and some more stuff. Operating Systems, Spring 2018, I. Dinur, D. Hendler and R. Iakobashvili

Size: px
Start display at page:

Download "Synchronization: semaphores and some more stuff. Operating Systems, Spring 2018, I. Dinur, D. Hendler and R. Iakobashvili"

Transcription

1 Synchronization: semaphores and some more stuff 1

2 What's wrong with busy waiting? The mutual exclusion algorithms we saw used busy-waiting. What s wrong with that? Doesn't make sense for uni-processor o May cause deadlock Wastes CPU time o But may be efficient if waiting-time is short 2

3 What's wrong with busy waiting? Busy waiting may cause deadlock Process A's priority is higher than process B's Process B enters the CS Process A needs to enter the CS, busy-waits for B to exit the CS Process B cannot execute as long as the higher-priority process A is executing/ready Deadlock results 3

4 Outline Semaphores and the producer/consumer problem Counting semaphores from binary semaphores Event counters and message passing synchronization 4

5 Semaphores Two atomic operations are supported by a semaphore S: down(s) [the 'p' operation] If S=0 the process is blocked. It will resume execution only after it is woken-up Else S-- up(s) [the 'v' operation] If there are blocked processes, wake-up one of them Else S++ S is non-negative Supported by Windows, Unix, 5

6 Semaphores: is the following correct? Two atomic operations are supported by a semaphore S: down(s) [the 'p' operation] If S 0 the process is blocked. It will resume execution only after it is woken-up S-- up(s) [the 'v' operation] S++ If there are blocked processes, wake-up one of them 6

7 Pseudo-code in previous slide is wrong Consider the following bad sceneario: S=0 and process A performs down(s) A is blocked Process B performs up(s) S=1 A is ready Process C performs down(s) S=0 & C proceeds Process A gets a time-slice and proceeds S=-1 A single up() freed 2 down()s Operating Systems, 2011, Danny Hendler & Amnon Meisels 7

8 Implementing mutex with semaphores Shared data: semaphore lock; /* initially lock = 1 */ down(lock) Critical section up(lock) Does the algorithm satisfy mutex? Does it satisfy deadlock-freedom? Does it satisfy starvation-freedom? Yes Yes Depends 8

9 Semaphore as a General Synchronization Tool Execute B in P j only after A executed in P i Use semaphore flag initialized to 0 Code: 0 time P i A up(flag) P j down(flag) B 9

10 More on synchronization using semaphores Three processes p1; p2; p3 semaphores s1 = 1, s2 = 0; p1 p2 p3 down(s1); down(s2); down(s2); A B C up(s2); up(s2); up(s1); Which execution orders of A, B, C, are possible? (A B* C)* 10

11 No guarantee for correct synchronization (when multiple semaphores/locks are used) P 0 P 1 down(s); down(q); move1 up(s); up(q) down(q); down(s); move2 up(q); up(s); 1 1 Example: move money between two accounts which are protected by semaphores S and Q Does this work? Deadlock! 11

12 Negative-valued semaphores Two atomic operations are supported by a semaphore S: down(s) S-- If S<0 the process is blocked. It will resume execution only when S is non-negative up(s) S++ If there are blocked processes (i.e. S 0), wake-up one of them -3 If S is negative, then there are S blocked processes 12

13 Negative semaphore Implementation type semaphore = record value: integer; end; L: list of process; -3 L atomic down(s): S.value--; if (S.value < 0) { add this process to S.L; sleep; } atomic up(s): S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(p); } 13

14 Implementing a spin-lock with TSL (test-set-lock) mutex_lock: TSL REG, mutex CMP REG, #0 JZE ok CALL thread_yield JMP mutex_lock ok: RET mutex_unlock: MOV mutex, #0 RET 14

15 Implementing a negative semaphore with TSL type semaphore = record value, flag: integer; L: list of process; end; -3 L down(s): repeat until test-and-set(s.flag) S.value--; if (S.value < 0) { add this process to S.L; S.flag=0 sleep; } else S.flag=0 up(s): repeat until test-and-set(s.flag) S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(p); } S.flag=0 Any problem with this code? In down(), resetting flag and sleeping should be atomic. 15

16 More on semaphore implementation On a uni-processor, disabling interrupts may be used TSL implementation works for multi-processors On a multi-processor, we can use spin-lock mutual exclusion to protect semaphore access Why is this better than busy-waiting in the first place? Busy-waiting is now guaranteed to be very short 16

17 buffer Producer-Consumer Problem in out Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process Two versions unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size 17

18 Bounded Buffer 0 buffer item1 item2 2 Out producer 4 item3 consumer 6 In 5 7 item4 18

19 Implementation using semaphores Two processes or more use a shared buffer in memory The buffer has finite size(i.e., it is bounded) The producer writes to the buffer and the consumer reads from it A full buffer stops the producer An empty buffer stops the consumer 19

20 Producer-Consumer implementation with semaphores #define N 100 /* Buffer size */ typedef int semaphore; semaphore mutex = 1; /* access control to critical section */ semaphore empty = N; /* counts empty buffer slots */ semaphore full = 0; /* counts full slots */ void producer(void) { int item; while(true) { produce_item(&item); /* generate something... */ down(&empty); /* decrement count of empty */ down(&mutex); /* enter critical section */ enter_item(item); /* insert into buffer */ up(&mutex); /* leave critical section */ up(&full); /* increment count of full slots */ } } 20

21 Producer-Consumer implementation with semaphores void consumer(void) { int item; } while(true) { down(&full); /* decrement count of full */ down(&mutex); /* enter critical section */ remove_item(&item); /* take item from buffer) */ up(&mutex); /* leave critical section */ up(&empty); /* update count of empty */ consume_item(item); /* do something... */ } 21

22 Outline Semaphores and the producer/consumer problem Counting semaphores from binary semaphores Event counters and message passing synchronization 22

23 Binary Semaphore Assumes only values 0 or 1 Wait blocks if semaphore=0 Signal (up operation) either wakes up a waiting process, if there is one, or sets value to 1 (if value is already 1, signal is wasted ) How can we implement a counting semaphore by using binary semaphores? 23

24 Implementing a counting semaphore with binary semaphores (user space): take 1 binary-semaphore S1 initially 1, S2 initially 0, S.value initially 1 down(s): down(s1); S.value--; if(s.value < 0){ L1: up(s1); L2: down(s2); } else up(s1); up(s): down(s1); S.value++; if(s.value 0) up(s2); up(s1) This code does not work. Why? 24

25 Race condition for counting semaphore take 1 1. Processes Q1 Q4 perform down(s), Q2 Q4 are preempted between lines L1 and L2: the value of the counting semaphore is now Processes Q5-Q7 now perform up(s): the value of the counting semaphore is now 0 3. Now, Q2-Q4 wake-up in turn and perform line L2 (down S2) 4. Q2 runs but Q3-Q4 block. 3 down( ) operations should have been permitted, only 1 was 25

26 Implementing a counting semaphore with binary semaphores (user space): take 2 binary-semaphore S1 initially 1, S2 initially 0, S.value initially 1 down(s): down(s1); S.value--; if(s.value < 0){ up(s1); //L1 down(s2); } //L2 up(s1); up(s): down(s1); S.value++; if(s.value 0) up(s2); else up(s1) Does this code work? 26

27 The effect of the changes up(s1) is performed by up(s) only if no process waits on S2 Q5 leaves up(s) without releasing S1 Q6 cannot enter the critical section that protects the counter It can only do so after one of Q2-Q4 releases S1 This generates a lock-step situation: an up(), a down(), an up() The critical section that protects the counter is entered alternately by a producer or a consumer 27

28 Recall the bounded-buffer algorithm #define N 100 typedef int semaphore; semaphore mutex = 1; semaphore empty = N; semaphore full = 0; void producer(void) { int item; while(true) { produce_item(&item); down(&empty); down(&mutex); enter_item(item); up(&mutex); up(&full); } } void consumer(void) { int } item; while(true) { down(&full); down(&mutex); remove_item(&item); up(&mutex); up(&empty); consume_item(item); } 28

29 A Problematic Scheduling Scenario Consider a Bounded buffer of 5 slots. Assume there are 6 processes each filling five slots in turn. 1 2 Empty.Value =

30 A Problematic Scheduling Scenario 1. five slots are filled by the first producer 1 2 Empty.Value =

31 A Problematic Scheduling Scenario 1. The second producer is blocked 1 2 Empty.Value =

32 A Problematic Scheduling Scenario 1. The third producer is blocked 1 2 Empty.Value =

33 A Problematic Scheduling Scenario 1. The fourth producer is blocked 1 2 Empty.Value =

34 A Problematic Scheduling Scenario 1. The fifth producer is blocked 1 2 Empty.Value =

35 A Problematic Scheduling Scenario 2. All blocked producers are waiting on S2 1 2 Empty.Value =

36 A Problematic Scheduling Scenario 3. The consumer consumes an item and is blocked on Empty.S1 until a producer adds an item. 1 2 Empty.Value =

37 A Problematic Scheduling Scenario 3. The consumer consumes an item and is blocked on S1, one producer adds an item. 1 2 Empty.Value =

38 A Problematic Scheduling Scenario 4. Consumer must consume, only then another producer wakes up and produces an item 1 2 Empty.Value =

39 A Problematic Scheduling Scenario 4. Same as in step Empty.Value =

40 A Problematic Scheduling Scenario 5. And again 1 2 Empty.Value =

41 Implementing a counting semaphore with binary semaphores (user space): take 3 (P.A. Kearns, 1988) binary-semaphore S1=1, S2=0, value initially 1, integer wake=0 down(s) down(s1); S.value--; if(s.value < 0){ up(s1); //L1 down(s2); //L2 down(s1); S.wake--; if(s.wake > 0) then up(s2);} //L3 up(s1); up(s): down(s1); S.value++; if(s.value <= 0) { S.wake++; up(s2); } up(s1); Does THIS work? 41

42 Correctness arguments (Kearns) The counter S.wake is used when processes performing down(s) are preempted between lines L1 and L2 In such a case, up(s2) performed by processes during up(s) has no effect However, these processes accumulate their waking signals on the (protected) counter S.wake After preemption is over, any single process that wakes up from its block on down(s2) checks the value of S.wake The check is again protected For each count of the wake-up signals, the awakened process performs the up(s2) (in line L3) Each re-scheduled process wakes up the next one 42

43 Kearns' algorithm is wrong Processes P 0..P 7 perform down(s), P 0 goes through, P 1..P 7 go to sleep after performing line L2 of the operation Processes P 8..P 11 perform up(s) and their up(s2) operations release, say, P 1..P 4 Processes P 5, P 6, P 7 are still waiting on S2 and S.wake = 4 Processes P 1..P 4 are ready, just before line L3 Each of P 1..P 3 will decrement S.wake in its turn, check that it's positive and signal one of P 5..P 7 Four up operations have released 7 down operations 43

44 Implementing a counting semaphore with binary semaphores (user space): take 4 (Hemmendinger, 1989) binary-semaphore S1=1, S2=0, integer wake=0 down(s) down(s1); S.value--; if(s.value < 0){ up(s1); down(s2); down(s1); S.wake--; if(s.wake > 0) then up(s2);} // L3 up(s1); up(s): down(s1); S.value++; if(s.value <= 0) { S.wake++; if (S.wake == 1) up(s2); } up(s1); This works 44

45 Implementing a counting semaphore with binary semaphores (user space): take 5 (Barz, 1983) binary-semaphore S1=1, S2=min(1, init_value), value=init_value down(s) down(s2); down(s1); S.value--; if (S.value>0) then up(s2); up(s1); up(s): down(s1); S.value++; if(s.value == 1) { up(s2); } up(s1); This works, is simpler, and was published earlier(!) Can we switch the order of downs in down(s)? 45

46 Correctness arguments The critical section is guarded by S1 and each of the operations down(s) and up(s) uses it to correctly update the value of S.value After updating (and inside the critical section) both operations release the S2 semaphore only if value is positive S.value is never negative, because any process performing down(s) is blocked at S2 Signals cannot be 'wasted' 46

47 Fairness of semaphores Order of releasing blocked processes: o Weak up() performing process enters after (one of the) blocked processes o Strong An upper bound on the number of entries of process that performed up() if others are waiting Unfair: o No guarantee about the number of times the up() performing process enters before the blocked o Open competition each time the lock is free o Imitating the Java 'wait' 'notify' mechanism o Or the spin-lock of XV6 47

48 Outline Semaphores and the producer/consumer problem Counting semaphores from binary semaphores Event counters and message passing synchronization 48

49 Event Counters Integer counters with three operations: o Advance(E): increment E by 1, wake up relevant sleepers o Await(E,v): wait until E v. Sleep if E < v o Read(E): return the current value of E Counter value is ever increasing The Read() operation is not required for the bounded-buffer implementation in the next slide 49

50 producer-consumer with Event Counters (for a single producer and a single consumer) #define N 100 typedef event_counter int in = 0; event_counter; /* counts inserted items */ event_counter out = 0; /* items removed from buffer */ void producer(void) { int item, sequence = 0; while(true) { produce_item(&item); sequence = sequence + 1; /* counts items produced */ await(out, sequence - N); /* wait for room in buffer */ enter_item(item); /* insert into buffer */ advance(&in); /* inform consumer */ } } 50

51 Event counters (producer-consumer) void consumer(void) { int item, sequence = 0; } while(true) { sequence = sequence + 1; /* count items consumed */ await(in, sequence); /* wait for item */ remove_item(&item); /* take item from buffer */ advance(&out); /* inform producer */ consume_item(item); } 51

52 Message Passing no shared memory In a multi-processor system without shared memory, synchronization can be implemented by message passing Implementation issues: o Acknowledgements may be required (messages may be lost) o Message sequence numbers required to avoid message duplication o Unique process addresses across CPUs (domains..) o Authentication (validate sender s identity, a multi-machine environment ) Two main functions: o send(destination, &message); o receive(source, &message) block while waiting... 52

53 Producer-consumer using message passing void consumer(void) { int item, i; message m; for(i = 0; i < N; i++) send(producer, &m); /* send N empties */ while(true) { receive(producer, &m); /* get message with item */ extract_item(&m, &item); send(producer, &m); /* send an empty reply */ consume_item(item); } } 53

54 Producer-consumer using message passing (continued) #define N 100 #define MSIZE 4 /* message size */ typedef int message(msize); void producer(void) { int item; message m; /* message buffer */ } while(true) { produce_item(&item); receive(consumer, &m); /*wait for an empty */ construct_message(&m, item); send(consumer, &m); /* send item */ } 54

55 Message passing variations Messages can be addressed to a process address or to a mailbox o Mailboxes are generated with some capacity. When sending a message to a full mailbox, a process blocks o Buffer management done by mailbox Unix pipes - a generalization of messages no fixed size message (blocking receive) If no buffer is maintained by the system, then send and receive must run in lock-step. Example: Unix rendezvous 55

CS Operating Systems

CS Operating Systems CS 4500 - Operating Systems Module 4: The Producer-Consumer Problem and Solution Methods Stanley Wileman Department of Computer Science University of Nebraska at Omaha Omaha, NE 68182-0500, USA June 3,

More information

CS Operating Systems

CS Operating Systems CS 4500 - Operating Systems Module 4: The Producer-Consumer Problem and Solution Methods Stanley Wileman Department of Computer Science University of Nebraska at Omaha Omaha, NE 68182-0500, USA June 3,

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

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

Operating Systems. User OS. Kernel & Device Drivers. Interface Programs. Interprocess Communication (IPC) Operating Systems User OS Kernel & Device Drivers Interface Programs Interprocess Communication (IPC) Brian Mitchell (bmitchel@mcs.drexel.edu) - Operating Systems 1 Interprocess Communication Shared Memory

More information

Inter- process Communications

Inter- process Communications Inter- process Communications The IPC mechanisms found in most UNIX systems: Pipes Pipes FIFO s (named pipes) Signals Message Queues Shared Memory Semaphores Sockets Piping is a process where the input

More information

C09: Process Synchronization

C09: Process Synchronization CISC 7310X C09: Process Synchronization Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/29/2018 CUNY Brooklyn College 1 Outline Race condition and critical regions The bounded

More information

Operating Systems. David Vernon. Copyright 2007 David Vernon (www.vernon.eu)

Operating Systems. David Vernon. Copyright 2007 David Vernon (www.vernon.eu) Operating Systems David Vernon Course Overview Key objective is to introduce the concept of operating systems (OS) The need for operating systems Understand design choices, design tradeoffs, and implications

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

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 Concurrent programming Assumptions:

More information

Synchronization Principles I

Synchronization Principles I CSC 256/456: Operating Systems Synchronization Principles I John Criswell University of Rochester 1 Synchronization Principles Background Concurrent access to shared data may result in data inconsistency.

More information

Introduction to OS Synchronization MOS 2.3

Introduction to OS Synchronization MOS 2.3 Introduction to OS Synchronization MOS 2.3 Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Mahmoud El-Gayyar / Introduction to OS 1 Challenge How can we help processes synchronize with each other? E.g., how

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 9: Semaphores and Monitors Some slides from Matt Welsh Summarize Where We Are Goal: Use mutual exclusion to protect critical sections of code that

More information

Operating Systems. OPS Processes

Operating Systems. OPS Processes Handout Introduction Operating Systems OPS Processes These notes were originally written using (Tanenbaum, 1992) but later editions (Tanenbaum, 2001; 2008) contain the same information. Introduction to

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

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

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

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018 SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T 2. 3. 8 A N D 2. 3. 1 0 S P R I N G 2018 INTER-PROCESS COMMUNICATION 1. How a process pass information to another process

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

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

Concurrency and Synchronisation. Leonid Ryzhyk

Concurrency and Synchronisation. Leonid Ryzhyk Concurrency and Synchronisation Leonid Ryzhyk Textbook Sections 2.3 & 2.5 2 Concurrency in operating systems Inter-process communication web server SQL request DB Intra-process communication worker thread

More information

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang COP 4225 Advanced Unix Programming Synchronization Chi Zhang czhang@cs.fiu.edu 1 Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating

More information

CENG334 Introduction to Operating Systems

CENG334 Introduction to Operating Systems CENG334 Introduction to Operating Systems Semaphores Topics: Need for higher-level synchronization primitives Semaphores and their implementation The Producer/Consumer problem and its solution with semaphores

More information

Process/Thread Synchronization

Process/Thread Synchronization CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov February 14, 2013 Reading Assignment 7 Chapter 7 Deadlocks, due 2/21 2/14/13 CSE325: Synchronization

More information

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers Mutual exclusion & synchronization mutexes Unbounded buffer, 1 producer, N consumers out shared by all consumers mutex among consumers producer not concerned: can still add items to buffer at any time

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

518 Lecture Notes Week 7

518 Lecture Notes Week 7 518 Lecture Notes Week 7 (October 13, 2014) 1/9 518 Lecture Notes Week 7 1 Topics Semaphores Messages Monitors Race conditions, deadlock and starvation Banker's Algorithm Dining Philosophers Java Threads

More information

7: Interprocess Communication

7: Interprocess Communication 7: Interprocess Communication Mark Handley Interprocess Communication Processes frequently need to communicate to perform tasks. Shared memory. Shared files. Message passing. Whenever processes communicate,

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

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

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem 1 CS 550 Operating Systems Spring 2018 Concurrency Semaphores, Condition Variables, Producer Consumer Problem Semaphore Semaphore is a fundamental synchronization primitive used for Locking around critical

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

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems Concurrency 1 Concurrency Execution of multiple processes. Multi-programming: Management of multiple processes within a uni- processor system, every system has this support, whether big, small or complex.

More information

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS Name: Student Number: SOLUTIONS ENGR 3950U / CSCI 3020U (Operating Systems) Quiz on Process Synchronization November 13, 2012, Duration: 40 Minutes (10 questions and 8 pages, 45 Marks) Instructor: Dr.

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

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for? Lightweight programming construct for concurrent activities How to implement? Kernel thread vs.

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

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

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

Synchronization Spinlocks - Semaphores

Synchronization Spinlocks - Semaphores CS 4410 Operating Systems Synchronization Spinlocks - Semaphores Summer 2013 Cornell University 1 Today How can I synchronize the execution of multiple threads of the same process? Example Race condition

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

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

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 What does a typical thread API look

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

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

Lecture 8: September 30

Lecture 8: September 30 CMPSCI 377 Operating Systems Fall 2013 Lecture 8: September 30 Lecturer: Prashant Shenoy Scribe: Armand Halbert 8.1 Semaphores A semaphore is a more generalized form of a lock that can be used to regulate

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

Reminder from last time

Reminder from last time Concurrent systems Lecture 2: More mutual exclusion, semaphores, and producer-consumer relationships DrRobert N. M. Watson 1 Reminder from last time Definition of a concurrent system Origins of concurrency

More information

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

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1 Synchronization Disclaimer: some slides are adopted from the book authors slides with permission 1 What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for?

More information

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

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set? Last Class: Synchronization Review Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical

More information

Process/Thread Synchronization

Process/Thread Synchronization CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov March 1, 2011 The image cannot be displayed. Your computer may not have enough memory to open the image,

More information

Synchronization: Semaphores

Synchronization: Semaphores Illinois Institute of Technology Lecture 26 4/25 solved Synchronization: Semaphores CS 536: Science of Programming, Spring 2018 A. Why Avoiding interference, while good, isn t the same as coordinating

More information

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras Week 06 Lecture 29 Semaphores Hello. In this video, we will

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

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

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

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

Concurrency: a crash course

Concurrency: a crash course Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Concurrency: a crash course Concurrent computing Applications designed as a collection of computational units that may execute

More information

CS3733: Operating Systems

CS3733: Operating Systems Outline CS3733: Operating Systems Topics: Synchronization, Critical Sections and Semaphores (SGG Chapter 6) Instructor: Dr. Tongping Liu 1 Memory Model of Multithreaded Programs Synchronization for coordinated

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

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

Tasks. Task Implementation and management

Tasks. Task Implementation and management Tasks Task Implementation and management Tasks Vocab Absolute time - real world time Relative time - time referenced to some event Interval - any slice of time characterized by start & end times Duration

More information

Interprocess Communication

Interprocess Communication Chapter 6 Interprocess Communication 6.1 Introduction Processes frequently need to communicate with other processes. For example, in a shell pipeline, the output of the first process must be passed to

More information

High-level Synchronization

High-level Synchronization Recap of Last Class High-level Synchronization CS 256/456 Dept. of Computer Science, University of Rochester Concurrent access to shared data may result in data inconsistency race condition. The Critical-Section

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

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

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

Concurrent Processes Rab Nawaz Jadoon

Concurrent Processes Rab Nawaz Jadoon Concurrent Processes Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS Lahore Pakistan Operating System Concepts Concurrent Processes If more than one threads

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

Chapters 5 and 6 Concurrency

Chapters 5 and 6 Concurrency Operating Systems: Internals and Design Principles, 6/E William Stallings Chapters 5 and 6 Concurrency Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Concurrency When several processes/threads

More information

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

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

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

IT 540 Operating Systems ECE519 Advanced Operating Systems IT 540 Operating Systems ECE519 Advanced Operating Systems Prof. Dr. Hasan Hüseyin BALIK (5 th Week) (Advanced) Operating Systems 5. Concurrency: Mutual Exclusion and Synchronization 5. Outline Principles

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

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

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

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem? What is the Race Condition? And what is its solution? Race Condition: Where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular

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

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

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

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois 1 Synchronization primitives Mutex locks Used for exclusive access to a shared resource (critical section) Operations:

More information

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

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives. CS 5523 Operating Systems: Concurrency and Synchronization Thank Dr. Dakai Zhu and Dr. Palden Lama for providing their slides. Lecture Outline Problems with concurrent access to shared data Ø Race condition

More information

Last Class: Synchronization

Last Class: Synchronization Last Class: Synchronization Synchronization primitives are required to ensure that only one thread executes in a critical section at a time. Concurrent programs Low-level atomic operations (hardware) load/store

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

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

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

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

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5 1 Threads The Thread Model (1) (a) Three processes each with one thread (b) One process with three threads 2 1 The Thread Model (2)

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

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

COMP 3430 Robert Guderian

COMP 3430 Robert Guderian Operating Systems COMP 3430 Robert Guderian file:///users/robg/dropbox/teaching/3430-2018/slides/06_concurrency/index.html?print-pdf#/ 1/76 1 Concurrency file:///users/robg/dropbox/teaching/3430-2018/slides/06_concurrency/index.html?print-pdf#/

More information

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write Goals for Today CS162 Operating Systems and Systems Programming Lecture 5 Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors and condition variables Semaphores,

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

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

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks Synchronization II Today Condition Variables Semaphores Monitors and some classical problems Next time Deadlocks Condition variables Many times a thread wants to check whether a condition is true before

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

Process Synchronization

Process Synchronization Process Synchronization Concurrent access to shared data in the data section of a multi-thread process, in the shared memory of multiple processes, or in a shared file Although every example in this chapter

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 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 5 Concurrency: Mutual Exclusion and Synchronization

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Chapter 5 Concurrency: Mutual Exclusion and Synchronization Seventh Edition By William Stallings Designing correct routines for controlling concurrent

More information

Systèmes d Exploitation Avancés

Systèmes d Exploitation Avancés Systèmes d Exploitation Avancés Instructor: Pablo Oliveira ISTY Instructor: Pablo Oliveira (ISTY) Systèmes d Exploitation Avancés 1 / 32 Review : Thread package API tid thread create (void (*fn) (void

More information

1 Process Coordination

1 Process Coordination COMP 730 (242) Class Notes Section 5: Process Coordination 1 Process Coordination Process coordination consists of synchronization and mutual exclusion, which were discussed earlier. We will now study

More information

Models of concurrency & synchronization algorithms

Models of concurrency & synchronization algorithms Models of concurrency & synchronization algorithms Lecture 3 of TDA383/DIT390 (Concurrent Programming) Carlo A. Furia Chalmers University of Technology University of Gothenburg SP3 2016/2017 Today s menu

More information