1 / 35

Process Synchronization I

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.

jhendrix
Download Presentation

Process Synchronization I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Process Synchronization I CPE 261403 - Operating Systems http://www.e-cpe.org/moodle

  2. Why do we need to Synchronize?

  3. An Example: Air Ticket Reservation Airbus A380

  4. Online Reservation Systems

  5. What happens when both customers see 5A as available and both want to reserve? Customer A Customer B

  6. Synchronization in a computer program • A simple C primitive: • Count++ • Actual machine code: • Register1 = count • Increase register1 • Count = register1

  7. 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

  8. Critical SectionCode where multiple processes (threads) can write to a shared location or acquire a shared resource. Need some form of Synchronization!

  9. 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)

  10. 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

  11. Making Things Easier:Synchronization Hardware Special atomichardware instructions

  12. 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

  13. 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

  14. Requirements for solving the Critical Section Problem • Mutual Exclusion One process at a time • Progress No deadlock • Bounded WaitingPreventing Starvation

  15. Semaphores

  16. Semaphore as a Signaling Tool http://www.globalsecurity.org

  17. Semaphore as a Communication Tool http://www.wikipedia.org

  18. R O G E R

  19. Semaphore in Pop Culture http://www.viewimages.com Nuclear Disarmament

  20. 1981 2007 http://prop1.org

  21. Critical section tool (Mutex Lock) Counting Semaphore Types of Semaphores

  22. Mutex Lock Semaphore S; Wait(S); Signal(S); Wait(S); Signal(S); • //Critical Section • Count++; • //Critical Section • Count++; P1 P2

  23. Counting Semaphore Semaphore S = 3; Wait(S); Signal(S); P1 • // Access Shared • // resources Database P1

  24. Semaphore Implementation Wait (S) { S.value--; if (S.value < 0) { sleep(); } } Signal (S){ S.value++; if (S.value <= 0) { wakeupSleepingProcesses(); } }

  25. Semaphore’s Application • Critical Section Tool • Resource Usage Control (limiting usage) • Synchronizing threads

  26. Semaphore Example I: Resource Usage Control P1 Database Manager P2 … Pn Limit 2 connections

  27. P1 Database Manager P2 … Pn Semaphore Database = 2 Wait(Database); // make DB connection // and get the data Signal(Database); P

  28. 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

  29. 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

  30. The Dim sum Restaurant

  31. The Dim sum Restaurant Waiter Waiter Restaurant Steamer • Waiters use the steamer to cook the dim sum • Waiters CANNOT share a steamer

  32. Equivalent Computer Processes Customer Customer Customer Customer Customer Waiter Customer Customer Waiter Customer Process Resource Tables steamer

  33. Semaphore Seats=6; Customer=0; Waiter=0; Steamer=1; Food=1; Customer Waiter

  34. 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

More Related