Slides:



Advertisements
Similar presentations
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.
Advertisements

Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
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
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.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 8b Monitors.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
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.
Chapter 2.3 : Interprocess Communication
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Atomic Operations David Monismith cs550 Operating Systems.
111 © 2002, Cisco Systems, Inc. All rights reserved.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Synchronization with shared memory AMANO, Hideharu Textbook pp
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Operating systems Deadlocks.
Background on the need for Synchronization
Chapter 5: Process Synchronization
Inter-Process Communication and Synchronization
Programming with Shared Memory
Threading And Parallel Programming Constructs
Operating systems Deadlocks.
Lecture 2 Part 2 Process Synchronization
Critical section problem
Global Environment Model
Concurrency: Mutual Exclusion and Process Synchronization
Thread Synchronization including Mutual Exclusion
Kernel Synchronization II
Chapter 6: Synchronization Tools
Programming with Shared Memory - 2 Issues with sharing data
Process Synchronization
CSE 451 Section 1/27/2000.
“The Little Book on Semaphores” Allen B. Downey
Process Synchronization
CSE 542: Operating Systems
Presentation transcript:

Mutexes: Mutually Exclusive Another mechanism to prevent simultaneous access to shared resources. Mutex myMutex = new Mutex(); // lib class … myMutex.WaitOne(); // build-in method Try { // lock and access object } finally { myMutex.ReleaseMutex(); Require to create an instance/object before using it.

Why Another Mechanism? Mutexes have the power to synchronize both threads and processes belonging to different applications in OS; Monitors and locks do not! If a thread acquires a mutex and terminates without freeing it, the system can detect the orphan situation and automatically free it; Mutexes are orders of magnitude slower than the inner-application synchronization mechanisms. Do not use them unless you need to synchronizes processes among different applications.

Reaching cross-application synchronization // In Applications A and B, create same-named mutexes Mutex myMutex = new Mutex(“named_shared_mutexes”); … myMutex.WaitOne(); // build-in method Try { // objects here will be mutually exclusive } finally { myMutex.ReleaseMutex();

Semaphore: Managing Multiple Identical Resources A Semaphore is a flag to prevent more processes (P) than permitted from accessing a pool of resources (R) at the same time: P  R A semaphore is usually implemented by a non-negative integer s ≥ 0, which can be modified by certain operations only, such as Unix system calls wait(s) and signal(s): wait(s): if s > 0 then s := s - 1 else wait until s > 0, then s := s - 1; signal(s): s := s + 1; Indivisible operations supported by hardware interrupt Indivisible operations

Object Lock vs. Indivisible Operations Object lock does not prevent the thread that locks the object from being interrupted; The locked object cannot be accessed while locked; What happens, if a thread tests the lock and it is free, then it is interrupted? Indivisible operations do not lock the object (bit); If multiple threads/processes exist, there is a possibility that another thread can access the object (bit) at the same time; Hardware support is necessary to test and lock in an indivisible instruction at the instruction level!

Bit Test and Set Instructions in MC68000 Three indivisible instructions that test a flag and change it in the same instruction: Case Study We need this instruction to turn on the lock-bit in the object to be locked, using an indivisible instruction Mtarget L Rindex BSET Rindex, Mtarget if L = 0, set L = 1 BCLR Rindex, Mtarget if L = 1, set L = 0 BCHG Rindex, Mtarget if L = b, set L = b

Named Semaphores in Windows OS The Windows operating system allows semaphores to have names. A named semaphore is system wide. That is, once the named semaphore is created, it is visible to all threads in all processes. Named semaphore can be used to synchronize the activities among OS processes as well as among threads. Caution: Because named semaphores are system wide, another process that uses the same name can access your semaphore unexpectedly. Malicious code executing on the same computer could use this as the basis of a denial-of-service attack

C# .Net Semaphore A Semaphore class is defined to control access to a pool of resources. Threads increment the semaphore by calling the WaitOne method, and decrement the semaphore by calling the Release method. When the count is zero, subsequent requests are blocked until other threads release the semaphore. When all threads have released the semaphore, the count is at the max value specified when the semaphore was created; A release call when the count is at its max value will throw an exception!

Example: A semaphore that simulates a resource pool using System; using System.Threading; public class SemaphoreExample { private static Semaphore _pool; private static int padding = 0; public static void Main() { _pool = new Semaphore(0, 3); // create a semaphore between 0 & 3 // The initial count is zero, so that the entire semaphore // count is initially owned by the main program thread for (int i = 1; i <= 5; i++) { // Create 5 numbered threads. Thread t = new Thread(new ParameterizedThreadStart(Worker)); t.Start(i); // i will be passed to the constructor of Work() } Thread.Sleep(500); // Wait for 0.5 sec, to allow all threads to start Console.WriteLine("Main thread calls Release(3)."); _pool.Release(3); // set semaphore to 3 (max value) Console.WriteLine("Main thread exits.");

The Worker Method Started as Threads private static void Worker(object num) { // Each worker thread begins by requesting the semaphore. Console.WriteLine("Thread {0} begins " + "and waits for the semaphore.", num); _pool.WaitOne(); // Requesting a resource padding = padding + 100; // Adding a padding interval Console.WriteLine("Thread {0} enters the semaphore.", num); Thread.Sleep(1000 + padding); // sleep about 1 second Console.WriteLine("Thread {0} releases the semaphore.", num); Console.WriteLine("Thread {0} previous semaphore count: {1}", num, _pool.Release()); // Release one resource }

Output Blocked because semaphore = 0 semaphore = 3 2 1 semaphore = 0

Example: Let’s Play (Table) Tennis Table Tennis: The players must play the ball alternatively; Tennis: The two players on one site can complete to play the ball; What mechanisms can be used to program the two cases? A2 A1

Coordination Events Monitors, Reader/Writer Locks, and Mutexes are used to guard shared resources from being accessed simultaneously: The threads compete for resources – no matter which thread wins; Order could be defined in some cases: if no item to consume, the consumer has to wait for the producer. Coordination events are used to define the order of executions among threads – also called thread triggers. The focus is not on the resources to share Example: Producer wants to fill the buffer before allowing the consumer to read the buffer. This cannot be done using monitors/locks.

Mechanisms Supporting Events Windows support two types of events: Auto-reset events Manual-reset events .Net class library wraps these OS kernel objects into classes AutoResetEvent ManualResetEvent Both classes contain methods: Set: set an event Reset: reset an event WaitOne: blocked until the event becomes set. If called on an event that is set, it runs immediately: no waiting.

Threads Printing Odd / Even Numbers Version without events using System; using System.Threading; class MyApp { static void Main() { // Create two threads Thread thread1 = new Thread(new ThreadStart(ThreadFuncEven)); Thread thread2 = new Thread(new ThreadStart(ThreadFuncOdd)); thread1.Start(); // Start the threads thread2.Start(); } static void ThreadFuncEven() { for (int i = 0; i < 100; i += 2) Console.WriteLine(i); // Output the next even number static void ThreadFuncOdd() { for (int i = 1; i < 100; i += 2) Console.WriteLine(i); // Output the next odd number 2 4 6 8 1 3 5 10 12 14 output

Creating Event Objects class MyApp { static AutoResetEvent event1 = new AutoResetEvent(false); static AutoResetEvent event2 = new AutoResetEvent(false); static void Main() { try { // Create two threads Thread thread1 = new Thread(new ThreadStart(ThreadFuncEven)); Thread thread2 = new Thread(new ThreadStart(ThreadFuncOdd)); thread1.Start(); // Start the threads thread2.Start(); thread1.Join(); // Wait for the child threads to end thread2.Join(); } finally { event1.Close(); // Close the events event2.Close();

Use Events to Define the Order of Printing static void ThreadFuncEven() { for (int i = 0; i < 100; i += 2) Console.WriteLine(i); // Output the next even number event1.Set(); // Release the other thread event2.WaitOne(); // Wait for the other thread } static void ThreadFuncOdd() for (int i = 1; i < 101; i += 2) event1.WaitOne(); // Wait for the other thread Console.WriteLine(i); // Output the next odd number event2.Set(); // Release the other thread 1 2 3 4 5 6 7 8 9 10 output

AutoResetEvent versus ManualResetEvent AutoResetEvent Class event1.reset is automatically called before event1.WaitOne method is called; WaitOne will always be blocked and activated by a later event1.set It only triggers one thread at a time If ManualResetEvent Class is used in the program event1.reset needs to be called before each event1.WaitOne call. It triggers all threads waiting for the event

Event-Driven Architecture Events and Delegates Text Section 2.6, pp. 99-109

Routine of a Medical Professor in Model-Driven Approach In Office Outside office Research Consult students Write proposal See ICU patients Routine of a Medical Professor in Model-Driven Approach Teaching Prep Teach a course See out-patients See all in-patients Read reports See ICU patients

Routine of a Medical Professor in Event-Driven Approach In Office Outside office Research Event Board Student questions Student questions Write proposal Notification Student questions Student questions Teaching Prep Answer student questions Student questions Routine of a Medical Professor in Event-Driven Approach Teach a course See out-patients Read reports Alert Board ICU patient Interrupt / Notification ICU patient ICU patient See all in-patients

Model-Driven Programming Main Methods/Services Temperature Exchange rate Breaking News

Event-Drive Programming Main Parallel Activities Control the motors Event Board Sonar sensor Receiving information from base station Temperature sensor Notification Compass sensor Event-Drive Programming Read Sensors Decompression Image processing Sending information to base station Alert Board Touch Sensor 1 Interrupt / Notification Touch Sensor 2 Fire Sensor Compression

Event-Driven Programming Event-driven programming is a programming paradigm which allows interactions between the computer program and the user or the environment; The execution flow of the program is determined by user actions, such as mouse clicks, key presses in GUI programming! sensor outputs (e.g., touch sensor, motion sensor, etc.), and messages from other programs

Case Study Touch sensor right Motor 1 Touch sensor left Motor 2 Orchestration Event Handling Arm control servo Sonar sensor Alarm Gyro sensor

Events and Event Handling Design Class A (event service) Class B (event client) Events taking Event subscribing Event subscribing Event handling Class C (event client) Event handling Delegates (Signatures) . . . Callbacks Flexibility of deciding when to call back

Events and Event Handling Design (contd.) Event client classes Event service class Subscription lists Event subscribing Event handling Events taking Sensor inputs Event subscribing myDelegate Event handling functionName Orchestration Event subscribing Callbacks Event handling … Actuator Actuator Event subscribing Event handling

C# Delegate for Events and Event Handling Reading: Text section 2.6.2 A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature (prototype). A delegate can be used like a class to create an instance, which encapsulates a static or an instance method. A delegate is similar to a function pointer in C++. A delegate allows a method name to be passed as a parameter, and thus allow the same method call to be associated with different methods. A delegates can be used to define callback methods by passing the name of the event handler to the delegate reference

Delegate d1 PiValue (10) d2 EValue (20) using System; delegate double MyDelegate(int i); // Declare a delegate class Program { public static void Main() { // call the delegates as instance methods MyDelegate d1 = new MyDelegate(PiValue); d1(10); // Call PiValue method using delegate MyDelegate d2 = new MyDelegate(EValue); d2(20); // Call EValue method using same delegate } public static double PiValue(int i) { double p = i + System.Math.PI; System.Console.WriteLine("Handler Pi: {0}", p); return p; public static double EValue(int i) { double e = i + System.Math.E; System.Console.WriteLine("Handler E called: {0}", e); return e; MyDelegate d1 PiValue (10) MyDelegate d2 EValue (20)

Combining Delegate and Event MyDelegate EventClass Touch sensor Touch sensor add MyEvent: EventInterface Motion event Touched event MyDelegate MyDelegate add Motion sensor Motion sensor EventEmitter()

Combining Delegate and Event (code) using System; public delegate void MyDelegate(); // delegate declaration public interface EventInterface { event MyDelegate MyEvent; // Define an event void EventEmitter(); // to be implemented in MyClass } public class EventClass : EventInterface { // implement the interface public event MyDelegate MyEvent; // Define an event public void EventEmitter() { if (MyEvent != null) MyEvent(); // emit an event // continue next page

Delegate in Event-Driven Programming public class MainClass { static private void TouchSensor() { // Event handler touch sensor Console.WriteLine("Touched"); } static private void MotionSensor() { // Event handler motion sensor Console.WriteLine("Motion Detected"); static public void Main() { EventInterface i = new EventClass(); i.MyEvent += new MyDelegate(TouchSensor); // Add an event method i.EventEmitter(); // Emit an event i.MyEvent -= new MyDelegate(TouchSensor); // Remove the method i.MyEvent += new MyDelegate(MotionSensor); // Add an event method i.EventEmitter(); // Emit an event

Summary General Issues in Distributed Computing Resource sharing; Deadlock and deadlock handling Synchronization Creating child process in Unix Multithreading in Java Multithreading and different synchronization mechanisms in C# and .Net Monitors / Lock / Conditional Monitors Reader/Writer Locks Mutex Semaphores Coordination events: define orders of thread execution Delegate and Event-Driven Programming