Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements


Operating Systems Part III: Process Management (Process Synchronization)
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Synchronization and Deadlocks
 Read about Therac-25 at  [  [
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
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.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
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.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Concurrency, Mutual Exclusion and Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
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.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Deadlock Conditions for Deadlock Deadlock Prevention Deadlock Detection Deadlock Recovery Dining Philosophers Semaphores.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Chapter 5: Process Synchronization
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Concurrency: Mutual Exclusion and Synchronization
Midterm review: closed book multiple choice chapters 1 to 9
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Conditions for Deadlock
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
Presentation transcript:

Lecture 6: Monitors & Semaphores

Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for threads outside monitor to access monitor data Resource allocation using Monitors Thread must call monitor entry routine Mutual exclusion is rigidly enforced at monitor boundary A thread that tries to enter monitor when it is in use must wait Monitor Operating Systems - Deitel & Associates

lock( ) vs. monitor( ) Both lock( ) and monitor( ) are methods of enforcing mutual exclusion in modern programming languages such as C# and Java. In the.NET environment, lock( ) is preferred since it manages the thread queue. Like the lock keyword, monitors prevent blocks of code from simultaneous execution by multiple threads. The Enter method allows one and only one thread to proceed; all other threads are blocked until the executing thread calls Exit. This is just like using the lock keyword. In fact, the lock keyword is implemented with the Monitor class. Using the lock is generally preferred over using the Monitor class directly, both because lock is more concise, and because lock ensures that the underlying monitor is released, even if the protected code throws an exception. This is accomplished with the finally keyword, which executes its associated code block regardless of whether an exception is thrown.

using System; using System.Threading; namespace monitor_demo_02 { public class Test { static int count = 0; static void Main() { ThreadStart job = new ThreadStart(ThreadJob); Thread thread = new Thread(job); thread.Start(); for (int i = 0; i < 5; i++) { int tmp = count; Console.WriteLine("Read count={0}", tmp); Thread.Sleep(50); tmp++; Console.WriteLine("Incremented tmp to {0}", tmp); Thread.Sleep(20); count = tmp; Console.WriteLine("Written count={0}", tmp); Thread.Sleep(30); } thread.Join(); Console.WriteLine("Final count: {0}", count); Console.ReadKey(); } Multi-Threaded Demo no Monitor

static void ThreadJob() { for (int i = 0; i < 5; i++) { int tmp = count; Console.WriteLine("\t\t\t\tRead count={0}", tmp); Thread.Sleep(20); tmp++; Console.WriteLine("\t\t\t\tIncremented tmp to {0}", tmp); Thread.Sleep(10); count = tmp; Console.WriteLine("\t\t\t\tWritten count={0}", tmp); Thread.Sleep(40); } Multi-Threaded Demo - concluded

Read count=0 Incremented tmp to 1 Written count=1 Incremented tmp to 1 Written count=1 Read count=1 Incremented tmp to 2 Read count=1 Written count=2 Read count=2 Incremented tmp to 2 Incremented tmp to 3 Written count=2 Written count=3 Read count=3 Incremented tmp to 4 Written count=4 Incremented tmp to 4 Written count=4 Read count=4 Incremented tmp to 5 Written count=5 Incremented tmp to 5 Written count=5 Read count=5 Incremented tmp to 6 Written count=6 Final count: 6 Multi-Threaded Sample Output

using System; using System.Threading; namespace monitor_demo_01 { public class Test { static int count = 0; static readonly object countLock = new object(); static void Main() { ThreadStart job = new ThreadStart(ThreadJob); Thread thread = new Thread(job); thread.Start(); for (int i = 0; i < 5; i++) { Monitor.Enter(countLock); int tmp = count; Console.WriteLine("Read count={0}", tmp); Thread.Sleep(50); tmp++; Console.WriteLine("Incremented tmp to {0}", tmp); Thread.Sleep(20); count = tmp; Console.WriteLine("Written count={0}", tmp); Monitor.Exit(countLock); Thread.Sleep(30); } thread.Join(); Console.WriteLine("Final count: {0}", count); Console.ReadKey(); } Monitor Demo

static void ThreadJob() { for (int i = 0; i < 5; i++) { Monitor.Enter(countLock); int tmp = count; Console.WriteLine("\t\t\t\tRead count={0}", tmp); Thread.Sleep(20); tmp++; Console.WriteLine("\t\t\t\tIncremented tmp to {0}", tmp); Thread.Sleep(10); count = tmp; Console.WriteLine("\t\t\t\tWritten count={0}", tmp); Monitor.Exit(countLock); Thread.Sleep(40); } Monitor Demo - concluded

Read count=0 Incremented tmp to 1 Written count=1 Read count=1 Incremented tmp to 2 Written count=2 Read count=2 Incremented tmp to 3 Written count=3 Read count=3 Incremented tmp to 4 Written count=4 Read count=4 Incremented tmp to 5 Written count=5 Read count=5 Incremented tmp to 6 Written count=6 Read count=6 Incremented tmp to 7 Written count=7 Read count=7 Incremented tmp to 8 Written count=8 Read count=8 Incremented tmp to 9 Written count=9 Read count=9 Incremented tmp to 10 Written count=10 Final count: 10 Monitor Demo Sample Output

Semaphores Software construct that can be used to enforce mutual exclusion Contains a protected variable Can be accessed only via wait and signal commands Also called P and V operations, respectively Semaphores

Binary semaphore: allow only one thread in its critical section at any time... Wait operation If no threads are waiting, allow thread into its critical section Decrement protected variable (to 0 in this case) Otherwise place in waiting queue Signal operation Indicate that thread is outside its critical section Increment protected variable (from 0 to 1) A waiting thread (if there is one) may now enter Binary Semaphore

Counting semaphores Initialized with values greater than one Can be used to control access to a pool of identical resources Decrement the semaphore’s counter when taking resource from pool Increment the semaphore’s counter when returning it to pool If no resources are available, thread is blocked until a resource becomes available Counting Semaphores

Semaphores A semaphore is a protected variable whose value can be accessed and altered only by the operations P( ) and V( ) and an initialization operation SemaphoreInit( ). Binary semaphores can have values true or false while counting semaphores can have non- negative integer values. function P(S:semaphore) if S>0 then S:=S-1; else wait_for(S); end if; end P; function V(S:semaphore) if waiting_for(S) then release_for(S); else S:=S+1; end if; end V; wait_for(S) adds the process calling P(S) to a queue. waiting_for(S) is a boolean function that checks to see if the queue is empty. release_for(S) releases the process waiting at the front of the queue.

Using Semaphores: Mutual Exclusion program example_one is active : semaphore; procedure P1 is begin while true do begin stuff; P(active); critical_region; V(active); end; end P1; procedure P2 is begin while true do begin stuff; P(active); critical_region; V(active); end; end P2; begin example_one semaphore_init(active,1); parbegin P1; P2; parend; end example_one; The semaphore active is set to one (1) and acts to control the process’s access to its critical section.

Using Semaphores: Producer-Consumer Interprocess communication occurs when one process passes data to another process. This is more complex than a procedure call with parameter passing since the two processes do not necessarily share the same address space. The processes must be synchronized which means that the data transfer must occur in the proper time sequence. procedure producer is nextvalue : integer; begin while true do begin calculate(nextvalue); P(access_numbuffer); numbuffer:=nextvalue; V(access_numbuffer); V(numbuffer_loaded); end; end producer; procedure consumer is nextvalue : integer; begin while true do begin P(numbuffer_loaded); P(access_numbuffer); nextvalue:=numbuffer; V(access_numbuffer); make_use_of(nextvalue); end; end consumer;

Mutual Exclusion with Test-and-Set When the computer hardware can be used to support the enforcement of mutual exclusion, the complexity of the resulting software is greatly reduced. The indivisible instruction testandset(a,b) reads the value of a boolean b, copies it into a and then sets b to true all within the span of a single uninterruptible instruction. procedure P1 no_P1 : boolean; begin while true do begin no_P1:=true; while no_P1 do testandset(no_P1,go); critical_region; go:=false; other_stuff; end; end P1; procedure P2 no_P2 : boolean; begin while true do begin no_P2:=true; while no_P2 do testandset(no_P2,go); critical_region; go:=false; other_stuff; end; end P2;

Summary Monitors hold data to be protected from concurrent asynchrononous access Monitors can be used to manage shared resources of any type In C# lock( ) is preferred when enforcing mutual exclusion of shared data Semaphores require special hardware implementation (binary and counting) Test-and-Set is an uninteruptable instruction