1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Interprocess Communication
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
1 OMSE 510: Computing Foundations 6: Multithreading Chris Gilmore Portland State University/OMSE Material Borrowed from Jon Walpole’s lectures.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.
1 CS 333 Introduction to Operating Systems Class 5 – Semaphores and Classical Synchronization Problems Jonathan Walpole Computer Science Portland State.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Jonathan Walpole Computer Science Portland State University
Jonathan Walpole Computer Science Portland State University
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
Jonathan Walpole Computer Science Portland State University
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
1 CS 333 Introduction to Operating Systems Class 5 – Semaphores and Classical Synchronization Problems Jonathan Walpole Computer Science Portland State.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
CS510 Concurrent Systems Introduction to Concurrency.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Synchronizing Threads with Semaphores
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS533 Concepts of Operating Systems Jonathan Walpole.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Chapter 5: Process Synchronization
Lecture 13: Producer-Consumer and Semaphores
Jonathan Walpole Computer Science Portland State University
Jonathan Walpole Computer Science Portland State University
CS399 New Beginnings Jonathan Walpole.
CS510 Operating System Foundations
Lecture 13: Producer-Consumer and Semaphores
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Oregon Health and Science University
Chapter 6: Synchronization Tools
Jonathan Walpole Computer Science Portland State University
Synchronization CSE 2431: Introduction to Operating Systems
Presentation transcript:

1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University

2 Synchronization primitives  Sleep  Put a thread to sleep  Thread becomes BLOCKed  Wakeup  Move a BLOCKed thread back onto “Ready List”  Thread becomes READY (or RUNNING)  Yield  Move to another thread  Does not BLOCK thread  Just gives up the current time-slice

3 But how can these be implemented?  In User Programs:  System calls to the kernel  In Kernel:  Calls to the thread scheduler routines

4 Concurrency control in the kernel  Different threads call Yield, Sleep,...  Scheduler routines manipulate the “ready list”  The ready list is shared data !  Problem:  How can scheduler routines be programmed correctly?  Solution:  Scheduler can disable interrupts, or  Scheduler can use the TSL instruction

5 Concurrency in the kernel  The kernel can avoid performing context switches while manipulating the ready list  prevents concurrent execution of system call code  … but what about interrupts?  … what if interrupt handlers touch the ready list?  Disabling interrupts during critical sections  Ensures that interrupt handling code will not run  Using TSL for critical sections  Ensures mutual exclusion for all code that follows that convention

6 Disabling interrupts  Disabling interrupts in the OS vs disabling interrupts in user processes  why not allow user processes to disable interrupts?  is it ok to disable interrupts in the OS?  what precautions should you take?

7 Disabling interrupts in the kernel Scenario 1: A thread is running; wants to access shared data Disable interrupts Access shared data (“critical section”) Enable interrupts

8 Disabling interrupts in the kernel Scenario 2: Interrupts are already disabled and a second thread wants to access the critical section...using the above sequence...

9 Disabling interrupts in the kernel Scenario 2: Interrupts are already disabled.  Thread wants to access critical section using the previous sequence... Save previous interrupt status (enabled/disabled) Disable interrupts Access shared data (“critical section”) Restore interrupt status to what it was before

10 Classical Synchronization Problems Producer-Consumer  One thread produces data items  Another thread consumes them  Use a bounded buffer / queue between the threads  The buffer is a shared resource Must control access to it!!!  Must suspend the producer thread if buffer is full  Must suspend the consumer thread if buffer is empty

11 Producer/Consumer with Busy Waiting thread producer { while(1){ // Produce char c while (count==n) { no_op } buf[InP] = c InP = InP + 1 mod n count++ } thread consumer { while(1){ while (count==0) { no_op } c = buf[OutP] OutP = OutP + 1 mod n count-- // Consume char } n-1 … Global variables: char buf[n] int InP = 0 // place to add int OutP = 0 // place to get int count

12 Problems with this code  Count variable can be corrupted if context switch occurs at the wrong time  A race condition exists!  Race bugs very difficult to track down  What if buffer is full?  Produce will busy-wait  Consumer will not be able to empty the buffer  What if buffer is empty?  Consumer will busy-wait  Producer will not be able to fill the buffer

13 0 thread consumer { 1 while(1) { 2 while (count==0) { 3 sleep(empty) 4 } 5 c = buf[OutP] 6 OutP = OutP + 1 mod n 7 count--; 8 if (count == n-1) 9 wakeup(full) 10 // Consume char 11 } 12 } Producer/Consumer with Blocking 0 thread producer { 1 while(1) { 2 // Produce char c 3 if (count==n) { 4 sleep(full) 5 } 6 buf[InP] = c; 7 InP = InP + 1 mod n 8 count++ 9 if (count == 1) 10 wakeup(empty) 11 } 12 } n-1 … Global variables: char buf[n] int InP = 0 // place to add int OutP = 0 // place to get int count

14 This code is still incorrect!  The “count” variable can be corrupted:  Increments or decrements may be lost!  Possible Consequences: Both threads may sleep forever Buffer contents may be over-written  What is this problem called?

15 This code is still incorrect!  The “count” variable can be corrupted:  Increments or decrements may be lost!  Possible Consequences: Both threads may sleep forever Buffer contents may be over-written  What is this problem called? Race Condition  Code that manipulates count must be made into a ??? and protected using ???

16 This code is still incorrect!  The “count” variable can be corrupted:  Increments or decrements may be lost!  Possible Consequences: Both threads may sleep forever Buffer contents may be over-written  What is this problem called? Race Condition  Code that manipulates count must be made into a critical section and protected using mutual exclusion!

17 Semaphores  An abstract data type that can be used for condition synchronization and mutual exclusion What is the difference between mutual exclusion and condition synchronization?

18 Semaphores  An abstract data type that can be used for condition synchronization and mutual exclusion  Condition synchronization  wait until invariant holds before proceeding  signal when invariant holds so others may proceed  Mutual exclusion  only one at a time in a critical section

19 Semaphores  An abstract data type  containing an integer variable (S)  Two operations: Down (S) and Up (S)  Alternative names for the two operations  Down(S) = Wait(S) = P(S)  Up(S) = Signal(S) = V(S)

20 Semaphores  Down (S) … called “Wait” in SPANK  decrement S by 1  if S would go negative, wait/sleep until signaled  Up (S) … called “Signal” in SPANK  increment S by 1  signal/wakeup a waiting thread  S will always be >= 0.  Both Up () and Down () are assumed to be atomic!!!  A kernel implementation must ensure atomicity

21 Variation: Binary Semaphores  Counting Semaphores  same as just “semaphore”  Binary Semaphores  a specialized use of semaphores  the semaphore is used to implement a Mutex Lock

22 Variation: Binary Semaphores  Counting Semaphores  same as just “semaphore”  Binary Semaphores  a specialized use of semaphores  the semaphore is used to implement a Mutex Lock  the count will always be either 0 = locked 1 = unlocked

23 Using Semaphores for Mutex 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE semaphore mutex = 1-- unlocked Thread A Thread B

24 Using Semaphores for Mutex semaphore mutex = 0-- locked 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE Thread A Thread B

25 Using Semaphores for Mutex semaphore mutex = 0--locked 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE Thread A Thread B

26 Using Semaphores for Mutex semaphore mutex = 0-- locked 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE Thread A Thread B

27 Using Semaphores for Mutex semaphore mutex = 0-- locked 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE Thread A Thread B

28 Using Semaphores for Mutex semaphore mutex = 1-- unlocked This thread can now be released! 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE Thread A Thread B

29 Using Semaphores for Mutex semaphore mutex = 0-- locked 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE 1 repeat 2 down(mutex); 3 critical section 4 up(mutex); 5 remainder section 6 until FALSE Thread A Thread B

30 Exercise: Implement producer/consumer Global variables semaphore full_buffs = ?; semaphore empty_buffs = ?; char buff[n]; int InP, OutP; 0 thread producer { 1 while(1){ 2 // Produce char c... 3 buf[InP] = c 4 InP = InP + 1 mod n 5 } 6 } 0 thread consumer { 1 while(1){ 2 c = buf[OutP] 3 OutP = OutP + 1 mod n 4 // Consume char... 5 } 6 }

31 Counting semaphores in producer/consumer 0 thread producer { 1 while(1){ 2 // Produce char c... 3 down(empty_buffs) 4 buf[InP] = c 5 InP = InP + 1 mod n 6 up(full_buffs) 7 } 8 } 0 thread consumer { 1 while(1){ 2 down(full_buffs) 3 c = buf[OutP] 4 OutP = OutP + 1 mod n 5 up(empty_buffs) 6 // Consume char... 7 } 8 } Global variables semaphore full_buffs = 0; semaphore empty_buffs = n; char buff[n]; int InP, OutP;

32 Implementing semaphores  Up () and Down () are assumed to be atomic

33 Implementing semaphores  Up () and Down () are assumed to be atomic How can we ensure that they are atomic?

34 Implementing semaphores  Up() and Down() are assumed to be atomic How can we ensure that they are atomic?  Implement Up() and Down() as system calls?  how can the kernel ensure Up() and Down() are completed atomically?  avoid scheduling another thread when they are in progress?  … but how exactly would you do that?  … and what about semaphores for use in the kernel?

35 Semaphores with interrupt disabling Up(semaphore sem) DISABLE_INTS sem.val++ if (sem.val <= 0) { th = remove next thread from sem.L wakeup(th) } ENABLE_INTS struct semaphore { int val; list L; } Down(semaphore sem) DISABLE_INTS sem.val-- if (sem.val < 0){ add thread to sem.L block(thread) } ENABLE_INTS

36 Semaphores with interrupt disabling Up(semaphore sem) DISABLE_INTS sem.val++ if (sem.val <= 0) { th = remove next thread from sem.L wakeup(th) } ENABLE_INTS struct semaphore { int val; list L; } Down(semaphore sem) DISABLE_INTS sem.val-- if (sem.val < 0){ add thread to sem.L block(thread) } ENABLE_INTS

37 But what are block() and wakeup()?  If block stops a thread from executing, how, where, and when does it return?  which thread enables interrupts following Down()?  the thread that called block() shouldn’t return until another thread has called wakeup() !  … but how does that other thread get to run?  … where exactly does the thread switch occur?  Scheduler routines such as block() contain calls to switch() which is called in one thread but returns in a different one!!

38 Semaphores using atomic instructions  As we saw earlier, hardware provides special atomic instructions for synchronization  test and set lock (TSL)  compare and swap (CAS)  etc  Semaphore can be built using atomic instructions 1. build mutex locks from atomic instructions 2. build semaphores from mutex locks

39 Building blocking mutex locks using TSL Mutex_lock: TSL REGISTER,MUTEX | copy mutex to register and set mutex to 1 CMP REGISTER,#0 | was mutex zero? JZE ok | if it was zero, mutex is unlocked, so return CALL thread_yield | mutex is busy, so schedule another thread JMP mutex_lock | try again later Ok:RET | return to caller; enter critical section Mutex_unlock: MOVE MUTEX,#0 | store a 0 in mutex RET | return to caller

40 Building spinning mutex locks using TSL Mutex_lock: TSL REGISTER,MUTEX | copy mutex to register and set mutex to 1 CMP REGISTER,#0 | was mutex zero? JZE ok | if it was zero, mutex is unlocked, so return CALL thread_yield | mutex is busy, so schedule another thread JMP mutex_lock | try again later Ok:RET | return to caller; enter critical section Mutex_unlock: MOVE MUTEX,#0 | store a 0 in mutex RET | return to caller

41 To block or not to block?  Spin-locks do busy waiting  wastes CPU cycles on uni-processors  Why?  Blocking locks put the thread to sleep  may waste CPU cycles on multi-processors  Why?

42 Building semaphores using mutex locks Problem: Implement a counting semaphore Up () Down ()...using just Mutex locks

43 How about two “blocking” mutex locks? var cnt: int = 0 -- Signal count var m1: Mutex = unlocked -- Protects access to “cnt” m2: Mutex = locked -- Locked when waiting Down (): Lock(m1) cnt = cnt – 1 if cnt<0 Unlock(m1) Lock(m2) else Unlock(m1) endIf Up(): Lock(m1) cnt = cnt + 1 if cnt<=0 Unlock(m2) endIf Unlock(m1)

44 How about two “blocking” mutex locks? var cnt: int = 0 -- Signal count var m1: Mutex = unlocked -- Protects access to “cnt” m2: Mutex = locked -- Locked when waiting Down (): Lock(m1) cnt = cnt – 1 if cnt<0 Unlock(m1) Lock(m2) else Unlock(m1) endIf Up(): Lock(m1) cnt = cnt + 1 if cnt<=0 Unlock(m2) endIf Unlock(m1) Contains a Race Condition!

45 Oops! How about this then? var cnt: int = 0 -- Signal count var m1: Mutex = unlocked -- Protects access to “cnt” m2: Mutex = locked -- Locked when waiting Down (): Lock(m1) cnt = cnt – 1 if cnt<0 Lock(m2) Unlock(m1) else Unlock(m1) endIf Up(): Lock(m1) cnt = cnt + 1 if cnt<=0 Unlock(m2) endIf Unlock(m1)

46 Oops! How about this then? var cnt: int = 0 -- Signal count var m1: Mutex = unlocked -- Protects access to “cnt” m2: Mutex = locked -- Locked when waiting Down (): Lock(m1) cnt = cnt – 1 if cnt<0 Lock(m2) Unlock(m1) else Unlock(m1) endIf Up(): Lock(m1) cnt = cnt + 1 if cnt<=0 Unlock(m2) endIf Unlock(m1) Contains a Deadlock!

47 Ok! Lets have another try! var cnt: int = 0 -- Signal count var m1: Mutex = unlocked -- Protects access to “cnt” m2: Mutex = locked -- Locked when waiting Down (): Lock(m2) Lock(m1) cnt = cnt – 1 if cnt>0 Unlock(m2) endIf Unlock(m1) … is this solution valid? Up(): Lock(m1) cnt = cnt + 1 if cnt=1 Unlock(m2) endIf Unlock(m1)

48 What about this solution? Mutex m1, m2; // binary semaphores int C = N; // N is # locks int W = 0; // W is # wakeups Down(): Lock(m1); C = C – 1; if (C<0) Unlock(m1); Lock(m2); Lock(m1); W = W – 1; if (W>0) Unlock(m2); endif; else Unlock(m1); endif; Up(): Lock(m1); C = C + 1; if (C<=0) W = W + 1; Unlock(m2); endif; Unlock(m1);

49 Implementation possibilities  Implement Mutex Locks... using Semaphores  Implement Counting Semaphores... using Binary Semaphores... using Mutex Locks  Implement Binary Semaphores ... etc Can also implement using Test-And-Set Calls to Sleep, Wake-Up

50 Quiz  What is a race condition?  How can we protect against race conditions?  Can locks be implemented simply by reading and writing to a binary variable in memory?  How can a kernel make synchronization-related system calls atomic on a uniprocessor?  Why wouldn’t this work on a multiprocessor?  Why is it better to block rather than spin on a uniprocessor?  Why is it sometimes better to spin rather than block on a multiprocessor?

51 Spare slides

52 Semaphores in UNIX  User-accessible semaphores in UNIX are somewhat complex  Each up and down operation is done atomically on an “array” of semaphores. ********* WARNING *********  Semaphores are allocated by (and in) the operating system  Number based on configuration parameters  Semaphores in UNIX are a shared resource, potentially used by almost everyone  Must REMOVE your semaphores after you are done with them

53 Typical usage  main(){ int sem_id; sem_id = NewSemaphore(1);... Down(sem_id); [CRITICAL SECTION] Up (sem_id);... FreeSemaphore(sem_id); }

54 Managing your UNIX semaphores Listing currently allocated ipc resources ipcs Removing semaphores ipcrm -s