Download presentation
Presentation is loading. Please wait.
Published byEaster Ward Modified over 9 years ago
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 Section Code 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; //Critical Section Count++; turn = p2; While (turn == p1) {/* busy wait */}; turn = p1; //Critical Section Count++; P1P2 (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; //Critical Section Count++; turn = p1; p2_needs_to_work = true; While (turn == p1 && p1_needs_to_work) {/* busy wait */}; p2_needs_to_work = false; //Critical Section Count++; P1P2
8
Synchronization Hardware Special atomic hardware 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; //Critical Section Count++; While (testAndSet(&Lock)) { /* busy wait */ }; Lock = false; //Critical Section Count++; P1P2
11
Semaphores
12
http://www.globalsecurity.org Semaphore as a Signaling Tool
13
Semaphore as a Communication Tool http://www.wikipedia.org
14
ROGER
15
Nuclear Disarmamenthttp://www.viewimages.com Semaphore in Pop Culture
16
http://prop1.org 1981 2007
18
Semaphore as a Process Sync Tool Critical section tool (Mutex Lock) Counting Semaphore
19
Mutex Lock Wait(S); Signal(S); //Critical Section Count++; Wait(S); Signal(S); //Critical Section Count++; P1P2 Semaphore S;
20
Counting Semaphore Wait(S); Signal(S); // Access Shared // resources P1 Semaphore S = 3; Database P1
21
Semaphore Implementation Wait (S) { value--; if (value < 0) { sleep(); } Signal (S){ value++; if (value <= 0) { wakeupSleepingProcesses(); }
22
Semaphore Example I: Client-Server information request P3: Database Manager P1: Client P2: Server Request DB Connection DB DataResponse P3: Limit 2 connections
23
P3: Database Manager P1: Client P2: Server RequestDB Connection DB DataResponse Semaphore Client = 0, Server = 1, Database = 2, DBData = 0 Wait(server); Signal(Client); Wait(DBData); // receives the data Signal(server); While (true) { Wait(Client); Wait(Database); // make DB connection // and get the data Signal(Database); Signal(DBData); } ClientServer
24
The Restaurant Model Kitchen Waiter Chef (max 3 servings) Restaurant
25
Semaphore Seats=6; Customer=1; Waiter=1; Chef=3; Food=1; CustomerWaiter
26
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) Semaphore Seats=6; Customer=1; Waiter=1; Chef=3; Food=1; CustomerWaiter
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.