Presentation is loading. Please wait.

Presentation is loading. Please wait.

Similar presentations

Presentation on theme: ""— Presentation transcript:

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

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

91 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();

92 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

93 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!

94 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

95 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

96 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!

97 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.");

98 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( 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 }

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

100 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

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

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

103 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

104 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();

105 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

106 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

107 Event-Driven Architecture
Events and Delegates Text Section 2.6, pp

108 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

109 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

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

111 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

112 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

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

114 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

115 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

116 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

117 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)

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

119 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

120 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

121 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

Download ppt ""

Similar presentations

Ads by Google