170 likes | 179 Views
This lecture covers topics such as mutual exclusion, Dekker's algorithm, Lamport's bakery algorithm, and concurrent execution with multiple threads. It also discusses the lock statement in C# and the importance of enforcing mutual exclusion in multi-processing and multi-threaded applications.
E N D
Lecture 5 Concurrency and Process/Thread SynchronizationMutual 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 • Threads generally independent • Must occasionally communicate or synchronize • Complex and difficult to manage such interactions Operating Systems - Deitel & Deitel
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
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...
@$#%&*!! 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?
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 } }
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
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)
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)
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.
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