Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.

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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems Part III: Process Management (Process Synchronization)
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Race Conditions. Isolated & Non-Isolated Processes Isolated: Do not share state with other processes –The output of process is unaffected by run of other.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Race Conditions Critical Sections Deker’s Algorithm.
Critical Sections with lots of Threads. Announcements CS 414 Homework due today.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Race Conditions Critical Sections Dekker’s Algorithm.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
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.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Chapter 2.3 : Interprocess Communication
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,
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.
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.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
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.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Chapter 6: Process Synchronization
Process Synchronization
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Chapter 5: Process Synchronization
Race Conditions Critical Sections Dekker’s Algorithm
Critical Section and Critical Resources
Critical Section and Critical Resources
Topic 6 (Textbook - Chapter 5) Process Synchronization
Synchronization Hank Levy 1.
Lecture 2 Part 2 Process Synchronization
Synchronization Hank Levy 1.
CSE 542: Operating Systems
CSE 542: Operating Systems
Synchronization CSE 2431: Introduction to Operating Systems
Presentation transcript:

Semaphores

Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today

Race conditions Definition: timing dependent error involving shared state –Whether it happens depends on how threads scheduled Hard to detect: –All possible schedules have to be safe Number of possible schedule permutations is huge Some bad schedules? Some that will work sometimes? –they are intermittent Timing dependent = small changes can hide bug

The Fundamental Issue: Atomicity Our atomic operation is not done atomically by machine –Atomic Unit: instruction sequence guaranteed to execute indivisibly –Also called “critical section” (CS)  When 2 processes want to execute their Critical Section, –One process finishes its CS before other is allowed to enter

Revisiting Race Conditions Process b: while(i > -10) i = i - 1; print “B won!”; Process a: while(i < 10) i = i +1; print “A won!”; – Who wins? – Will someone definitely win?

Critical Section Problem Problem: Design a protocol for processes to cooperate, such that only one process is in its critical section –How to make multiple instructions seem like one? Processes progress with non-zero speed, no assumption on clock speed Used extensively in operating systems: Queues, shared variables, interrupt handlers, etc. Process 1 Process 2 CS 1 Time  CS 2

Solution Structure Shared vars: Initialization: Process:... Entry Section Critical Section Exit Section Added to solve the CS problem

Solution Requirements Mutual Exclusion –Only one process can be in the critical section at any time Progress –Decision on who enters CS cannot be indefinitely postponed No deadlock Bounded Waiting –Bound on #times others can enter CS, while I am waiting No livelock Also efficient (no extra resources), fair, simple, …

Semaphores Non-negative integer with atomic increment and decrement Integer ‘S’ that (besides init) can only be modified by: –P(S) or S.wait(): decrement or block if already 0 –V(S) or S.signal(): increment and wake up process if any These operations are atomic semaphore S; P(S) { while(S ≤ 0) ; S--; } V(S) { S++; }

Semaphore Types Counting Semaphores: –Any integer –Used for synchronization Binary Semaphores –Value 0 or 1 –Used for mutual exclusion (mutex) Shared: semaphore S Init: S = 1; Process i P(S); Critical Section V(S);

Implementing Semaphores Busy waiting (spinlocks) Consumes CPU resources No context switch overhead Alternative: Blocking Should spin or block? –Less time  spin –More time  block –A theory result: Spin for as long as block cost If lock not available, then block Shown factor of 2-optimal! typedef struct semaphore { int value: ProcessList L; } Semaphore; void P(Semaphore *S) { S->value = S->value - 1; if (S.value < 0) { add this process to S.L; block(); } void V(Semaphore *S) { S->value = S->value + 1; if (S->value <= 0) { remove process P from S.L; wakeup P }

Implementing Semaphores Per-semaphore list of processes –Implemented using PCB link field –Queuing Strategy: FIFO works fine Will LIFO work?

Mutexes and Synchronization semaphore S; P(S) { while(S ≤ 0) ; S--; } V(S) { S++; } Process i P(S); Code XYZ V(S); Process j P(S); Code ABC V(S); Init: S = 0;Init: S = 1;

Mutexes and Synchronization semaphore S; P(S) { while(S ≤ 0) ; S--; } V(S) { S++; } Process i P(S); Code XYZ Process j Code ABC V(S); Init: S = 0;Init: S = 1;

Sleeping Barber Problem

One Solution Barber P(customer); /* cut hair */ V(barber); Shared: semaphore customer, barber; int waiting; Init: customer = 0; barber = 1; waiting = 0; Customer /* get haircut */ V(customer); P(barber);

One Solution Barber do{ P(customer); P(mutex); waiting--; /* cut hair */ V(mutex); V(barber); } while (true); Shared: semaphore customer, barber; int waiting; Init: customer = 0; barber = 1; waiting = 0; Customer P(mutex); if(waiting < n) { /* get haircut */ waiting++; V(customer); V(mutex); P(barber); } else { /* leave */ V(mutex); }