Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process Synchronization I CPE 261403 - Operating Systems

Similar presentations


Presentation on theme: "Process Synchronization I CPE 261403 - Operating Systems"— Presentation transcript:

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

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 Requirements for solving the Critical Section Problem Mutual Exclusion Progress Bounded Waiting

12 Semaphores

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

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

15 ROGER

16 Nuclear Disarmamenthttp://www.viewimages.com Semaphore in Pop Culture

17 http://prop1.org 1981 2007

18

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

20 Mutex Lock Wait(S); Signal(S); //Critical Section Count++; Wait(S); Signal(S); //Critical Section Count++; P1P2 Semaphore S;

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

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

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

24 Semaphore Example I: Resource Usage Control Database Manager P1 Limit 2 connections P2 PnPn …

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

26 Semaphore Example II: Process Synchronization P3: Database Manager P1: Client P2: Server Request DB Connection DB DataResponse P2 limits 10 connection P3 limits 2 connections

27 P3: Database Manager P1: Client P2: Server RequestDB Connection DB DataResponse Semaphore Client = 0, Server = 10, 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

28 The Dim sum Restaurant

29 Steamers Waiter Restaurant Waiter

30 Semaphore Seats=6; Customer=0; Waiter=2; Steamer=3; Food=1; CustomerWaiter

31 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) Semaphore Seats=6; Customer=0; Waiter=1; Steamer=3; Food=0; CustomerWaiter


Download ppt "Process Synchronization I CPE 261403 - Operating Systems"

Similar presentations


Ads by Google