1 / 26

Process Synchronization I

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

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 Nov 27, 2007 CPE 261403 - Operating Systems http://groups.google.com/group/cpe-os cpe-os@googlegroups.com

  2. Why do we need to Synchronize?

  3. Problem Example • A simple C primitive: • Count++ • Actual machine code: • Register1 = count • Increase register1 • Count = register1

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

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

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

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

  8. Synchronization Hardware Special atomichardware instructions

  9. Test-and-Set Instruction Equivalent to: boolean testAndSet(boolean *Lock) { boolean originalVal = *Lock; *Lock = true; return originalVal; }

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

  11. Semaphores

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

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

  14. R O G E R

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

  16. 1981 2007 http://prop1.org

  17. Critical section tool (Mutex Lock) Counting Semaphore Semaphore as a Process Sync Tool

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

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

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

  21. Semaphore Example I: Client-Server information request DB Connection Request Response DB Data P1: Client P2: Server P3: Database Manager P3: Limit 2 connections

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

  23. The Restaurant Model Chef (max 3 servings) Waiter Restaurant Kitchen

  24. Semaphore Seats=6; Customer=1; Waiter=1; Chef=3; Food=1; Customer Waiter

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

More Related