Example of use. Shared var mutex: semaphore = 1; Process i. begin.. P(mutex); execute CS; V(mutex);.. End;

Similar documents
Concept of a process

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

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

Process Synchronization. studykorner.org

Threading and Synchronization. Fahd Albinali

Operating Systems ECE344

Process Management And Synchronization

Outline for Today. 5 Dining Philosophers. Template for Philosopher. Naive Solution. Objective: Administrative details:

5 Dining Philosophers. Template for Philosopher. Naive Solution. while (food available) /*pick up forks*/ eat; /*put down forks*/ think awhile;

Critical Section Problem: Example

Critical Section Problem: Example

CS3502 OPERATING SYSTEMS

Opera&ng Systems ECE344

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

Chapter 6: Process Synchronization

Two Types of Semaphores

1 Process Coordination

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

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

Process Synchronization

CSC501 Operating Systems Principles. Process Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Outline for Today. Readers/Writers Problem

CHAPTER 6: PROCESS SYNCHRONIZATION

Semaphore Use In Synchronization

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

Concurrency: Deadlock and Starvation. Chapter 6

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

CS510 Operating System Foundations. Jonathan Walpole

Enforcing Mutual Exclusion Using Monitors

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

CONCURRENT/DISTRIBUTED PROGRAMMING ILLUSTRATED USING THE DINING PHILOSOPHERS PROBLEM *

Outline. Monitors. Barrier synchronization The sleeping barber problem Readers and Writers One-way tunnel. o Monitors in Java

Concurrency. Chapter 5

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

Process Synchronization and Cooperation

CSE 153 Design of Operating Systems

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Concurrency pros and cons. Concurrent Programming Problems. Linked list example. Linked list example. Mutual Exclusion. Concurrency is good for users

CSE 120 Principles of Operating Systems Spring 2016

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

CS 318 Principles of Operating Systems

CSE 120 Principles of Operating Systems Spring 2016

CSCE 313: Introduction to Computer Systems

Process Synchronization

Module 6: Process Synchronization

Process Synchronization

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

CS 318 Principles of Operating Systems

Process Synchronization

Lesson 6: Process Synchronization

CS370 Operating Systems

Synchronization Principles

Multitasking / Multithreading system Supports multiple tasks

Semaphores and Monitors: High-level Synchronization Constructs

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

Interprocess Communication By: Kaushik Vaghani

Silberschatz and Galvin Chapter 6

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

Synchronization. Prof. Bracy and Van Renesse CS 4410 Cornell University. based on slides designed by Prof. Sirer

Chapter 7: Process Synchronization. Background. Illustration

The S-Expression Design Language (SEDL) James C. Corbett. September 1, Introduction. 2 Origins of SEDL 2. 3 The Language SEDL 2.

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

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

Synchronization Classic Problems

OS Process Synchronization!

Chapter 6: Process Synchronization

Lecture 3: Intro to Concurrent Processing using Semaphores

CS533 Concepts of Operating Systems. Jonathan Walpole

Process Synchronization

Chapter 7: Process Synchronization. Background

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Process Coordination

Chapter 8. Basic Synchronization Principles

Basic Synchronization Principles

Prof. Hui Jiang Dept of Computer Science and Engineering York University

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

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Lecture 6 (cont.): Semaphores and Monitors

ArdOS The Arduino Operating System Reference Guide Contents

Process Synchronization(2)

Synchronization Principles II

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

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

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

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

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Introduction to Operating Systems

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Operating Systems (2INC0) 2017/18

Chapters 5 and 6 Concurrency

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CS370 Operating Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Process Synchronization

Transcription:

Example of use Shared var mutex: semaphore = 1; Process i P(mutex); execute CS; V(mutex); End;

Other synchronization problems Semaphore can be used in other synchronization problems besides Mutual Exclusion The Producer-Consumer problem a finite buffer pool is used to exchange messages between producer and consumer processes The Readers-Writers Problem reader and writer processes accessing the same file The Dining Philosophers Problem five philosophers competing for a pair of forks

Readers-Writers solution with Procedure reader concurrent reader access P(reader_mutex) if readers = 0 then readers = readers + 1 P(writer_mutex) else readers = readers + 1 V(reader_mutex) <read file> Procedure writer P(writer_mutex) <write file> V(writer_mutex) P(reader_mutex) readers = readers - 1 if readers == 0 then V(writer_mutex) V(reader_mutex)

Readers-Writers with reader s priority Procedure reader Procedure writer P(reader_mutex) if readers = 0 then readers = readers + 1 P(writer_mutex) else readers = readers + 1 V(reader_mutex) P(sr_mutex) P(writer_mutex) <write file> V(writer_mutex) V(sr_mutex) <read file> P(reader_mutex) readers = readers - 1 if readers == 0 then V(writer_mutex) V(reader_mutex)

Producer-Consumer: solution #1 Process producer while count = N ; P(mutex) count = count + 1 write(head_ptr) head_ptr = (head_ptr + 1) mod N V(mutex) Process consumer while count = 0 ; P(mutex) count = count - 1 read(tail_ptr) tail_ptr = (tail_ptr + 1) mod N V(mutex) Semaphore mutex ensures mutual exclusion in accessing the pool, however solution shown is not correct because variable count is not protected (for example two producers could enter when count = N-1)

Producer-Consumer: Correct Solution Process producer P(mutex) if count = N then V(mutex); P(mutex_p); P(mutex) else P(mutex_p) ; count = count + 1 write(head_ptr) head_ptr = (head_ptr + 1) mod N V(mutex_c) V(mutex) Process consumer P(mutex) if count = 0 then V(mutex); P(mutex_c); P(mutex) else P(mutex_c) ; count = count - 1 read(tail_ptr) tail_ptr = (tail_ptr + 1) mod N V(mutex_p) V(mutex) Initialize: count = 0; mutex_c = 0; mutex_p = N ; Assertions count == mutex_c ; count + mutex_p = N

Producer-Consumer: another solution?? Process producer P(mutex) P(mutex_p) ; count = count + 1 write(head_ptr) head_ptr = (head_ptr + 1) mod N V(mutex_c) V(mutex) Process consumer P(mutex) P(mutex_c) ; count = count - 1 read(tail_ptr) tail_ptr = (tail_ptr + 1) mod N V(mutex_p) V(mutex) Initialize: count = 0; mutex_c = 0; mutex_p = N ; Assertions count == mutex_c ; count + mutex_p = N Does not work DEADLOCK!!

Pros: Semaphore: pros and cons no waste of resources due to busy waiting flexible resource management using an initial value > 1 Cons: processes using semaphores must be aware of each other and coordinate respective use of semaphores insertion of P and V calls is tricky and prone to errors correctness of program using semaphores can be very hard to verify do not scale up well - ie impractical for large scale use

Monitors: definition Monitors are abstract data types for encapsulating shared resources A monitor consists of: shared objects and local variables, a set of procedures Basic properties of the monitor procedures are the only operations that can be performed on the resource and on the local variables only one process at a time can be active (ie executing a procedure) within a monitor

Monitors: condition variables Condition variables are variables on which two operations are defined, wait and signal: syntax: <variable>wait and <variable>signal They are used to delay and resume execution of processes calling monitor s procedures Condition variables are visible only from within monitor procedures

Semantic of wait and signal A queue is associated with each condition variable <variable>queue returns true if queue is not empty The <variable>wait call suspends the calling process calling process relinquishes control of the monitor calling process is enqueued on the variable s queue The <variable>signal call causes one waiting process to gain control of the monitor it resume execution from where it left (ie right after the wait statement) the calling process is enqueued on the urgent queue

Producer-Consumer problem circular_pool: monitor pool: array 0N-1 of buffer; count, head, tail: int; nonemtpy, nonfull: condition; Procedure extract(x) if count = 0 then nonemptywait; x:= pool[tail] ; tail := tail + 1 mod N; count := count - 1; nonfullsignal end count := 0 head := 0; tail := 0; end circular_pool Procedure insert(x) if count = N then nonfullwait; pool[head] := x; head := head + 1 mod N; count := count + 1; nonemptysignal end

Readers-Writers: base version procedure Read; <read file> end Read; procedure Write; <write file> end Write;

Readers-Writers with concurrent reader access procedure startread readers = readers+1; end <READ FILE> procedure writer if (readers >0) then writerwait; <WRITE FILE> end procedure endread readers = readers -1; if (readers == 0) then writersignal; end This solution works, but does not guarantee readers priority hint: who is allowed into the monitor when a writer exits?

Readers-Writers solution with procedure startread; if busy then OKtoreadwait; readcount := readcount + 1; OKtoreadsignal; end startread; readers priority procedure endread; readcount := readcount - 1; if readcount = 0 then OKtowritesignal; end endread; procedure startwrite; if busy OR readcount 0 then OKtowritewait; busy := true; end startwrite; procedure endwrite; busy := false; if OKtoreadqueue then OKtoreadsignal else OKtowritesignal; end endwrite;

wait with priority An enhanced version of the wait operation accepts an optional priority argument: syntax: <variable>wait <parameter> the smaller the value of the parameter, the highest the priority When the variable is signaled, the process with highest priority in the queue is activated the base wait implementation used a First-In-First-Out (FIFO) discipline

Example: Smallest job first procedure startprint; if NOT printerisbusy then jobavailablewait; printer-file := buffer; end startprint; <print printer-file> procedure endprint; printerisbusy := false; OKtoprintsignal; end endprint; procedure enqueuejob(file); if printerisbusy then OKtoprintwait sizeof(file); printerisbusy := true; buffer := file; jobavailablesignal end;

Monitors: pros and cons Pros: encapsulation provides automatic serialization flexibility in blocking and unblocking process execution within monitor procedures Cons lack of concurrency if monitor encapsulates shared resources possibility of deadlock with nested monitor calls

Lessons learned Encapsulation of critical section of code is desirable provides automatic mutual exclusion single copy of code, single point of synchronization however would be nice to have some form of controlled concurrency Blocking/unblocking of processes is powerful tool basic ingredient are named queues, enqueue and dequeue operations enqueue and dequeue operations usually subject to condition