Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems Part III: Process Management (Process Synchronization)
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
Chapter 6: Process Synchronization
Secure Operating Systems Lesson 5: Shared Objects.
Discussion Week 5 TA: Kyle Dewey. Overview HW 3.10 and 6.2 review Binary formats System call execution in NACHOS Memory management in NACHOS I/O in NACHOS.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
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.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
Synchronization CSCI 444/544 Operating Systems Fall 2008.
CS510 Concurrent Systems Introduction to Concurrency.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Games Development 2 Concurrent Programming CO3301 Week 9.
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.
CSE 451: Operating Systems Section 5 Midterm review.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
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.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
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.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Thread Synchronization
PROCESS MANAGEMENT IN MACH
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Atomic Operations in Hardware
Inter-Process Communication and Synchronization
Threading And Parallel Programming Constructs
Background and Motivation
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Thread Synchronization including Mutual Exclusion
Kernel Synchronization II
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
CSCI1600: Embedded and Real Time Software
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Discussion Week 3 TA: Kyle Dewey

Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1

Concurrency Looks easy Really hard to get right Really hard No seriously, borderline impossible

Race Condition Different results are possible based on different process/thread orderings Ordering may be correct % of the time

Deadlock Two processes/threads wait for each other to do something While they wait, they do not do whatever it is they are waiting for Potential outcome of a race condition

(Sort of) Real Deadlock Example

Critical Region A point in code where the ordering matters Almost always this is some state that is shared between processes/threads Client connect to server:port1 connect to server:port2 do something with both Server accept from port1 accept from port2 do something with both

Fixing the Problem Do not share state Only share read-only state Carefully regulate write access to shared state

Regulation A critical region can be manipulated by only one thread at a time Need a way to enforce that at most one thread at any time point is in such a region

Solving in Java Java provides the synchronized keyword for blocks Only one thread at a time may access a block marked with the synchronized keyword int x = 0; public synchronized void set( int y ) {x = y;} public int get() {return x;}

Who cares about Java? Many concurrency primitives work exactly like this, just with a little more work One call upon entrance to critical region, another upon exit The entrance and exit are implicit through blocks with Java

Semaphores Simply a shared integer One call decrements, another increments By convention, 0 is locked, and values > 0 are unlocked Values < 0 mean?

Semaphores Increment/decrement are atomic - they are uninterruptible The highest possible number it can hold is equal to the max number of callers to the region it protects

Usage Example

Fix the Notebook Problem

NACHOS Semaphore Methods P() : wait until the value is > 0, then decrement V() : increment the value, waking up any waiting threads

NACHOS Semaphore Implementation

Spinlock Alternative to blocking A.K.A. busy waiting “Spin” in a tight loop More efficient for short critical regions

Everything In Between May spinlock under certain conditions May schedule differently if in a locked state Implementation can do whatever it wants

Project 1 Task 1 Experiment according to instructions Explain the execution of multithreaded code Add semaphores and contrast the difference

Project 1 Task 2 Implement locks - essentially semaphores with a maximum of one caller at a time Given all the semaphore code to look at Hint hint it is a special case of a semaphore

Lock Methods Acquire() : calling thread waits until lock is available, then grabs the lock Release() : calling threads gives up the lock

Lock vs. Semaphore Locks permit at most one thread in a region, not n Locks make sure that only the thread that grabs the lock can release the lock

Lock Example

Project 1 Task 3 Implement conditions Requires a correct Lock implementation

Conditions Allow a group of threads to synchronize on a given condition Until the condition is true, they wait

Condition Methods Wait( lock ) : release the given lock, wait until signaled, and acquire the lock Signal( lock ) : wake up any single thread waiting on the condition Broadcast( lock ) : wake up all threads waiting on the condition

Condition Semantics The lock should be owned by the calling thread Only reason why the reference implementation’s Signal() and Broadcast() needs the lock Signal() and Broadcast() require that the lock is currently held

Condition Example - Broadcast

Condition Example - Signal

Project 1 Task 4 Identify and describe a race condition in a given section of code Fix the race condition using semaphores Fix it another way using locks and/or conditions

Identifying Race Conditions NACHOS is more or less deterministic Some of the hardest errors to find

Project Tips Start early Use the given implementation as a guide Overcomplicated Buggy Ugly The Print() method is a lifesaver

FAQ

“What’s the difference?” Not much Possible to implement some in terms of others Some may be more natural in different contexts

“Are these even working?” If everything is done correctly, the output remains the same for first task NACHOS thread scheduler is simple No interrupts All threads are part of the same program

“Why bother?” Change any of the aforementioned things, and it will matter big time Later projects will need this for correctness Gentle introduction to concurrency and synchronization primitives

“Conditions make no sense!” Name most people are used to: monitors samples.com/showtutorial.php?tutoriali d=306 has an excellent example of usage (Java standpoint. Examples were adapted from this.) samples.com/showtutorial.php?tutoriali d=306