Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.

Similar presentations

Presentation on theme: "Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm."— Presentation transcript:

1 Lecture 5 Concurrency and Process/Thread Synchronization     Mutual 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) throw new Exception("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

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 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. 1329 42?… number 42? @$#%&*!! 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;
class Mutex_Demo { private static Mutex mut = new Mutex(); //create a new Mutex private const int numIterations = 2; private const int numThreads = 5; static void Main() for (int i = 0; i < numThreads; i++) Thread myThread = new Thread(new ThreadStart(MyThreadProc)); myThread.Name = String.Format("Thread{0}", i + 1); myThread.Start(); } Console.ReadKey(); mut.Close(); private static void MyThreadProc() for (int i = 0; i < numIterations; i++) UseResource(); private static void 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 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

Download ppt "Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm."

Similar presentations

Ads by Google