Operating Systems CMPSC 473

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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
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.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Synchronization Semaphores
Interprocess Communication Race Conditions
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
CS703 – Advanced Operating Systems
COT 4600 Operating Systems Fall 2009
Process Synchronization
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
CS510 Operating System Foundations
Critical Section and Critical Resources
Critical Section and Critical Resources
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Synchronization Hank Levy 1.
Lecture 2 Part 2 Process Synchronization
Critical section problem
Concurrency: Mutual Exclusion and Process Synchronization
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
“The Little Book on Semaphores” Allen B. Downey
CSE 542: Operating Systems
Presentation transcript:

Operating Systems CMPSC 473 Mutual Exclusion Lecture 12: October 7, 2010 Instructor: Bhuvan Urgaonkar

Agenda Last class Next: Solutions that eliminate busy wait Liveness conditions accompanying mutual exclusion Mutex locks Peterson’s solution: based only on atomic loads/stores Solutions based on test&set and swap atomic instructions Next: Solutions that eliminate busy wait Condition variables Semaphores

Condition Variables Mutex locks waste CPU cycles via busy wait Mutex locks not suitable for making a thread wait till a certain condition becomes true One Solution: Condition variables A condition variable indicates an event and has no value Manipulated using wait() and signal() operations

Condition Variables Wait operation Signal operation When a thread executes a wait call on a condition variable, it is immediately suspended It is now is waiting for the event that is represented by the condition variable to occur Signal operation Eventually, a thread will cause the event to occur after which it will call the signal method on the corresponding condition variable If there are threads waiting on the signaled condition variable, the “monitor” will allow one of the waiting threads to resume its execution If there is no waiting thread on the signaled condition variable, this signal is lost as if it never occured

cond_t not_full, not_empty; int count == 0; Produce() { if (count == N) wait (not_full); … ADD TO BUFFER, count++ … signal (not_empty); } Consume() { if (count == 0) wait (not_empty); … REMOVE FROM BUFFER, count-- … signal (not_full); What is wrong?

NOTE: You can improve this code for more concurrency! cond_t not_full, not_empty; mutex_lock m; int count == 0; Produce() { mutex_lock (m); if (count == N) wait (not_full,m); … ADD TO BUFFER, count++ … signal (not_empty); mutex_unlock (m); } Consume() { if (count == 0) wait (not_empty,m); … REMOVE FROM BUFFER, count-- … signal (not_full); NOTE: You can improve this code for more concurrency!

Condition Variables Wait operation Signal operation When a thread executes a wait call on a condition variable, it is immediately suspended It is now is waiting for the event that is represented by the condition variable to occur Signal operation Eventually, a thread will cause the event to occur after which it will call the signal method on the corresponding condition variable If there are threads waiting on the signaled condition variable, the monitor will allow one of the waiting threads to resume its execution If there is no waiting thread on the signaled condition variable, this signal is lost as if it never occured Always used in conjunction with a mutex lock

Condition Variables pthreads functions/data struct pthread_cond_t condition = PTHREAD_COND_INITIALIZER; or pthread_cont_init (condition, attr) pthread_cond_wait (condition, mutex) pthread_cond_signal (condition)

Semaphores Definition (Dijkstra) ACK: Lot of material borrowed from “The Little Book of Semaphores” by Allen B. Downey Available online (free) at: http://www.greenteapress.com/semaphores/

Semaphores Definition (contd.) Can only be accessed via two indivisible (atomic) operations wait (S) { /* also called decrement */ while S <= 0; // no-op S--; } signal (S) { /* also called increment */ S++; Note: Busy waiting in these definitions, we will see how they can be improved to avoid busy waiting Entry section Exit section

Semaphore Usage (Prelim.) Can only be accessed via two indivisible (atomic) operations wait (S) { /* also called decrement */ while S <= 0; // no-op S--; } signal (S) { /* also called increment */ S++; Provides mutual exclusion Semaphore S; // initialized to 1 wait (S); Critical Section signal (S);

Consequences of the definition In general, there is no way to know before a thread decrements a semaphore whether it will block After a thread increments a semaphore and another thread gets woken up, both threads continue running concurrently. There is no way to know which thread, if either, will continue immediately When you signal a semaphore, you don’t necessarily know whether another thread is waiting, so the number of unblocked threads may be zero or one.

Meaning of Semaphore Values If the value is +, it represents the number of threads that can decrement without blocking If the value is -, it represents the number of threads that are blocked and are waiting If the value is 0, it means there are no threads waiting, but if a thread tries to decrement, it will block

Why Semaphores? Semaphores impose deliberate constraints that help programmers avoid errors Solutions using semaphores are often clean and organized, making it easy to demonstrate their correctness Semaphores can be implemented efficiently on many systems, so solutions that us semaphores are portable and efficient

Counting and Binary Semaphores Counting semaphore – integer value can range over an unrestricted domain Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement Also known as mutex locks Can implement a counting semaphore S as a binary semaphore - will return to this later

Semaphore Usage: Basic Synchronization Patterns Not just for Mutex but many other purposes! Signaling Rendezvous Mutex Multiplex Barrier Re-usable barrier Queues FIFO Queue We will study these

Signaling Initial value of sem = 0 This ensures a1 executes before b1 Why?

Rendezvous We want a1 before b2 and b1 before a2 Hint: Create two semaphores aArrived (indicating A has arrived at the rendevous) and bArrived both initialized to 0;

Rendezvous: Solution 1 Initialization: aArrived = 0; bArrived = 0;

Rendezvous: Solution 2 Initialization: aArrived = 0; bArrived = 0;

Rendezvous: Solutions 1 and 2 Compared Initialization: aArrived = 0; bArrived = 0; Which is likely more efficient? Hint: Solution 2 might require one extra context switch