CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CAS3SH3 Midterm Review. The midterm 50 min, Friday, Feb 27 th Materials through CPU scheduling closed book, closed note Types of questions: True & False,
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
PROCESS SYNCHRONIZATION READINGS: CHAPTER 5. ISSUES IN COOPERING PROCESSES AND THREADS – DATA SHARING Shared Memory Two or more processes share a part.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Mutual Exclusion: Primitives and Implementation Considerations.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
CS162 Operating Systems and Systems Programming Lecture 7 Mutual Exclusion, Semaphores, Monitors, and Condition Variables September 22, 2008 Prof. John.
CS162 Operating Systems and Systems Programming Lecture 7 Mutual Exclusion, Semaphores, Monitors, and Condition Variables September 21, 2005 Prof. John.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
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.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
Threads and Critical Sections Vivek Pai / Kai Li Princeton University.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
CS510 Concurrent Systems Introduction to Concurrency.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Operating Systems Review for Chap.6 & 7 Hung Q. Ngo KyungHee University Spring 2009
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
COSC 3407: Operating Systems Lecture 7: Implementing Mutual Exclusion.
CS 162 Discussion Section Week 2 (9/16 – 9/20). Who am I? Kevin Klues Office Hours:
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
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.
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.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations, Locks, Semaphores September 12, 2011 Anthony D. Joseph and.
CPS110: Reader-writer locks
CS703 – Advanced Operating Systems
Chapter 5: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
CS162 Operating Systems and Systems Programming Lecture 7 Mutual Exclusion, Semaphores, Monitors, and Condition Variables September 21, 2009 Prof. John.
CSCI 511 Operating Systems Chapter 5 (Part B) Mutual Exclusion, Semaphores Dr. Frank Li.
January 31, 2011 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations,
Anthony D. Joseph and Ion Stoica
CS162 Operating Systems and Systems Programming Lecture 7 Mutual Exclusion, Semaphores, Monitors, and Condition Variables February 8, 2006 Prof. Anthony.
Anthony D. Joseph and John Canny
Critical section problem
Implementing Mutual Exclusion
February 6, 2013 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Implementing Mutual Exclusion
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
September 19, 2018 Prof. Ion Stoica
Presentation transcript:

CS 162 Discussion Section Week 3

Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM W && 9-10AM F Research Datacenter Networks Cloud Computing

Project 1 Can be found in the course website – Under the heading “Projects and Nachos” Stock Nachos has an incomplete thread system. Your job is to – complete it, and – use it to solve several synchronization problems

Project 1 Grading Design docs [40 points] – First draft [10 points] – Design review [10 points] – Final design doc [20 points] Code [60 points]

Design Document Overview of the project as a whole along with its parts Header must contain the following info – Project Name and # – Group Members Name and ID – Section # – TA Name

Design Document Structure Each part of the project should be explained using the following structure Overview Correctness Constraints Declarations Descriptions Testing Plan

Design Document First draft [9 th Feb] – Initial ideas – At most 10 pages Final draft [22 nd Feb] – At most 15 pages Include diagram showing interactions between system components Talking much about oneself can also be a means to conceal oneself -- Friedrich Nietzsche Talking too much about your design is a means to conceal your ideas. Overdo it to lose 20%. -- CS162 Teaching Staff

Project 1 Deadlines Initial design: 9 th Feb Design reviews: Week of 13 th Feb Code: 21 st Feb Group evaluations, test cases, and final design docs: 22 nd Feb 1.Signup for a timeslot in your section. 2.If anyone is absent, everyone loses 20% on the whole project

Synchronization. Say what?!

Definitions Synchronization: using atomic operations to ensure cooperation between threads Mutual Exclusion: ensuring that only one thread does a particular thing at a time – One thread excludes the other while doing its task Critical Section: piece of code that only one thread can execute at once – Critical section is the result of mutual exclusion – Critical section and mutual exclusion are two ways of describing the same thing

Where are we going with synchronization?

We are going to implement various higher-level synchronization primitives using atomic operations –Everything is pretty painful if only atomic primitives are load and store –Need to provide primitives useful at user-level Load/Store Disable Ints Test&Set Comp&Swap Locks Semaphores Monitors Send/Receive Shared Programs Hardware Higher-level API Programs

Examples of Read-Modify-Write test&set (&address) { /* most architectures */ result = M[address]; M[address] = 1; return result; } swap (&address, register) { /* x86 */ temp = M[address]; M[address] = register; register = temp; } compare&swap (&address, reg1, reg2) { /* */ if (reg1 == M[address]) { M[address] = reg2; return success; } else { return failure; } }

Implementing Locks with test&set Simple solution: int value = 0; // Free Acquire() { while (test&set(value)); // while busy } Release() { value = 0; } Simple explanation: –If lock is free, test&set reads 0 and sets value=1, so lock is now busy. It returns 0 so while exits –If lock is busy, test&set reads 1 and sets value=1 (no change). It returns 1, so while loop continues –When we set value = 0, someone else can get lock Busy-waiting

test&set without busy-waiting? => Nope Only busy-wait to atomically check lock value Note: sleep has to be sure to reset the guard variable –Why can’t we do it just before or just after the sleep? Release() { // Short busy-wait time while (test&set(guard)); if anyone on wait queue { take thread off wait queue Place on ready queue; } else { value = FREE; } guard = 0; int guard = 0; int value = FREE; Acquire() { // Short busy-wait time while (test&set(guard)); if (value == BUSY) { put thread on wait queue; go to sleep() & guard = 0; } else { value = BUSY; guard = 0; } }

Life without locks?

Semaphores A Semaphore has a non-negative integer value(S) and supports the following two operations – P(S) = Down(S) = Wait(S) – V(S) = Up (S) = Signal(S) Note that P() stands for “proberen” (to test) and V() stands for “verhogen” (to increment) in Dutch

Classical definition of Wait and Signal Wait(S) { while (S <= 0) { } S = S – 1; } Signal(S) { S = S + 1; } Busy-waiting

Blocking implementation of Semaphore Wait(S) { S.val = S.val – 1; if (S.val < 0) { S.list.add(calling_thread); sleep(); } Signal(S) { S.val = S.val + 1; if (S.val <= 0) { T = S.list.removeHead(); wakeup(T); } Initialize(S, X) { S.val = X }

Mutex Used to control access to shared data – Only one thread can execute inside a Mutex – Others are blocked until the Mutex is unlocked Can be implemented using Semaphore – Just initialize your Semaphore to 1

Condition Variables (CV) Used to wait for specific events; e.g., – When free memory is too low; wake up the garbage collector – New packet arrived from the network; push it to appropriate handlers Each CV has a single associated Mutex – Condition of the CV depends on data protected by the Mutex

Condition Variables Semantics Wait – Atomically unlocks the Mutex and blocks the thread Signal – Thread is awaken inside Wait – Tries to Lock the Mutex – When it (finally) succeeds, returns from Wait

CV Example Mutex io_mutex; Condition non_empty; Consumer: Lock (io_mutex) { while (port.empty()) Wait(io_mutex, non_empty); process_data(port.first_in()); } Producer: Lock (io_mutex) { port.add_data(); Signal(non_empty); }