445 by jizhen1947


									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 {                           Require to create
                                        an instance/object
        myMutex.ReleaseMutex();           before using it.
                                                             Y. Chen
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.

                                                             Y. Chen
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 {

                                                         Y. Chen
         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):
                                        Indivisible operations
         interrupt                                  supported by
          wait(s):   if s > 0 then s := s - 1 else hardware
                         wait until s > 0, then s := s - 1;
          signal(s): s := s + 1;
                                     Indivisible operations   Y. Chen
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!
                                                               Y. Chen
Case Study      Bit Test and Set Instructions in MC68000

             Three indivisible instructions that test a
             flag and change it in the same instruction:
                               Mtarget                    We need this
                                                          instruction to turn
                                             L            on the lock-bit in the
              Rindex                                      object to be locked,
                                                          using an indivisible

             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

                                                                           Y. Chen
95          Named Semaphores in Windows OS

        The Windows operating system allows semaphores to have
        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
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!

                                                             Y. Chen
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.");
                                                                                          Y. Chen
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(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
99                 Output

 Blocked because
   semaphore = 0

 semaphore = 3
 semaphore = 0
 semaphore = 0
 semaphore = 0
 semaphore = 2

 semaphore = 3

                            Y. Chen
100   Example: Let’s Play (Table) Tennis

      B1              B2      • Table Tennis: The
                                players must play
                                the ball
                              • Tennis: The two
                                players on one site
                                can complete to
                                play the ball;
                              • What mechanisms
                                can be used to
                                program the two
                      A2        cases?

                                                  Y. Chen
101          Coordination Events

         Monitors, Reader/Writer Locks, and Mutexes are used to
          guard shared resources from being accessed
            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
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
            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
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                           output
    }                                                                     0
    static void ThreadFuncEven() {                                        2
        for (int i = 0; i < 100; i += 2)                                  4
           Console.WriteLine(i); // Output the next even number           6
    }                                                                     8
    static void ThreadFuncOdd() {                                         1
        for (int i = 1; i < 100; i += 2)                                  3
           Console.WriteLine(i); // Output the next odd number            5
    }                                                                     10
 }                                                                        12
                                                                               Y. Chen
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
             thread1.Join();       // Wait for the child threads to end
             event1.Close();       // Close the events

                                                                             Y. Chen
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
      }                                                                      output
      static void ThreadFuncOdd()                                            1
      {                                                                      2
         for (int i = 1; i < 101; i += 2)                                    3
         {                                                                   4
            event1.WaitOne();               // Wait for the other thread     5
            Console.WriteLine(i);           // Output the next odd number    6
            event2.Set();                   // Release the other thread      7
         }                                                                   8
      }                                                                      9
 }                                                                           10
                                                                                  Y. Chen
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
        event1.reset needs to be called before each

         event1.WaitOne call.
        It triggers all threads waiting for the event

                                                         Y. Chen
Event-Driven Architecture

         Events and Delegates
       Text Section 2.6, pp. 99-109
                                           In Office    Outside office
Routine of a Medical Professor

                                                       Consult students
in Model-Driven Approach

                                 Write proposal

                                                       See ICU patients

                                 Teaching Prep

                                                        Teach a course

                                 See out-patients

                                                       See all in-patients

                                  Read reports

                                                       See ICU patients
                                                                             Y. Chen
                                                    In Office                               Outside office
Routine of a Medical Professor
                                                                                             Student questions

                                 Write proposal                                              Student questions
in Event-Driven Approach

                                                                                             Student questions
                                                                                             Student questions
                                 Teaching Prep
                                                                                             Student questions
                                 Answer student

                                                                                            Teach a course

                                 See out-patients           Alert                                     ICU patient

                                                                                    Interrupt /
                                  Read reports              Board
                                                                                                      ICU patient

                                                                                                      ICU patient

                                                                                       See all in-patients
                                                                                                                    Y. Chen
Model-Driven Programming

 Main                 Methods/Services




                                         Y. Chen
                                                                  Parallel Activities
                           Control the
                            motors         Event
                                                                                  Sonar sensor
Event-Drive Programming

                            Receiving                                             Temperature
                           information                                              sensor
                            from base
                              station                                            Compass sensor

                          Read Sensors


                              Image        Alert                                        Touch Sensor 1

                                                                   Interrupt /
                            processing     Board
                             Sending                                                    Touch Sensor 2
                          information to                                                  Fire Sensor
                           base station

                                                                                                    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
113    Case Study

      sensor right                                     Motor 1

         Touch                                         Motor 2

                     Event Handling
       sensor left
                                                      Arm control
      Sonar sensor                                      servo

      Gyro sensor

                                                                 Y. Chen
114          Events and Event Handling Design

              Class A (event service)   Class B (event client)

                    Events taking         Event subscribing

                      Delegates            Event handling
                                        Class C (event client)

                                          Event subscribing

                                            Event handling
      Flexibility of deciding
        when to call back

                                                                 Y. Chen
   115                  Events and Event Handling Design (contd.)
                                                            Event client classes
                Event service class    Subscription lists
                                                            Event subscribing
Sensor inputs

                     Events taking                            Event handling

                                                            Event subscribing
                                      functionName            Event handling

                                                            Event subscribing

                                                              Event handling

                Actuator … Actuator                         Event subscribing

                                                              Event handling
                                                                               Y. Chen
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

                                                                        Y. Chen
117           Delegate    using System;
                          delegate double MyDelegate(int i); // Declare a delegate
                          class Program {
      MyDelegate             public static void Main() {
                               // call the delegates as instance methods
 d1      PiValue   (10)        MyDelegate d1 = new MyDelegate(PiValue);
                               d1(10); // Call PiValue method using delegate
      MyDelegate               MyDelegate d2 = new MyDelegate(EValue);
                               d2(20); // Call EValue method using same delegate
 d2      EValue    (20)      }
                             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;
                                                                              Y. Chen
118       Combining Delegate and Event

          sensor   add    MyEvent: EventInterface

      MyDelegate                                     event
          sensor         EventEmitter()

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

                                                                                   Y. Chen
120            Delegate in Event-Driven Programming

      public class MainClass {
        static private void TouchSensor() {       // Event handler touch sensor
        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
                                                                              Y. Chen
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
                                                                       Y. Chen

To top