CSE 451 Section 4. 2 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, semaphor, condition var.

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)
Synchronization and Deadlocks
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
 Read about Therac-25 at  [  [
Ch 7 B.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
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.
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.
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.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
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.
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
Jonathan Walpole Computer Science Portland State University
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.
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.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
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.
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.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
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.
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.
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.
© 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.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
Interprocess Communication Race Conditions
CS703 – Advanced Operating Systems
Operating Systems CMPSC 473
Section 5 Project 1 Recap Synchronization primitives
Jonathan Walpole Computer Science Portland State University
Lecture 14: Pthreads Mutex and Condition Variables
Lecture 2 Part 2 Process Synchronization
Critical section problem
Implementing Mutual Exclusion
Lecture 14: Pthreads Mutex and Condition Variables
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSE 153 Design of Operating Systems Winter 2019
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
CSE 542: Operating Systems
CSE 542: Operating Systems
Synchronization and liveness
Presentation transcript:

CSE 451 Section 4

2 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, semaphor, condition var Message passing primitives Low-level support Disable/enable interrupts Atomic instructions (test_and_set)

3 Disabling/Enabling Interrupts Prevents context-switches during execution of critical sections Sometimes necessary E.g. to prevent further interrupts during interrupt handling Many problems Thread B: disable_irq() critical_section() enable_irq() Thread A: disable_irq() critical_section() enable_irq()

4 Disabling/Enabling Interrupts Prevents context-switches during execution of critical sections Sometimes necessary E.g. to prevent further interrupts during interrupt handling Many problems E.g., an interrupt may be shared How does it work on multi-processors? Thread B: disable_irq() critical_section() enable_irq() Thread A: disable_irq() critical_section() enable_irq()

5 Hardware support Atomic instructions: test_and_set Compare-exchange (x86) Use these to implement higher-level primitives E.g. test-and-set on x86 is written using compare-exchange:  compare_exchange(lock_t *x, int y, int z): if(*x == y) *x = z; return y; else return *x;

6 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, futex, semaphor, condition var Message passing primitives Low-level support Disable/enable interrupts Atomic instructions Used to implement higher- level sync primitives (in the kernel typically) Why not use in apps?

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 Mutex is a binary semaphore (capacity 1)

8 Condition Variable A “place” to let threads wait for a certain event to occur while holding a 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 Typically associated with some logical condition in program

9 Semaphores vs. CVs Semaphores Used in apps wait() does not always block the caller signal() either releases a blocked thread, if any, or increases sem. counter. Condition variables Typically used in monitors Wait() always blocks caller Signal() either releases blocked thread(s), if any, or the signal is lost forever.

10 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 (must_study) { 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.

11 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

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

13 Condition Variable Solution 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(); } int slices=0; Condition order, deliver; Lock mutex; bool has_been_ordered = false; DeliveryGuy() { while(employed) { mutex.lock(); order.wait(mutex); makePizza(); slices = S; has_been_ordered = false; mutex.unlock(); deliver.broadcast(); }

Scheduling What if...? Threads were the scheduling entity in your scheduler and you had two processes running. One process spawns two threads and the other spawns one hundred. What kind of problem might occur? Would this act fairly? How could we make it “fair”?