Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO

Similar documents
Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

Process synchronization

Lecture 3: Intro to Concurrent Processing using Semaphores

Threading and Synchronization. Fahd Albinali

Semaphore Use In Synchronization

G52CON: Concepts of Concurrency

Operating Systems ECE344

Concept of a process

Opera&ng Systems ECE344

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Process Management And Synchronization

Topic 4: Synchronization with Semaphores

CS 326 Operating Systems Synchronization. Greg Benson Department of Computer Science University of San Francisco

Semaphores. A semaphore is an object that consists of a. methods (e.g., functions): signal and wait. semaphore. method signal. counter.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

CSC501 Operating Systems Principles. Process Synchronization

Chapter 7: Process Synchronization!

CSCE Operating Systems Deadlock. Qiang Zeng, Ph.D. Fall 2018

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

Synchronization problems with semaphores

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Synchronization: Semaphores

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Deadlocks. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Chapters 5 and 6 Concurrency

CS 4410 Operating Systems. Review 1. Summer 2016 Cornell University

Chapter 6: Process Synchronization

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

CSE Traditional Operating Systems deal with typical system software designed to be:

Chapter 6: Process Synchronization

G52CON: Concepts of Concurrency

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

SFDV3006 Concurrent Programming

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Silberschatz and Galvin Chapter 6

Process Coordination

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Concurrency. Chapter 5

Semaphores. Otto J. Anshus University of {Tromsø, Oslo}

Process Synchronization

Concurrent Computing

Programming Languages

Interprocess Communication and Synchronization

Week 3. Locks & Semaphores

On Multi-Threaded Programming

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo}

2 Threads vs. Processes

Basic Synchronization Principles

Introduction to Operating Systems

IV. Process Synchronisation

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

CS3502 OPERATING SYSTEMS

Dealing with Issues for Interprocess Communication

Concurrency and Synchronisation

Process Synchronization. studykorner.org

Concurrency and Synchronisation

EECS 482 Introduction to Operating Systems

Chapter 7: Process Synchronization. Background. Illustration

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

Concurrency: Deadlock and Starvation. Chapter 6

Process Synchronization

SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores

Chapter 7: Process Synchronization. Background

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

PROCESS SYNCHRONIZATION

Interprocess Communication By: Kaushik Vaghani

Semaphores and Monitors: High-level Synchronization Constructs

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors

Chapter 8. Basic Synchronization Principles

Process Synchronization(2)

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Concurrency and Synchronisation. Leonid Ryzhyk

Process Synchronization

Critical Section Problem: Example

Concurrent Programming

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

Chapter 6 Process Synchronization

Process Synchronization

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Process Synchronization

Dining Philosophers, Semaphores

Carnegie Mellon Synchronization: Advanced

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Process Synchronization Mechanisms

Transcription:

Semaphores A shared integer variable, s, initialized to init, and manipulated only by two operations: pass (proberen): P(s) df = await(s > 0)s = s 1 1 Fairness: V may release a waiting process Weakly fair, Strongly fair, or FIFO Unless otherwise stated we ll assume only weak fairness. 2 release (verhogen): V(s) df = s = s + 1 s is non-negative General semaphore: can take on any nonnegative value. Binary Semaphore: either 0 or 1. Inference Rules 3 Mutual Exclusion 4 (P g > 0) Q g (g 1) {PP(g){Q Coarse-grained int s = 1; Fine-grained sem s = 1; P Q g (g+1) {PV(g){Q process CS[i = 1 to n] { < await(s>0) s--; > critical section; < s++; > noncritical section; process CS[i = 1 to n] { P(s); critical section; V(s); noncritical section;

Barrier Synchronization 5 Two process case: 6 signaling semaphore sem arrive1 = 0; # Shared sem arrive2 = 0; # Shared Used to signal event (i.e., arrival at some part of the code). Usually initialized to 0. Use two semaphores per process pair: one signals arrival at barrier, another to control departure process Worker1 { code to implement task 1; V(arrive1); P(arrive2); process Worker2 { code to implement task 2; V(arrive2); P(arrive1); Can be extended to n-processes by appropriate choice of semaphores. Barrier Synchronization: Coordinator Workers signal arrival with V(done) Wait on P(continue[i]) Coordinator waits on n P(done) Releases all with V(continue[i]) Worker sem done = 0; sem continue[1:n] = ([n] 0); process Worker[i = 1 to n] { code to implement task i; V(done); P(continue[i]); Coordinator process Coordinator { for [i = 1 to n] P(done); for [i = 1 to n] V(continue[i]); 7 Split Binary Semaphore Use semaphores to signal data state rather than process state. split binary semaphore two or more binary semaphores that have the property that at most one is 1 at any time. Initially only one is 1. Invariant: 0 s 0 + s 1 +... + s n 1 In every execution path, a P operation on one semaphore is followed (eventually) by a V on a (possibly different) semaphore. Code between P and V executed in mutual exclusion. 8

Producers & Consumers 9 Semaphores as Counters 10 int buf; sem empty = 1, full = 0; process Producer[i = 1 to n] { P(empty); deposit to buf; V(full); process Consumer[i = 1 to m] { P(full); fetch from buf; V(empty); System with N (identical) resources that are to be shared. Use semaphore to represent number available, P to obtain one, V to release one. Consider producer-consumer with bounded buffer of size N and multiple producers and consumers. int buf[0:n]; int front = 0; # next cell to read int rear = 0; # next cell to write sem empty = N; # Num. empty cells sem full = 0; # Num. full cells sem mutexa = 1; sem mutexf = 1; void Add(int x) { P(empty); P(mutexA); buf[rear] = x; rear = (rear + 1) % N; V(mutexA); V(full); Producer and Consumer int Fetch() { P(full); P(mutexF); int result = buf[front]; front = (front + 1) % N; V(mutexF); V(empty); return result; 11 Overlapping Shared Resources Dining Philosophers process Philosophers[i = 0 to n] { think; acquire forks; eat; release forks; Wait-for cycle two or more process such that every one is waiting for a resource held by another. (e.g., p[0 : n] such that p[i] is waiting for something held by p[(i + 1)%n] for all i.) A necessary condition for deadlock. Eliminate by asymetry. 12

Aside: Necessary Conditions for Deadlock 13 Readers/Writers Problem 14 Serially reusable resources shared under mutual exclusion Several processes share a database, Incremental acquisition Readers several can access concurrently. No pre-emption Writers must have exclusive access. Wait-for cycle Two solution forms: 1) Mutual exclusion use semaphore for lock and count the readers. First reader in acquires lock, last reader out releases it. Writer acquires lock and releases when it s done. 2) Conditional synchronization Passing the Baton Reader-Writer Coarse Grained Solution int nw := 0; # number of writers int nr := 0; # number of readers ## INV: nw == 0 \/ (nw == 1 /\ nr = 0) process Reader[i = 1 to M] { < await(nw == 0) nr++; > read database < nr--; > process Writer[i = 1 to N] { < await(nr == 0 && nw == 0) nw++; > write database < nw--; > 15 Passing the Baton A technique to implement general await statements using (split binary) semaphores: sem e = 1; Control entry to atomic statements. For each condition (guard), B: A semaphore to delay processes that do await(b) A counter counts the number of delayed processes. Global data int nw := 0, nr := 0; # number of writers/readers ## INV: nw == 0 \/ (nw == 1 /\ nr = 0) 16 sem e := 1; # exclusive access sem r := 0; # used to delay readers sem w := 0; # used to delay writers int dr := 0, dw := 0; # count of delayed readers/writers

Signal if (nw == 0 and dr > 0) { dr = dr-1; V(r); # Awaken a reader else if (nr == 0 and nw == 0 and dw > 0) { dw = dw-1; V(w); # Awaken a writer else { V(e); # Release entry lock 17 process Reader[i = 1 to M] { if (nw > 0) { dr++; V(e); P(r); nr = nr + 1; read database nr = nr - 1; process Writer[j = 1 to N] { if (nr > 0 or nw > 0) { dw++; V(e); P(w); nw = nw + 1; write database nw = nw - 1; 18