y. chen 89 mutexes: mutually exclusive another mechanism to prevent simultaneous access to shared...
TRANSCRIPT
Y. Chen
1 Mutexes: Mutually Exclusive
Another mechanism to prevent simultaneous access to shared resources.
Mutex myMutex = new Mutex(); // lib class…myMutex.WaitOne(); // build-in methodTry {
// lock and access object}finally {
myMutex.ReleaseMutex();}
Require to create an instance/object
before using it.
Y. Chen
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.
Y. Chen
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();
}
Y. Chen
4Semaphore: 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
Indivisible operations
interrupt
Y. Chen
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!
5
Y. Chen
Bit Test and Set Instructions in MC680006
Three indivisible instructions that test a
flag and change it in the same instruction:
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
Mtarget
LRindex
We need this instruction to turn on the lock-bit in the object to be locked, using an indivisible instruction
Cas
e S
tudy
Y. Chen
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
Y. Chen
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!
Y. Chen
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 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.");
}
Y. Chen
10 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
}
}
Y. Chen
11 Output
Blocked because semaphore = 0
semaphore = 321
semaphore = 01
semaphore = 01
semaphore = 01
semaphore = 2
semaphore = 3
Y. Chen
12 Example: Let’s Play (Table) Tennis
A1 A2
B1 B2 • 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?
Y. Chen
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.
Y. Chen
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.
Y. Chen
15 Threads Printing Odd / Even Numbers
using System;using System.Threading;class MyApp {
static void Main() {// Create two threadsThread thread1 = new Thread(new ThreadStart(ThreadFuncEven));Thread thread2 = new Thread(new ThreadStart(ThreadFuncOdd));thread1.Start(); // Start the threadsthread2.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 }}
Version without events
02468135101214
output
Y. Chen
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 threadsthread2.Start();thread1.Join(); // Wait for the child threads to endthread2.Join();
} finally {
event1.Close(); // Close the eventsevent2.Close();
} }
Y. Chen
17 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 } }}
012345678910
output
Y. Chen
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
Event-Driven Architecture
Events and Delegates
Text Section 2.6, pp. 99-109
Y. Chen
Rou
tin
e of
a M
edic
al P
rofe
ssor
in M
odel
-Dri
ven
Ap
pro
ach
Research
Write proposal
Consult students
Teaching Prep
See ICU patients
See out-patients
Teach a course
Read reports
See all in-patients
See ICU patients
Outside officeIn Office
Y. Chen
Rou
tin
e of
a M
edic
al P
rofe
ssor
in E
ven
t-D
rive
n A
pp
roac
hResearch
Write proposal
Student questions
Teaching Prep
Answer student questions
ICU patient
Teach a course
See out-patients
Read reports
See all in-patients
Outside office
Event Board
Alert Board
ICU patient
ICU patient
Student questions
Student questions
Student questions
Student questions
Not
ific
atio
n
Inte
rrup
t /
Not
ific
atio
n
In Office
Y. Chen
Model-Driven Programming
Main
Temperature
Breaking News
Exchange rate
Methods/Services
Y. Chen
Eve
nt-
Dri
ve P
rogr
amm
ing
Control the motors
Receiving information from base
station
Sonar sensor
Read Sensors
Touch Sensor 1
Decompression
Image processing
Sending information to
base station
Compression
Event Board
Alert Board
Touch Sensor 2
Fire Sensor
Temperature sensor
Compass sensor
Not
ific
atio
n
Inte
rrup
t /
Not
ific
atio
n
Parallel ActivitiesMain
Y. Chen
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
Y. Chen
Case Study25
Touch sensor left
Sonar sensor
Gyro sensor
Orchestration
Motor 1
Motor 2
Arm control servo
Alarm
Eve
nt H
andl
ing
Touch sensor right
Y. Chen
Events and Event Handling Design26
Events taking
Delegates(Signatures)
Class A (event service)
Event subscribing
Event handling
Class B (event client)
Callbacks
Event subscribing
Event handling
Class C (event client)
Flexibility of deciding when to call back
. . .
Y. Chen
Events and Event Handling Design (contd.)27
myDelegate
Event service classEvent subscribing
Event handling
Event client classes
Callbacks
Event subscribing
Event handling
Subscription lists
Event subscribing
Event handling
Event subscribing
Event handling
Events taking
Sens
or in
puts
functionName
Orchestration
Actuator Actuator…
Y. Chen
C# Delegate for Events and Event Handling
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
28
Reading: Text section 2.6.2
Y. Chen
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;
}
}
29
MyDelegate
d1 PiValue
(10)
MyDelegate
d2 EValue (20)
Delegate
Y. Chen
Combining Delegate and Event30
Touch sensor
MyDelegate
MyDelegate
EventClass
MyDelegate
MyEvent: EventInterface
EventEmitter()
Touched event
add
add
Touch sensor
Motion event
Y. Chen
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
31
Y. Chen
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
}
}
32
Y. Chen
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