Process Synchronization: Semaphores

Slides:



Advertisements
Similar presentations
Module 6: Process Synchronization
Advertisements

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.
Synchronization Principles Gordon College Stephen Brinton.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
What we will cover… Process Synchronization Basic Concepts
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Synchronization Solutions
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrency, Mutual Exclusion and Synchronization.
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.
Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
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: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
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 CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Chapter 6: Process Synchronization
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
COT 4600 Operating Systems Fall 2009
Process Synchronization
Chapter 5: Process Synchronization
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Chapter 6: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
Presentation transcript:

Process Synchronization: Semaphores CSSE 332 Operating Systems Rose-Hulman Institute of Technology Hand out Semaphore Quiz. Quiz due on Friday at start of class. 1

Critical-section problem solution Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes A critical section is a section of code in each process that may be changing a common variable, updating a shared table, writing a file, and so on. The critical-section problem is to design a protocol that the process can use to cooperate. A solution to the critical-section problem must satisfy the following three requirements: 2

Structure of a typical process do { entry section critical section exit section remainder section } while (TRUE); Protocol description: Each process must request permission to enter its critical section (code in which this request is made --- entry section) Critical section code may be followed by an exit section. The remaining code is called the remainder section. Entry section and remainder section are highlighted. 3

Four different approaches Software-defined approaches Hardware support Support from the OS Support from the programming language Four different approaches for solving the critical-section problem. We are going to discuss support from the OS. 4

Semaphore Synchronization tool that does not require busy waiting Semaphore S – integer variable Two standard operations modify S: wait() and signal() Less complicated than using hardware instructions Can only be accessed via two indivisible (atomic) operations wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; Notes Questions: What is busy waiting and why can it be a bad thing? Semaphore S in this case is a binary semaphore or a mutex because the values cannot be negative (should be 0 and 1). If s is incremented beyond 1, it is a counting semaphore. 5

Semaphore as General Synchronization Tool 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 Provides mutual exclusion Semaphore mutex; // initialized to 1 do { wait (mutex); // Critical Section signal (mutex); // remainder section } while (TRUE); Can implement a counting semaphore as a binary semaphore. Binary semaphores are called mutex locks because they are locks that provide mutual exclusion. Notes Questions: Describe how this code follows the structure of a typical process in an effort to solve the critical-section problem. Notes Questions: How do we use wait() and signal() to control access to the critical section? 6

Semaphore implementation Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section. Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Note that applications may spend lots of time in critical sections and therefore this is not a good solution. So how should the OS implement wait() and signal()? Third bullet is especially true for wait() since it contains a spin loop. 7

Semaphore Implementation with no busy wait Define a semaphore as a struct typedef struct { int value; struct process *queue; } semaphore; Assume same simple operations: wait(S) may block the process that invokes it signal(S) resumes the execution of a blocked process P This is a much better implementation since busy waiting is ineffective and consumes CPU time. Its really 3 operations because you have to initialize it. 8

Implementation wait(S): S.value--; if (S.value < 0) { add this process P to S.queue; block(P); } signal(S): S.value++; if (S.value <= 0) { remove a process P from S.queue; wakeup(P); Block() and wakeup() are two system calls. 9

Allowing multiple Critical Sections Counting semaphores s.value >= 0 - # of processes that can execute wait(s) without blocking Assumption: no signal(s) executed Each can enter its critical section. s.value < 0 - # of processes blocked on s These blocked processes are in s.queue 10

Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P0 P1 wait (S); wait (Q); wait (Q); wait (S); . . . . signal (S); signal (Q); signal (Q); signal (S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process Notes Questions: What events would processes P0 and P1 be waiting for? How does this wait cause a deadlock? 11

Classical Synchronization Problems Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem Today we will explore using semaphores to solve a sample bounded buffer problem. 12

Bounded-Buffer Problem N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N 13

Bounded Buffer Problem (Cont.) The structure of the producer process do { // produce an item in nextp wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (TRUE); 14

Bounded Buffer Problem (Cont.) The structure of the consumer process do { wait (full); wait (mutex); // remove an item from buffer in nextc signal (mutex); signal (empty); // consume the item in nextc } while (TRUE); Notes Questions: Briefly describe the bounded-buffer problem. 15