1 / 33

Mutexes: Mutually Exclusive

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(); }.

koren
Télécharger la présentation

Mutexes: Mutually Exclusive

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. 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(); }

  4. 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

  5. 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!

  6. 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

  7. 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

  8. 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!

  9. 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 initiallyowned 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."); }

  10. The Worker Method Started as Threads private static void Worker(object num){ // Each worker thread begins by requesting thesemaphore. 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 } }

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

  12. Example: Let’s Play (Table) Tennis B2 B1 • 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

  13. 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.

  14. 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.

  15. output 0 2 4 6 8 1 3 5 10 12 14 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 } }

  16. 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(); } }

  17. output 0 1 2 3 4 5 6 7 8 9 10 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 } } }

  18. 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

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

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

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

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

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

  24. 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

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

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

  27. Events and Event Handling Design (contd.) Event client classes Subscription lists Event service class 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

  28. 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

  29. Delegate MyDelegate using System; delegate double MyDelegate(inti); // 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(inti) { double p = i + System.Math.PI; System.Console.WriteLine("Handler Pi: {0}", p); return p; } public static double EValue(inti) { double e = i + System.Math.E; System.Console.WriteLine("Handler E called: {0}", e); return e; } } PiValue d1 (10) MyDelegate EValue d2 (20)

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

  31. 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

  32. 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 } }

  33. 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

More Related