Reminders Homework 3 due next Monday

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.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
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.
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.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
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.
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.
Jonathan Walpole Computer Science Portland State University
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
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,
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
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
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.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
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)
Tutorial 2: Homework 1 and Project 1
CS703 – Advanced Operating Systems
CIS Operating Systems Synchronization
January 29, 2004 Adrienne Noble
Operating Systems CMPSC 473
Section 5 Project 1 Recap Synchronization primitives
Lecture 14: Pthreads Mutex and Condition Variables
Critical section problem
CSE 451 Autumn 2003 Section 3 October 16.
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Implementing Mutual Exclusion
Lecture 14: Pthreads Mutex and Condition Variables
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
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
Process/Thread Synchronization (Part 2)
Presentation transcript:

Reminders Homework 3 due next Monday Synchronization Project 2 parts 1,2,3 due next Wednesday Threads, synchronization Office hour at 3:30, not 4:30 today Today: Project 2 continued (parts 2,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

Synchronization Solutions High-level Monitors Java synchronized method OS-level support Special variables – mutexes, semaphores, condition vars Message passing primitives Why is cli/sti not good? Low-level support Disable/enable interrupts Atomic instructions

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

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 4) 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) { }

Looking ahead: preemption You can start inserting synchronization code disable/enable interrupts atomic_test_and_set Where would you use these? Example: … nextTCB = sthread_dequeue(readyQ); switch to nextTCB;

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

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 a simple lock on shared data

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 vari- able and another thread signals the condition just before the first thread actually waits on it.

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 Wait() always waits Signal() when queue empty doesn’t affect state No counter Lock association

Monitors: preview 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 CV shared data CV waiting queue of threads trying to enter the monitor f() { … } g() { … } h() { … } at most one thread in monitor at a time operations (procedures)

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

Miscellaneous Synchronization is necessary when multiple threads access the same shared data Can’t use some primitives in interrupt handlers Why? Which ones? Don’t forget to release lock, semaphore, etc Check all paths Synchronization bugs can be very difficult to find Read your code because interrupt handlers must not block (same goes for UNIX signal handlers)

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. Adapted from http://www.cs.duke.edu/~chase/cps110-archive/prob1-00s.pdf

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

Semaphore solution DeliveryGuy { Student { while(employed) { 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 mutex; (init to 1) // guard updating of num_slices DeliveryGuy {    while(employed) {       P(deliver);       make_pizza();       P(mutex);       num_slices=S;       V(mutex);       for (int i=0,i<S,i++) {          V(pizza);       }    } } Student {    while (diligent) { P(pizza); P(mutex); num_slices--; if (num_slices==0) // took last slice V(deliver); V(mutex); study();    } }

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