mutual exclusion

advertisement
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
• 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)
{
throw new Exception("Negative Balance");
}
// Comment out the next line to see the effect of leaving
lock (this)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : "
Console.WriteLine("Amount to Withdraw
: -"
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : "
return amount;
}
else
{
return 0; // transaction rejected
}
}
out lock
+ balance);
+ amount);
+ balance);
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...
Mutual Exclusion: Version 1
Mutual Exclusion: Version 2
Mutual Exclusion: Version 3
Mutual Exclusion: Version 4
Mutual Exclusion: Version 5
Mutual Exclusion: Version 6
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.
@$#%&*!!
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?
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
}
}
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
0.1
decide
accepts 0.9
Server Reserves
Seat(s)
Server Releases
Seat(s)
Seat Status Array
a shared resource
A B C D E
1
2
3
4
5
F
Possible Seat States
available
tagged
:
:
reserved
26
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
Download