Homework 3-4 Sarah Diesburg Operating Systems COP 4610.

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.
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
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.
1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
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.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
More Synchronization, Semaphores Vivek Pai / Kai Li Princeton University.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
Chapter 2.3 : Interprocess Communication
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.
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 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Homework 3-4 Sarah Diesburg Operating Systems COP 4610.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
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.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Practice Chapter Five.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
© 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.
Operating Systems NWEN 301 Lecture 6 More Concurrency.
Synchronization Exercises. Exercise 1 r Let S be a semaphore that is initialized to 2 r Consider the following: down(S) up(S) down(S) r Does this program.
CS703 - Advanced Operating Systems
Scheduling Review FIFO: RR: + simple
Sarah Diesburg Operating Systems COP 4610
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Monitors, Condition Variables, and Readers-Writers
Introduction to Operating Systems
Lecture 13: Producer-Consumer and Semaphores
Sarah Diesburg Operating Systems COP 4610
Andy Wang Operating Systems COP 4610 / CGS 5765
Semaphores and Bounded Buffer
Process Synchronization
Global Environment Model
Implementing Mutual Exclusion
Implementing Mutual Exclusion
Lecture 13: Producer-Consumer and Semaphores
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Review The Critical Section problem Peterson’s Algorithm
Sarah Diesburg Operating Systems CS 3430
Presentation transcript:

Homework 3-4 Sarah Diesburg Operating Systems COP 4610

1. All Possible Execution Orders Thread AThread B x = 3;y = 2; x = y - 1;y = x + 1; x = 3y = 2x = y - 1y = 2 y = x + 1 x = y - 1y = x + 1 x = y - 1 x = 3y = x + 1 x = 3 x = y - 1 (x = ?, y = ?) (x = 3, y = ?) (x = ?, y = ?) (x = ?, y = 2) (x = ?, y = ?) (x = 3, y = 2) (x = ?, y = 2) (x = ?, y = ?) (x = 1, y = 2) (x = 3, y = 4) (x = 3, y = ?) (x = ?, y = ?)

2. Why does disabling interrupts not work on multi-processors? Interrupts are disabled on a per-CPU basis A thread running on another CPU could enter the critical area

3. Too Much Milk w/ Busy Waits //define the busy-wait locks value = 0; Lock::Acquire() { // while the previous value is BUSY, loop while (test_and_set(value) == 1); } Lock::Release() { value = 0; }

3. (cont.) //Dumb Lock::Acquire(value) if(no milk) { //go get milk Lock::Release(value) //Dumber Lock::Acquire(value) if(no milk) { //go get milk Lock::Release(value)

4. Does this work? // consumer waits on 0 semaphore nLoadedBuffers = 0; // producer waits on 0 semaphore nFreeBuffers = N; // N >= 2 // one thread waits when another thread is // modifying the buffer semaphore mutex = 1; Producer() { 1. P(nFreeBuffers); 2. P(mutex); 3. // put 1 item in the buffer 4. V(nLoadedBuffers); 5. V(mutex); } Consumer() { 6. P(nLoadedBuffers); 7. P(mutex); 8. // take 1 item from the buffer 9. V(mutex); 10. V(nFreeBuffers); }

4. Does this work? // consumer waits on 0 semaphore nLoadedBuffers = 0; // producer waits on 0 semaphore nFreeBuffers = N; // N >= 2 // one thread waits when another thread is // modifying the buffer semaphore mutex = 1; Producer() { 1. P(nFreeBuffers); 2. P(mutex); 3. // put 1 item in the buffer 4. V(nLoadedBuffers); 5. V(mutex); } Consumer() { 6. P(nLoadedBuffers); 7. P(mutex); 8. // take 1 item from the buffer 9. V(mutex); 10. V(nFreeBuffers); }

4. Does this work? Actually, yes. Even if producer thread hits #4 and wakes up a consumer sleeping on #6, the consumer will not be able to enter the critical section until the producer comes back and hits #5

Additional Things

Race Conditions – An anology Suppose there is a bag of chips in the kitchen at home and I want to make “walking tacos” for dinner

Race Conditions – An anology Suppose there is a bag of chips in the kitchen at home and I want to make “walking tacos” for dinner If my husband gets home first, the bag of chips will be consumed before dinner  My dinner plans are foiled! If I get home first, I can use the bag of chips for dinner