Enforcing Mutual Exclusion Using Monitors

Similar documents
Chapter 7: Process Synchronization!

Mutual Exclusion and Synchronization

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

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

Opera&ng Systems ECE344

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

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

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

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Chapter 7: Process Synchronization. Background

Process Synchronization

Lesson 6: Process Synchronization

Operating Systems ECE344

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Introduction to Operating Systems

Process Synchronization

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Synchronization Principles

Interprocess Communication By: Kaushik Vaghani

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

CSE Opera,ng System Principles

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

Process Synchronization

CSE 153 Design of Operating Systems

Semaphores (by Dijkstra)

IV. Process Synchronisation

Module 6: Process Synchronization

Dept. of CSE, York Univ. 1

Process Synchronization

CS4410, Fall 2016 Homework 2 Deadline: 09/28/2016, 11:59AM

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

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

Process Synchronization and Cooperation

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

Module 6: Process Synchronization

Chapter 6: Process Synchronization

Concurrency and Synchronisation

CS370 Operating Systems

Chapter 6 Process Synchronization

Concurrency and Synchronisation

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CSE 120 Principles of Operating Systems Spring 2016

Process Management And Synchronization

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

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

PROCESS SYNCHRONIZATION

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

Lecture 3: Synchronization & Deadlocks

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

PESIT Bangalore South Campus

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Silberschatz and Galvin Chapter 6

High-level Synchronization

Two Types of Semaphores

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Process Coordination

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Process Synchronization

Concurrency: Mutual Exclusion and Synchronization

Chapter 5: Process Synchronization

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

Critical Section Problem: Example

Quiz Answers. CS 537 Lecture 9 Deadlock. What can go wrong? Readers and Writers Monitor Example

CS3502 OPERATING SYSTEMS

1. Motivation (Race Condition)

Process Co-ordination OPERATING SYSTEMS

Operating Systems. Synchronization, part 3 Monitors, classical sync. problems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

CS370 Operating Systems

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

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

Chapters 5 and 6 Concurrency

Classic Problems of Synchronization

Critical Section Problem: Example

Concurrency: Deadlock and Starvation. Chapter 6

CS370 Operating Systems

Lecture 6. Process Synchronization

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(2)

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

CS 318 Principles of Operating Systems

Dealing with Issues for Interprocess Communication

Chapter 6: Process Synchronization

Process Synchronization. studykorner.org

Semaphores and Monitors: High-level Synchronization Constructs

Chapter 5: Process Synchronization

Transcription:

Enforcing Mutual Exclusion Using Monitors

Mutual Exclusion Requirements Mutually exclusive access to critical section 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 # of times that other processes are allowed to enter their critical sections after a process has requested to enter its critical section and before that request is granted. Assume each process executes at a nonzero speed No assumption concerning relative speed of n processes. 2

Enforcing Mutual Exclusion funtion(){ <non- critical section> enter_critical_section(); <critical section> exit_critical_section(); <remainder section> 3

Four different approaches Hardware support Software- defined approaches Support from the OS Support from the programming language 4

Programming Language Sync. Construct Semaphores are error prone Hard to detect timing errors Obscure code (widely separated synchronization pairs) A monitor is a higher level (programming language) synchronization construct Semantics Only 1 process at a time can be active in a monitor A monitor variable can only be accessed within the monitor Signaling between processes is done through condition variables in a monitor 5

Monitor High- level synchronization construct that allows the safe sharing of an abstract data (ADT) type among concurrent processes. monitor monitor- name { shared variable declarations condition variable declarations procedure body P1 ( ) {... procedure body P2 ( ) {... procedure body Pn ( ) {... { initialization code Q1 6

Monitors To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; Condition variable can only be used with the operations wait and signal. The operation x.wait(); means that the process invoking this operation is blocked until another process invokes x.signal(); The x.signal operation resumes exactly one process blocked on x. If no process is blocked, then the signal operation has no effect. It is lost. Q2, 3 7

Schema@c View of a Monitor 8

Monitor With Condi@on Variables 9

Monitor summary Programming language construct that guarantees mutual exclusion A software module that consists of procedures, an initialization routine and local data. Local data are accessible only by the procedures of the monitor. A process can enter a monitor by invoking one of its procedures. Only one process may be executing in the monitor at any time. Other processes that would like to invoke a monitor procedure will be blocked. 10

Mutual Exclusion Mutual exclusion is guaranteed and automatic: only one process allowed in the monitor place the shared data structure in a monitor. Recall that monitor is a programming language construct (supported by some languages) Compiler provides support for mutually exclusive access to monitor procedures Q4 11

Classical Synchroniza@on Problems Bounded- Buffer Problem Readers and Writers Problem Dining- Philosophers Problem 12

Readers/Writers problem Many readers Many writers Any number of readers can be allowed to read simultaneously While a writer is writing, no others can write. No readers should read either. 13

Monitors Readers/Writers problem Monitor ReaderWriter; /* The following can only be accessed by monitor functions. */ int readers = 0; boolean writelock = false; Condition canwrite; Condition canread; 14

Reader(){ Writer(){ while(true){ beginread(); Read(); endread(); while(true){ beginwrite(); Write(); endwrite(); 15

The Reader: Hoare s model void beginread(){ if(writelock queue(canwrite)){ cwait(canread); readers++; csignal(canread); void endread(){ readers- - ; if(readers == 0){ csignal(canwrite); 16

The Writer: Hoare s model void beginwrite(){ if(readers > 0 writelock){ cwait(canwrite); writelock = true; void endwrite(){ writelock = false; if(queue(canread) csignal(canread); else if (queue(canwrite)) csignal(canwrite); 17

Hoare s model Hoare s model requires that a process signals at the end of the procedure and leaves the monitor. If signal() is not the last operation, then the process making the signal must block and instead of being placed on any of the conditions queues or the entry queue, will be placed in the urgent queue. The signal will wake the first process in the queue for that condition. If no process is waiting, the signal is lost. 18

19

Disadvantages of Hoare s model If the process that signaled is not done with the monitor, then unnecessary process switches: One to block the process One to resume it Process scheduling must be very strictly enforced. A process from the corresponding condition queue must be scheduled immediately Scheduler must ensure that no other process enters monitor before activation Reason: condition under which process was activated could change 20

Reader(){ Writer(){ while(true){ beginread(); Read(); endread(); while(true){ beginwrite(); Write(); endwrite(); 21

Reader 1: void beginread(){ if(writelock queue(canwrite)) { cwait(canread); readers++; signal(canread); Writer 1: void endwrite(){ writelock = false; if(queue(canread) else Reader 1 is blocked on canread. Writer 1 signals canread. csignal(canread); if (queue(canwrite)) csignal(canwrite); Reader 1 is un-blocked and is ready, but is not immediately scheduled. Writer 2 is scheduled sets writelock to true and is ready to write - IS WRITING when timed out. Reader 1 gets scheduled to run Will increment readers, signals to any waiting readers and then PROCEED TO READ. Writer 2: void beginwrite(){ if(readers > 0 writelock){ cwait(canwrite); writelock = true; Q5 22

Lampson and Redell modifica@on cnotify() instead of csignal(): Process will notify the first process waiting on condition. Notified process need not be scheduled immediately Notifying process need not quit or block. Code must change, such that any process that has to continue will check the condition again. Q6 23

Reader : Lampson and Redell s model void beginread(){ while(writelock queue(canwrite)){ cwait(canread); readers++; cnotify(canread); void endread(){ readers- - ; if(readers == 0){ cnotify(canwrite); 24

Writer: Lampson and Redell s model void beginwrite(){ while(readers > 0 writelock){ cwait(canwrite); writelock = true; void endwrite(){ writelock = false; if(queue(canread) cnotify(canread); else if (queue(canwrite)) cnotify(canwrite); 25

Modifica@on allows cbroadcast() cbroadcast(c) Wake all processes that are waiting on a condition. Example, if n processes are waiting on space in memory. If j bytes were available and now k more bytes become free. The memory manager does not know which processes waiting can run with k + j bytes. Using cbroadcast(), it can wake all the processes that are waiting and the process that can run with k + j bytes will run. Q7 26

Monitor solu@on Producer Consumer Checkout the JavaMonitor project from your svn repository Study and run the code Java uses wait() / notify(), synchronized methods, and locks to implement monitors 27