Date post: | 20-Jan-2016 |
Category: |
Documents |
Upload: | easter-ward |
View: | 219 times |
Download: | 0 times |
Process Synchronization I
Nov 27, 2007
CPE 261403 - Operating Systemshttp://groups.google.com/group/[email protected]
Why do we need to Synchronize?
Problem Example
A simple C primitive:
Count++
Actual machine code:
Register1 = countIncrease register1Count = register1
Problem Example
Register1 = countIncrease register1Count = register1
Register2 = countIncrease register2Count = register2
Register1 = countRegister2 = countIncrease register2Count = register2Increase register1Count = register1
Critical SectionCode where multiple processes (threads) can write to a shared location or acquire a shared resource.
Need some form of Synchronization!
Software Solution? Assuming only two processes
turn = p1;While (turn == p2) {/* busy wait */};
turn = p2;
//Critical SectionCount++;
turn = p2;While (turn == p1) {/* busy wait */};
turn = p1;
//Critical SectionCount++;
P1 P2(There are still problematic cases)
Software Solution? Peterson’s Solution
turn = p2;p1_needs_to_work = true;While (turn == p2 && p2_needs_to_work) {/* busy wait */};
p1_needs_to_work = false;
//Critical SectionCount++;
turn = p1;p2_needs_to_work = true;While (turn == p1 && p1_needs_to_work) {/* busy wait */};
p2_needs_to_work = false;
//Critical SectionCount++;
P1 P2
Synchronization Hardware
Special atomic hardware instructions
Test-and-Set Instruction
Equivalent to:
boolean testAndSet(boolean *Lock) {
boolean originalVal = *Lock; *Lock = true; return originalVal;
}
Test-And-Set Applies to any N processes
While (testAndSet(&Lock)) { /* busy wait */ };
Lock = false;
//Critical SectionCount++;
While (testAndSet(&Lock)) { /* busy wait */ };
Lock = false;
//Critical SectionCount++;
P1 P2
Semaphores
http://www.globalsecurity.org
Semaphore as a Signaling Tool
Semaphore as a Communication Tool
http://www.wikipedia.org
R O G E R
Nuclear Disarmament http://www.viewimages.com
Semaphore in Pop Culture
http://prop1.org
1981
2007
Semaphore as a Process Sync Tool
Critical section tool (Mutex Lock)
Counting Semaphore
Mutex Lock
Wait(S);
Signal(S);
//Critical SectionCount++;
Wait(S);
Signal(S);
//Critical SectionCount++;
P1 P2
Semaphore S;
Counting Semaphore
Wait(S);
Signal(S);
// Access Shared// resources
P1
Semaphore S = 3;
Database
P1
Semaphore Implementation
Wait (S) { value--; if (value < 0) { sleep(); } }
Signal (S){ value++; if (value <= 0) {
wakeupSleepingProcesses(); }}
Semaphore Example I: Client-Server information request
P3: DatabaseManager
P1: Client P2: Server
Request DB Connection
DB DataResponse
P3: Limit 2 connections
P3: DatabaseManager
P1: Client P2: Server
Request DB Connection
DB DataResponse
Semaphore Client = 0, Server = 1,
Database = 2, DBData = 0
Wait(server);Signal(Client);Wait(DBData);// receives the dataSignal(server);
While (true) {Wait(Client);Wait(Database);// make DB connection// and get the dataSignal(Database);Signal(DBData);
}
Client Server
The Restaurant Model
Kitchen
Waiter
Chef (max 3 servings)
Restaurant
Semaphore Seats=6;Customer=1;Waiter=1;Chef=3;Food=1;
Customer Waiter
While (true) {Wait (Customer)Signal (Waiter)// gets the orderWait (Chef)// food is cookedSignal (Chef)Signal (Food)
}
Wait(Seats)Signal(Customer)Wait(Waiter)/// Places orderWait(Food)/// Eat the foodSignal(Seats)
Semaphore Seats=6;Customer=1;Waiter=1;Chef=3;Food=1;
Customer Waiter