Day 13 Concurrency.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
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 ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Monitors. Controls Condition variables –Value –Queue –ONLY used in a monitor Signal "promotes" or "de-queues" a waiter Two types –Signal & exit(C.A.R.
Monitors CSCI 444/544 Operating Systems Fall 2008.
1 Chapter 5 Concurrency. 2 Concurrency 3 4 Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int *i) { if (*i == 0) { *i.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Monitor Solutions to Classical Problems. 2 Announcements CS 415 Projects graded. –Mean 80.7, High 90 out of 90 CS 414 Homework due Monday.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
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.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Java Thread and Memory Model
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Homework-6 Questions : 2,10,15,22.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Synchronization Condition Variables and Monitors
CSE 120 Principles of Operating
Synchronization, part 3 Monitors, classical sync. problems
Day 15 Concurrency.
Concurrency: Mutual Exclusion and Synchronization
Monitors Chapter 7.
An object-based synchronization mechanism.
Midterm review: closed book multiple choice chapters 1 to 9
Semaphore Originally called P() and V() wait (S) { while S <= 0
Critical section problem
Monitors Chapter 7.
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitors Chapter 7.
CSE 451: Operating Systems Autumn Lecture 7 Semaphores and Monitors
CSE 451: Operating Systems Autumn Module 8 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Monitors and Inter-Process Communication
Presentation transcript:

Day 13 Concurrency

Monitors - Programming language support

Monitors Programming language construct that guarantees mutual exclusion A software module that consists of procedures, an initialization routine, and local data. Local data is accessible only to 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.

Example: Monitor Student { private float age; public synchronized void setAge(float age){ ..} public synchronized float getAge(){..} } If we make the data fields private, they can be accessed only thru the functions. If we make the functions synchronized, only one process can execute any one function at a time. So, mutual exclusion is guaranteed.

Mutual exclusion Mutual exclusion is guaranteed: only one process allowed in the monitor place the shared data structure in a monitor.

Synchronization using monitors condition variables - contained within the monitor and accessible only within the monitor A queue is associated with each condition variable cwait(c) – suspend execution of process on condition c – Monitor is now available for use by another process. csignal(c) – Resume execution of some process blocked on c. If there are none, then do nothing.

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.

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; Functions: beginRead(), endRead(), beginWrite(), endWrite()

Reader(){ Writer(){ while(true){ beginRead(); Read(); endRead(); } beginWrite(); Write(); endWrite(); }

Monitor procedures for the Reader(Hoare’s model) void beginRead(){ if(writeLock || queue(canWrite)){ cwait(canRead); } readers++; csignal(canRead); void endRead(){ readers--; if(readers == 0){ csignal(canWrite); }

Monitor procedures for the Writers(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); }

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.

Monitors

Disadvantages of Hoare’s model If the process that signaled is not done with the monitor, then there is an un-necessary process switch. Process scheduling must be very strictly enforced.

Reader 1 is blocked on canRead because writeLock is true. Writer 1: void endWrite(){ writeLock = false; if(queue(canRead) csignal(canRead); else if (queue(canWrite)) csignal(canWrite); } Reader 1: void beginRead(){ if(writeLock || queue(canWrite)){ cwait(canRead); } readers++; signal(canRead); Writer 2: void beginWrite(){ if(readers > 0 || writeLock){ cwait(canWrite); } writeLock = true; Reader 1 is blocked on canRead because writeLock is true. Writer 1 signals canRead. 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.

Lampson and Redell suggested a modification cnotify() instead of csignal() - Process will notify the first process waiting on “c”. But, the process waiting need not be scheduled immediately and the notifying process need not quit or block. However, the code must change, such that any process that has to continue will check the condition again.

Monitor procedures for the 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); }

Monitor procedures for the Writers(Hoare’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); }

cbroadcast() cbroadcast(c) – Wake all processes that are waiting on a condition. For 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.

How do we prevent more than one process from being in the monitor at any given time? Every method in the monitor becomes a critical section and any of the methods we have seen earlier can be used to ensure that while a process is in a CS, no other process can be allowed to enter any of the other functions.