350 likes | 359 Views
Process Synchronization I. CPE 261403 - Operating Systems http://www.e-cpe.org/moodle. Why do we need to Synchronize?. An Example: Air Ticket Reservation. Airbus A380. Online Reservation Systems. What happens when both customers see 5A as available and both want to reserve?. Customer A.
E N D
Process Synchronization I CPE 261403 - Operating Systems http://www.e-cpe.org/moodle
An Example: Air Ticket Reservation Airbus A380
What happens when both customers see 5A as available and both want to reserve? Customer A Customer B
Synchronization in a computer program • 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
Making Things Easier:Synchronization Hardware Special atomichardware instructions
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
Test-and-Set Instruction Equivalent to: boolean testAndSet(boolean *Lock) { boolean originalVal = *Lock; *Lock = true; return originalVal; } Constantly setting the Lock to “true” and return its original value. Therefore, when Lock becomes “false” only one process will get this value
Requirements for solving the Critical Section Problem • Mutual Exclusion One process at a time • Progress No deadlock • Bounded WaitingPreventing Starvation
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 Types of Semaphores
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) { S.value--; if (S.value < 0) { sleep(); } } Signal (S){ S.value++; if (S.value <= 0) { wakeupSleepingProcesses(); } }
Semaphore’s Application • Critical Section Tool • Resource Usage Control (limiting usage) • Synchronizing threads
Semaphore Example I: Resource Usage Control P1 Database Manager P2 … Pn Limit 2 connections
P1 Database Manager P2 … Pn Semaphore Database = 2 Wait(Database); // make DB connection // and get the data Signal(Database); P
Semaphore Example II: Process Synchronization DB Connection Request Response DB Data P1: Client P2: Server P3: Database Manager P2 limits 10 connection P3 limits 2 connections
Request DB Connection Response DB Data P1: Client P2: Server P3: Database Manager Semaphore Client = 0, Server = 10, 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 Dim sum Restaurant Waiter Waiter Restaurant Steamer • Waiters use the steamer to cook the dim sum • Waiters CANNOT share a steamer
Equivalent Computer Processes Customer Customer Customer Customer Customer Waiter Customer Customer Waiter Customer Process Resource Tables steamer
Semaphore Seats=6; Customer=0; Waiter=0; Steamer=1; Food=1; Customer Waiter
Semaphore Seats=6; Customer=0; Waiter=0; Steamer=1; Food=0; While (true) { Wait (Customer) Signal (Waiter) // gets the order Wait (Steamer) // food is cooked Signal (Steamer) Signal (Food) } Wait(Seats) Signal(Customer) Wait(Waiter) /// Places order Wait(Food) /// Eat the food Signal(Seats) Customer Waiter