CSE 451: Operating Systems Section 5 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.
Operating Systems Part III: Process Management (Process Synchronization)
 Read about Therac-25 at  [  [
Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
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.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
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.
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
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.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Jonathan Walpole Computer Science Portland State University
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.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
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.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
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.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
Outline Other synchronization primitives
Other Important Synchronization Primitives
Section 5 Project 1 Recap Synchronization primitives
Chapter 5: Process Synchronization
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Kernel Synchronization II
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
CSE 451 Section 1/27/2000.
Monitors and Inter-Process Communication
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CSE 451: Operating Systems Section 5 Synchronization

10/28/102

Project 1 Recap  Tips:  Check flags with &, not ==  Use constants for printed strings #define PROMPT “CSE451Shell>”  Use errno/perror(3) for error detection  To make grading easier:  Preserve the build hierarchy/commands  Check your files before turnin! 10/28/103

Project 2.a is almost due  Remember to write more test cases!  Writeups:  Design decisions & alternative implementations: give them some real thought  Be mindful of what you use as a resource (and how much)  We expect you to research, but we expect you to fumble around a little too 10/28/104

Synchronization 10/28/105

Synchronization support  Processor level:  Disable/enable interrupts  Atomic instructions (test-and-set)  Operating system level:  Special variables: mutexes, semaphores, condition variables  Programming language level:  Monitors, Java synchronized methods 10/28/106

Disabling/enabling interrupts Thread A: disable_irq() critical_section() enable_irq() Thread B: disable_irq() critical_section() enable_irq() 10/28/107  Prevents context-switches during execution of critical sections  Sometimes necessary  Many pitfalls

8 Processor support  Atomic instructions:  test-and-set  compare-exchange (x86)  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 10/28/10

9 Processor support  Test-and-set 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 *lock) { ??? } 10/28/10

10 Processor support  Test-and-set 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 *lock) { compare_exchange(lock, 0, 1); } 10/28/10

11 Project 2: preemption  Think about where synchronization is needed  Start inserting synchronization code  disable/enable timer interrupts  atomic_test_and_set 10/28/10

Semaphores  Semaphore = a special variable  Manipulated atomically via two operations  P (wait): tries to decrement semaphore  V (signal): increments semaphore  Has a queue of waiting threads  If execute wait() and semaphore is available, continue  If not, block on the waiting queue  signal() unblocks a thread on queue 10/28/1012

Mutexes 10/28/1013

Mutexes  A binary semaphore (semaphore initialized with value 1)  A lock that waits by blocking, rather than spinning 10/28/1014

Aside: kernel locking  Can we use mutexes inside our kernel? 10/28/1015

Aside: kernel locking  Can we use mutexes inside our kernel?  Sometimes…  Spinlocks more common than semaphores/mutexes in Linux  Reader-writer locks (rwlocks):  Allow multiple readers or single writer  Good idea?  10/28/1016

Condition variables  Let threads block until a certain event occurs (rather than polling)  Associated with some logical condition in program while (x <= y) { sthread_user_cond_wait(cond, lock) } 10/28/1017

Condition variables  Operations:  wait: sleep on wait queue until event happens  signal: wake up one thread on wait queue  Explicitly called when event/condition has occurred  broadcast: wake up all threads on wait queue 10/28/1018

Condition variables sthread_user_cond_wait(sthread_cond_t cond, sthread_mutex_t 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 (by signal/broadcast)  So, must acquire lock before calling wait()!  Read man page for pthread_cond_[wait|signal|broadcast] 10/28/1019

Example synchronization problem  Late-Night Pizza  A group of students study for CSE 451 exam  Can only study while eating 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 10/28/1020

Late-night pizza  Each student thread executes the following: while (must_study) { pick up a piece of pizza; study while eating the pizza; } 10/28/1021

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 10/28/1022

Semaphore/mutex solution  Shared data: semaphore_t pizza; //Number of available //pizza resources; //init to 0 semaphore_t deliver; //init to 1 int num_slices = 0; mutex_t mutex; //guards updating of //num_slices 10/28/1023

student_thread { while (must_study) { wait(pizza); acquire(mutex); num_slices--; if (num_slices==0) signal(deliver); release(mutex); study(); } delivery_guy_thread { while (employed) { wait(deliver); make_pizza(); acquire(mutex); num_slices=S; release(mutex); for (i=0;i<S;i++) signal(pizza); }

Condition variable solution  Shared data: int slices=0; bool has_been_ordered; Condition order; //an order has been //placed Condition deliver; //a delivery has //been made Lock mutex; //protects “slices”; //associated with //both Condition //variables 10/28/1025

Student() { while(diligent) { mutex.lock(); if (slices > 0) { slices--; } else { if (!has_been_ordered){ order.signal(mutex); has_been_ordered = true; } deliver.wait(mutex); } mutex.unlock(); Study(); } DeliveryGuy() { while(employed) { mutex.lock(); order.wait(mutex); makePizza(); slices = S; has_been_ordered = false; mutex.unlock(); deliver.broadcast(); }

Monitors  An object that allows one thread inside at a time  Contain a lock and some condition variables  Condition variables used to allow other threads to access the monitor while one thread waits for an event to occur 10/28/1027

Monitors 10/28/1028 shared data f() { … } g() { … } h() { … } Entry set: queue of threads trying to enter the monitor CV operations (procedures) At most one thread in monitor at a time CV Wait sets

Monitors in Java  Each object has its own monitor Object o  The Java monitor supports two types of synchronization:  Mutual exclusion synchronized(o) { … }  Cooperation synchronized(o) { O.wait(); } synchronized(o) { O.notify(); } 10/28/1029

10/28/1030

Semaphores vs. CVs Semaphores  Used in apps  wait() does not always block the caller  signal() either releases a blocked thread, if any, or increases semaphore counter Condition variables  Typically used in monitors  wait() always blocks caller  signal() either releases a blocked thread, if any, or the signal is lost forever 10/28/1031