1 / 17

Lecture 5

Lecture 5. Concurrency and Process/Thread Synchronization Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm. Concurrent Execution More than one thread exists in system at once Can execute independently or in cooperation Asynchronous execution

edan
Download Presentation

Lecture 5

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. Lecture 5 Concurrency and Process/Thread SynchronizationMutual Exclusion         Dekker's Algorithm        Lamport's Bakery Algorithm

  2. Concurrent Execution • More than one thread exists in system at once • Can execute independently or in cooperation • Asynchronous execution • Threads generally independent • Must occasionally communicate or synchronize • Complex and difficult to manage such interactions Operating Systems - Deitel & Deitel

  3. lock( ) Statement In C#, the lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. int Withdraw(int amount) { // This condition will not occur unless lock statement is removed if (balance < 0) { thrownewException("Negative Balance"); } // Comment out the next line to see the effect of leaving out lock lock (this) { if (balance >= amount) { Console.WriteLine("Balance before Withdrawal : " + balance); Console.WriteLine("Amount to Withdraw : -" + amount); balance = balance - amount; Console.WriteLine("Balance after Withdrawal : " + balance); return amount; } else { return 0; // transaction rejected } } http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71).aspx

  4. Mutual Exclusion Enforcing mutual exclusion is the method for preventing more than one process or thread from accessing a shared memory space at any given time. In multi-processing and multi-threaded applications, this is needed to ensure that asynchronous operations do not produce inconsistent data. Modern programming languages provide mechanisms for enforcing mutual exclusion. For example C# includes the lock( ) method that can be used to prevent two or more threads from entering a designated critical section of code in which shared memory will be accessed/modified. Unfortunately these machine specific methods cannot be used in distributed applications since there is no way to guarantee that some remote system will support them. What is needed, is a software-only means of enforcing mutual exclusion...

  5. Mutual Exclusion: Version 1

  6. Mutual Exclusion: Version 2

  7. Mutual Exclusion: Version 3

  8. Mutual Exclusion: Version 4

  9. Mutual Exclusion: Version 5

  10. Mutual Exclusion: Version 6

  11. @$#%&*!! 42?… number 42? 1329 N-Process Mutual Exclusion When we consider n processes sharing memory rather than just two, the problem of mutual exclusion becomes much more complex. An efficient software-only algorithm for enforcing mutual exclusion among n process was developed by L.Lamport in which each process must “take a ticket” or be placed in a queue to wait for access to shared memory. This method is called Lamport’s Bakery Algorithm and is particularly well suited to distributed processing. Compare Lamport's Bakery Algorithm to Dekker's or Peterson's Algorithm. How are they different. What do we gain by using Lamport's Algorithm? What do we give up?

  12. Using Mutex in C# using System; using System.Threading; classMutex_Demo { privatestaticMutex mut = newMutex(); //create a new Mutex privateconstint numIterations = 2; privateconstint numThreads = 5; staticvoid Main() { for (int i = 0; i < numThreads; i++) { Thread myThread = newThread(newThreadStart(MyThreadProc)); myThread.Name = String.Format("Thread{0}", i + 1); myThread.Start(); } Console.ReadKey(); mut.Close(); } privatestaticvoid MyThreadProc() { for (int i = 0; i < numIterations; i++) UseResource(); } privatestaticvoid UseResource() // protected resource { mut.WaitOne(); // wait until it is safe to enter Console.WriteLine("{0} using resource", Thread.CurrentThread.Name); Thread.Sleep(500); // time in protected resource Console.WriteLine("{0} leaving resource \r\n",Thread.CurrentThread.Name); mut.ReleaseMutex(); // release the mutex } }

  13. Airline Reservation System - Case Study Boeing 737-400 Project Outline Simulate concurrent access to available seating on a particular carrier for a specific flight. Multiple agents (4 to 6) will seat customers with preferences. Program will simulate customer time of response. Program will monitor and report data on wait time, and service rates. Seating Chart

  14. Customer Preferences • Class • - First (rows 1-6) • - Business Coach (rows 7- 26) • Seat Preference • - Aisle • - Middle • - Window • Seat Location • - Near Front • - Over Wing • - Near Rear • Number of Seats • - 1 (Individual seating) • - 2 (assumed adjacent) • - 3 (at least 2 adjacent) • Seating Restrictions • - Exit row OK (true or false)

  15. Server Interaction with each Customer Customer States Preferences Server Tags Candidate Seat(s) available no yes Server Changes Candidate Seat(s) rejects decide Server Releases Seat(s) 0.1 accepts 0.9 Server Reserves Seat(s)

  16. Seat Status Array a shared resource A B C D E F 1 2 3 4 5 26 Possible Seat States available tagged reserved : : A ticket agent thread may tag any available seat. A tagged seat my be release or reserved only by the thread that has tagged it.

  17. Summary of Case Study • Airline Reservation System for One Flight on One Carrier • 4 to 6 ticketing agents running concurrently • A queue of customers for each agent • Threads use mutex and/or semaphores to enforce mutual exclusion • Customers are simulated WRT to Preferrences and Time of Service • Wait Times and Customer Service Times are Monitored, Analyzed and Reported

More Related