Operating Systems NWEN 301 Lecture 6 More Concurrency.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
CHAPTER3 Higher-Level Synchronization and Communication
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
1 Semaphores and Monitors: High-level Synchronization Constructs.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
Language Support for Concurrency. 2 Common programming errors Process i P(S) CS P(S) Process j V(S) CS V(S) Process k P(S) CS.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Synchronization: Monitors Hank Levy. 6/21/20152 Synchronization with Semaphores Semaphores can be used to solve any of the traditional synchronization.
Language Support for Concurrency. 2 Announcements CS 415 project 1 due today! CS 414 homework 2 available due next week Midterm will be first week in.
Jonathan Walpole Computer Science Portland State University
Semaphores CSCI 444/544 Operating Systems Fall 2008.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
Internet Software Development Controlling Threads Paul J Krause.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Lecture 6: Synchronization (II) – Semaphores and Monitors
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
CSE 451: Operating Systems Winter 2014 Module 8 Semaphores, Condition Variables, and Monitors Mark Zbikowski Allen Center 476 ©
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Chapter 71 Monitors (7.7)  A high-level-language object-oriented concept that attempts to simplify the programming of synchronization problems  A synchronization.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
Jonathan Walpole Computer Science Portland State University
CS510 Operating System Foundations
CS510 Operating System Foundations
Uniprocessor Lock Implementation
The Critical-Section Problem (Two-Process Solution)
Lecture 25 Syed Mansoor Sarwar
Semaphore Originally called P() and V() wait (S) { while S <= 0
Synchronization Hank Levy 1.
Critical section problem
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitor Giving credit where it is due:
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Autumn Module 8 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
Synchronization Hank Levy 1.
Synchronization: Monitors
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
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CIS 720 Lecture 6.
Monitors and Inter-Process Communication
Don Porter Portions courtesy Emmett Witchel
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

Operating Systems NWEN 301 Lecture 6 More Concurrency

Announcements Homework #1 - Due tonight Tutorial tomorrow 2

Recall: Circular bounded buffers data producer A nextIn nextOut consumer 3 producer B

A Race condition. Imagine this execution sequence. Producer#AProducer#Bvariable++;LDA #0x0111INCASTA #0x NWEN

Critical Regions in play 5 NWEN Producer A Producer B

Simple Prod/Con Use a lock (a binary semaphore -- mutex of 1) boolean produce(int x){ P(mutex); buf[in] = x; in = (in++) % N; V(mutex); return true; } boolean consume(int &x){ P(mutex); x = buf[out]; out = (out++) % N; V(mutex); return true; } 6 What about handling full/empty queues?

Semaphore Synch Consider a clever solution to the producer-consumer using counting semaphores to manage the buffers… semaphore mutex = 1, empty =N and full = 0; boolean produce(int x){ P(empty); P(mutex); buf[in] = x; in = (in++) % N; V(mutex); V(full); return true; } boolean consume(int &x){ P(full); P(mutex); x = buf[out]; out = (out++) % N; V(mutex); V(empty); return true; } 7

That was tricky! And gets worse with more complex scenarios. The use of 3 separate semaphores suggest the need for better synch primitives: Condition Variables Monitors Critical Regions These constructs lead not only to safer, but also to neater code and fewer bugs but first… 8

Pintos, Semaphores struct semaphore { unsigned value; /* Current value. */ struct list waiters; /* List of waiting threads. */ }; void sema_init (struct semaphore *, unsigned value); void sema_down (struct semaphore *); /* Really a P() */ void sema_up (struct semaphore *); /* Really a V() */ bool sema_try_down (struct semaphore *); 9

Pintos, Semaphores void sema_init (struct semaphore *sema, unsigned value) { ASSERT (sema != NULL); sema->value = value; list_init (&sema->waiters); /* Initialise a list/queue for waiting threads */ } 10

Pintos, Semaphores P() void sema_down (struct semaphore *sema) { /* Really P() */ enum intr_level old_level; ASSERT (sema != NULL); ASSERT (!intr_context ()); old_level = intr_disable (); while (sema->value == 0){ list_push_back (&sema->waiters, &thread_current ()->elem); thread_block (); } sema->value--; intr_set_level (old_level); } 11

Pintos, Semaphores V() void sema_up (struct semaphore *sema) {/* Really V */ enum intr_level old_level; ASSERT (sema != NULL); old_level = intr_disable (); if (!list_empty (&sema->waiters)) thread_unblock (list_entry (list_pop_front (&sema->waiters), struct thread, elem)); sema->value++; intr_set_level (old_level); } 12

Pintos, Semaphores bool sema_try_down (struct semaphore *sema) { enum intr_level old_level; bool success; ASSERT (sema != NULL); old_level = intr_disable (); if (sema->value > 0) { sema->value--; success = true; } else success = false; intr_set_level (old_level); return success; } 13

Condition Variables Are a higher level synchronization primitive. It makes coding of synchronization: more structured, easier to follow, and debug. It does not enforce mutual-exclusion, it needs a lock or semaphore as a mutex. 14

Condition Variables The general problem is the need to wait for some condition to become true within a critical section. For the producer the condition is that the buffer is not full. For the consumer the condition is that the buffer is not empty. For this we use condition variables. 15

Condition Variables Used to wait for a condition to be true Essentially just a container for waiting threads Functions: Wait(condition, lock): release the lock, put thread to sleep until condition signaled. Signal(condition, lock): wake a thread waiting for the condition. Caller must hold the lock. Broadcast(consition, lock): same as signal, but wakes all threads. 16

struct lock *mutex; // this is a class struct condition *notFull, *notEmpty;// Condition var struct buffer *buf; // buffer } Shared Prod/Con with CV bool producer(int x){ P(mutex); while(isFull(buf)) cond_wait(notFull, mutex); put(buf, x); cond_signal(notEmpty, mutex); V(mutex); return true; } bool consumer(int &x){ P(mutex); while(isEmpty(buf)) cond_wait(notEmpty, mutex); x = get(buf); cond_signal(notFull, mutex); V(mutex); return true; } 17

Monitors A monitor is like an object, with all state (data) private and a set of methods. With the addition of synchronisation, where invoking any method results in mutual exclusion over the entire object. You can also use monitors to control access to data not encapsulated in the monitor (acting as a gatekeeper). 18

Monitors Think of it as a thread-safe object. Multiple threads can use the object. However, at any one time only one thread may be executing any of its methods. The object has a set of methods and CVs. CVs allow threads to give up exclusive access while they wait for a condition. 19

The Monitor Shared Data Methods Constructor CV buf[] Waiting Threads 20

CVs in Monitors Essentially the monitor provides mutual exclusion. In addition we need additional constructs to allow us to wait on conditions (like CVs). Condition x;// Condition is a type x->wait(); // suspend process x->signal();// wake exactly one process 21

Monitor PC example Monitor ProducerConsumer{ public: ProducerConsumer(); bool produce(int x); bool consume(int *x); private: Buffer *buf; Condition notFull, notEmpty; }; Monitor:: produce(int x){ while(buffer->isFull()) notFull.wait(); buffer->add(x); notEmpty.signal(); } Monitor::consume(int *x){ while(buffer->isEmpty()) notEmpty.wait(); *x = buffer->remove(); notFull.signal(); } 22

Monitor use ProducerConsumer pc = new ProducerConsumer();// shared … // producer thread// consumer threadwhile(true){ pc.produce(rand()); pc.consume();} 23

CVs in Monitors What happens when a signal is issued? The unblocked process will be placed on the ready queue and resume from the statement following the wait. This may violate mutual exclusion with both the signaler and signaled process executing in the monitor. 24

Monitor CV Semantics We have two choices: The signaling process must wait until the signaled process has left the monitor, or waits on another condition. The signaled process must wait for the signaler to leave the monitor or wait on another condition. 25

A Pintos CV Implementation struct condition { struct lock *lock; /* A lock with which this condition variable is associated */ struct semaphore s; /* The semaphore which I will use for storage */ int wait_count; }; void cond_init (struct condition *cond){ ASSERT (cond != NULL); cond->lock = NULL;/* lock associated with this thread */ cond->wait_count = 0; sema_init(&cond->s, 0); } 26

A Pintos CV Implementation Note, also wrapped the pintos functions: void P (struct semaphore *sema) { sema_down(sema); } void V (struct semaphore *sema) { sema_up(sema); } 27

A Pintos CV Implementation void cond_wait (struct condition *cond, struct lock *lock) { /* Kris didn't like the original impl, so he rewrote it (except for the asserts) */ /* if no lock associated, associate the one passed in, this should only happen when the cond is empty */ if(!cond->lock) cond->lock = lock; lock_release (lock); cond->wait_count++; /* Gotta do this before P(), otherwise it will hang */ P(&cond->s); lock_acquire (lock); } 28

A Pintos CV Implementation void cond_signal (struct condition *cond, struct lock *lock UNUSED) { ASSERT (lock_held_by_current_thread (cond->lock)); /* If there is something waiting, signal it - otherwise no need */ if(cond->wait_count){ cond->wait_count--; /* Gotta do this before V(), otherwise can be interupted with horrible results */ V(&cond->s); if(!cond->wait_count){ /* The CV is now empty, we can disassociate the lock */ cond->lock = NULL; } } } 29