1 CSE451 – Section 4. 2 Reminders Project 2 parts 1,2,3 due next Thursday Threads, synchronization Today: Project 2 continued (parts 2,3) Synchronization.

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.
Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Operating Systems Part III: Process Management (Process Synchronization)
Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
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.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Reminders Homework 3 due next Monday
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS533 Concepts of Operating Systems Class 3 Monitors.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Reminders Homework 3 due Monday, Oct. 25 Synchronization Project 2 parts 1,2,3 due Tuesday, Oct. 26 Threads, synchronization Today: Project 2 continued.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CSE 451: Operating Systems Section 5: Synchronization.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
CS510 Concurrent Systems Introduction to Concurrency.
Nachos Phase 1 Code -Hints and Comments
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
CSE 451: Operating Systems Section 5 Synchronization.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CSE 451 Section 4. 2 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, semaphor, condition var.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
CS703 – Advanced Operating Systems
CIS Operating Systems Synchronization
January 29, 2004 Adrienne Noble
Operating Systems CMPSC 473
Section 5 Project 1 Recap Synchronization primitives
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Critical section problem
Implementing Mutual Exclusion
Implementing Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
“The Little Book on Semaphores” Allen B. Downey
Presentation transcript:

1 CSE451 – Section 4

2 Reminders Project 2 parts 1,2,3 due next Thursday Threads, synchronization Today: Project 2 continued (parts 2,3) Synchronization

3 Project 2 Part 1 Questions Any questions about part 1? Some common issues: sthread_create doesn’t immediately run the new thread sthread_exit can ignore its ret argument How do you clean up an exiting thread? Must switch to another thread Clean up in all places after sthread_switch() Have a special GC thread

4 Synchronization Solutions High-level Monitors Java synchronized method OS-level support Special variables – mutexes, semaphores, condition vars Message passing primitives Low-level support Disable/enable interrupts Atomic instructions

5 Disabling/Enabling Interrupts Prevents context-switches during execution of CS Sometimes necessary E.g. to prevent further interrupts during interrupt handling Many problems Thread B: disable_interrupts() critical_section() enable_interrupts() Thread A: disable_interrupts() critical_section() enable_interrupts()

6 Hardware support Atomic instructions: Test and set Swap Compare-exchange (x86) Load-linked store conditional (MIPS, Alpha, PowerPC) Use these to implement higher-level primitives E.g. test-and-set on x86 (given to you for part 5) is written using compare-exchange. compare_exchange(lock_t *x,int y,int z): if(*x == y) *x = z; return y; else return *x; test_and_set(lock_t *l) { return compare_exchange(l,0,1); }

7 Semaphore review Semaphore = a special variable Manipulated atomically via two operations: P (wait) V (signal) Has a counter = number of available resources P decrements it V increments it Has a queue of waiting threads If execute wait() and semaphore is free, continue If not, block on that waiting queue signal() unblocks a thread if it’s waiting

8 Synchronization in Project 2 Part 2: write two synchronization primitives Implement mutex (binary semaphore) How is it different from spinlock? Need to keep track of lock state Need to keep waiting threads on a queue In lock(), may need to block current thread Don’t put on ready queue Do run some other thread For unlock(), need to take a thread off the waiting queue if available

9 Condition Variable A “place” to let threads wait for a certain event to occur while holding a lock (often a monitor lock). It has: Wait queue Three functions: wait, signal, and broadcast wait – sleep until the event happens signal – event/condition has occurred. If wait queue nonempty, wake up one thread, otherwise do nothing Do not run the woken up thread right away FIFO determines who wakes up broadcast – just like signal, except wake up all threads In part 2, you implement all of these A condition variable must always be associated with a mutex, to avoid the race condition where a thread prepares to wait on a condition variable and another thread signals the condition just before the first thread actually waits on it.

10 Condition Variables 2 How are CVs different from semaphores? More about cond_wait(sthread_cond_t cond, sthread_mutex_t lock) Called while holding lock ! Should do the following atomically: Release the lock (to allow someone else to get in) Add current thread to the waiters for cond Block thread until awoken After woken up, a thread should reacquire its lock before continuing Good explanation: man pthread_cond_wait We follow the same spec for wait, signal, bcast

11 Monitors: quick review One thread inside at a time Lock + a bunch of condition variables (CVs) CVs used to allow other threads to access the monitor while one thread waits for an event to occur shared data f() { … } g() { … } h() { … } waiting queue of threads trying to enter the monitor CV operations (procedures) at most one thread in monitor at a time CV

12 Part 3 problem N cooks produce burgers & place on stack M students grab burgers and eat them Provide correct synchronization Check with your threads and pthreads! Print out what happens! sample output (rough draft): … cook 2 produces burger #5 cook 2 produces burger #6 cook 3 produces burger #7 student 1 eats burger #7 student 2 eats burger #6 cook 1 produces burger #8 student 1 eats burger #8 student 1 eats burger #5 …

13 Miscellaneous Synchronization is necessary when multiple threads access the same shared data Don’t forget to release lock, semaphore, etc Check all paths Synchronization bugs can be very difficult to find Read your code

14 Sample synchronization problem Late-Night Pizza A group of students study for cse451 exam Can only study while eating pizza Each student thread executes the following: while (1) { pick up a piece of pizza; study while eating the pizza; } If a student finds pizza is gone, the student goes to sleep until another pizza arrives First student to discover pizza is gone orders a new one. Each pizza has S slices.

15 Late-Night Pizza Synchronize student threads and pizza delivery thread Avoid deadlock When out of pizza, order it exactly once No piece of pizza may be consumed by more than one student

16 Semaphore solution Student { while (diligent) { P(pizza); P(guard); num_slices--; if (num_slices==0) // took last slice V(deliver); V(guard); study(); } shared data: semaphore pizza; (counting sema, init to 0, represent number of available pizza resources) semaphore deliver; (init to 1) int num_slices = 0; semaphore guard; (init to 1) // guard updating of num_slices DeliveryGuy { while(employed) { P(deliver); make_pizza(); P(guard); num_slices=S; V(guard); for (int i=0,i<S,i++) { V(pizza); }

17 Condition Variable Solution (broken) Student() { while(diligent) { guard.lock(); if( slices > 0 ) { slices--; } else { if(first) { order.signal(); first = false; } deliver.wait(guard); } guard.unlock(); Study(); } int slices=0; Condition order, deliver; mutex guard; bool first = true; DeliveryGuy() { while(employed) { guard.lock(); order.wait(guard); makePizza(); slices = S; first=true; guard.unlock(); deliver.broadcast(); } Problem?? What if no DeliveryGuy is in order’s wait queue when a Student signals?

18 Condition Variable Solution 2 Student() { while(diligent) { guard.lock(); if( slices > 0 ) { slices--; } else { if(first) { ordered = true; order.signal(); first = false; } deliver.wait(guard); } guard.unlock(); Study(); } int slices=0; Condition order, deliver; mutex guard; bool first = true, ordered = false; DeliveryGuy() { while(employed) { guard.lock(); while(ordered == false) { order.wait(guard); } ordered = false; makePizza(); slices = S; first=true; guard.unlock(); deliver.broadcast(); }