1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.

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.
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
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 ©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)
Interprocess Communication
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
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.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
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.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
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:
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
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.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Reference –text: Tanenbaum ch
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
pThread synchronization
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
CMSC621: Advanced Operating Systems Advanced Operating Systems
Background on the need for Synchronization
CIS Operating Systems Synchronization
Process Synchronization
Chapter 5: Process Synchronization – Part II
Thread synchronization
Lecture 14: Pthreads Mutex and Condition Variables
Chapter 30 Condition Variables
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
Lecture 14: Pthreads Mutex and Condition Variables
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CSCI1600: Embedded and Real Time Software
“The Little Book on Semaphores” Allen B. Downey
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof. Mark Corner and Prof. Emery Berger’s OS course at Umass Amherst Assistant Professor, University of Maryland Baltimore County

2 Announcements Project 1 due on Oct 7 th Homework 2 is out (due Oct 13 th ) Readings from Silberchatz [6 th chapter]

3 Test and Set semantics What’s the effect of testAndset(value) when: value = 0? (“unlocked”) value = 1? (“locked”) int testAndset (int* v) { int old = *v; *v = 1; return old; }

4 Blocking Locks  Suspend thread immediately –Lets scheduler execute another thread –Go to back of run queue or wait to be woken  Minimizes time spent waiting  But: always causes context switch void blockinglock (Lock* l) { while (testAndSet(l.v) == 1) { sched_yield(); }

5 Spin Locks  Instead of blocking, loop until released void spinlock (Lock* l) { while (testAndSet(l.v) == 1) { ; }

6 Other variants  Spin-then-yield : –Spin for some time, then yield Fixed spin time Exponential backoff

7 Safety  Locks can enforce mutual exclusion, but notorious source of errors –Failure to unlock –Double locking –Deadlock (its own lecture) –Priority inversion not an “error” per se pthread_mutex_t l; void square (void) { pthread_mutex_lock (&l); // acquires lock // do stuff if (x == 0) { return; } else { x = x * x; } pthread_mutex_unlock (&l); }

8 Bounded Buffer or Producer Consumer Problem  Suppose we have a thread-safe queue –insert(item), remove(), empty() –must protect access with locks  Consumer  Consumes items in the queue  Only if queue has items  Producer:  Produces items  Adds them only if the queue is not full A simple case: max size of queue is 1

9 Solution (sleep?)  Sleep = –“don’t run me until something happens ”  What about this? Dequeue(){ lock(); if (queue empty) { sleep(); } take one item; unlock(); } Enqueue(){ lock(); insert item; if (thread waiting) wake up dequeuer(); unlock(); }

10 Another solution  Does this work? Dequeue(){ lock(); if (queue empty){ unlock(); sleep(); remove item; } else unlock; } Enqueue(){ lock(); insert item; if (thread waiting) wake up dequeuer(); unlock(); }

11 Conditional variables  Make it possible/easy to go to sleep –Atomically: release lock put thread on wait queue go to sleep  Each cv has a queue of waiting threads  Worry about threads that have been put on the wait queue but have NOT gone to sleep yet? –no, because those two actions are atomic  Each condition variable associated with one lock

12 Conditional variables  Wait for 1 event, atomically release lock –wait(Lock& l, CV& c) If queue is empty, wait –Atomically releases lock, goes to sleep –You must be holding lock! –May reacquire lock when awakened (pthreads do) –signal(CV& c) Insert item in queue –Wakes up one waiting thread, if any –broadcast(CV& c) Wakes up all waiting threads  Monitors = locks + condition variables –Sometimes combined with data structures Lets take a look at a demo

13 Producer-consumer problem using pthread conditionals void * consumer (void *){ while (true) { pthread_mutex_lock(&l); while (q.empty()){ pthread_cond_wait(&nempty, &l); } cout << q.pop_back() << endl; pthread_mutex_unlock(&l); } void * producer(void *){ while (true) { pthread_mutex_lock(&l); q.push_front (1); pthread_cond_signal(&nempty); pthread_mutex_unlock(&l); }

14 Semaphores  Computer science: Dijkstra (1965) A non-negative integer counter with atomic increment & decrement. Blocks rather than going negative.

15 Semaphore Operations  P(sem), a.k.a. wait = decrement counter –If sem = 0, block until greater than zero –P = “prolagen” (proberen te verlagen, “try to decrease”)  V(sem), a.k.a. signal = increment counter –Wake 1 waiting process –V = “verhogen” (“increase”)

16 How do you implement a mutex using semaphores  More elegant than locks –Mutual Exclusion and Ordering –By initializing semaphore to 0, threads can wait for an event to occur thread A // wait for thread B sem.wait(); // do stuff … thread B // do stuff, then // wake up A sem.signal();

17 Counting Semaphores  Controlling resources: –E.g., allow threads to use at most 5 files simultaneously Initialize to 5 thread A sem.wait(); // use a file sem.signal(); thread B sem.wait(); // use a file sem.signal();

18 An in-class discussion (producer consumer problem using sem)