260 likes | 280 Views
Process Synchronization I. Nov 27, 2007. CPE 261403 - Operating Systems http://groups.google.com/group/cpe-os cpe-os@googlegroups.com. Why do we need to Synchronize?. Problem Example. A simple C primitive: Count++. Actual machine code: Register1 = count Increase register1
E N D
Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems http://groups.google.com/group/cpe-os cpe-os@googlegroups.com
Problem Example • A simple C primitive: • Count++ • Actual machine code: • Register1 = count • Increase register1 • Count = register1
Problem Example • Register1 = count • Increase register1 • Count = register1 • Register2 = count • Increase register2 • Count = register2 • Register1 = count • Register2 = count • Increase register2 • Count = register2 • Increase register1 • Count = 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; turn = p2; While (turn == p1) {/* busy wait */}; turn = p1; • //Critical Section • Count++; • //Critical Section • Count++; 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; turn = p1; p2_needs_to_work = true; While (turn == p1 && p1_needs_to_work) {/* busy wait */}; p2_needs_to_work = false; • //Critical Section • Count++; • //Critical Section • Count++; P1 P2
Synchronization Hardware Special atomichardware 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; While (testAndSet(&Lock)) { /* busy wait */ }; Lock = false; • //Critical Section • Count++; • //Critical Section • Count++; P1 P2
Semaphore as a Signaling Tool http://www.globalsecurity.org
Semaphore as a Communication Tool http://www.wikipedia.org
R O G E R
Semaphore in Pop Culture http://www.viewimages.com Nuclear Disarmament
1981 2007 http://prop1.org
Critical section tool (Mutex Lock) Counting Semaphore Semaphore as a Process Sync Tool
Mutex Lock Semaphore S; Wait(S); Signal(S); Wait(S); Signal(S); • //Critical Section • Count++; • //Critical Section • Count++; P1 P2
Counting Semaphore Semaphore S = 3; Wait(S); Signal(S); P1 • // Access Shared • // resources 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 DB Connection Request Response DB Data P1: Client P2: Server P3: Database Manager P3: Limit 2 connections
Request DB Connection Response DB Data P1: Client P2: Server P3: Database Manager Semaphore Client = 0, Server = 1, Database = 2, DBData = 0 While (true) { Wait(Client); Wait(Database); // make DB connection // and get the data Signal(Database); Signal(DBData); } Wait(server); Signal(Client); Wait(DBData); // receives the data Signal(server); Client Server
The Restaurant Model Chef (max 3 servings) Waiter Restaurant Kitchen
Semaphore Seats=6; Customer=1; Waiter=1; Chef=3; Food=1; Customer Waiter
Semaphore Seats=6; Customer=1; Waiter=1; Chef=3; Food=1; While (true) { Wait (Customer) Signal (Waiter) // gets the order Wait (Chef) // food is cooked Signal (Chef) Signal (Food) } Wait(Seats) Signal(Customer) Wait(Waiter) /// Places order Wait(Food) /// Eat the food Signal(Seats) Customer Waiter