+ All Categories
Home > Documents > Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems [email protected].

Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems [email protected].

Date post: 20-Jan-2016
Category:
Upload: easter-ward
View: 219 times
Download: 0 times
Share this document with a friend
26
Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems http://groups.google.com/group/ cpe-os
Transcript
Page 1: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Process Synchronization I

Nov 27, 2007

CPE 261403 - Operating Systemshttp://groups.google.com/group/[email protected]

Page 2: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Why do we need to Synchronize?

Page 3: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Problem Example

A simple C primitive:

Count++

Actual machine code:

Register1 = countIncrease register1Count = register1

Page 4: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Problem Example

Register1 = countIncrease register1Count = register1

Register2 = countIncrease register2Count = register2

Register1 = countRegister2 = countIncrease register2Count = register2Increase register1Count = register1

Page 5: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Critical SectionCode where multiple processes (threads) can write to a shared location or acquire a shared resource.

Need some form of Synchronization!

Page 6: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Software Solution? Assuming only two processes

turn = p1;While (turn == p2) {/* busy wait */};

turn = p2;

//Critical SectionCount++;

turn = p2;While (turn == p1) {/* busy wait */};

turn = p1;

//Critical SectionCount++;

P1 P2(There are still problematic cases)

Page 7: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

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 SectionCount++;

turn = p1;p2_needs_to_work = true;While (turn == p1 && p1_needs_to_work) {/* busy wait */};

p2_needs_to_work = false;

//Critical SectionCount++;

P1 P2

Page 8: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Synchronization Hardware

Special atomic hardware instructions

Page 9: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Test-and-Set Instruction

Equivalent to:

boolean testAndSet(boolean *Lock) {

boolean originalVal = *Lock; *Lock = true; return originalVal;

}

Page 10: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Test-And-Set Applies to any N processes

While (testAndSet(&Lock)) { /* busy wait */ };

Lock = false;

//Critical SectionCount++;

While (testAndSet(&Lock)) { /* busy wait */ };

Lock = false;

//Critical SectionCount++;

P1 P2

Page 11: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Semaphores

Page 12: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

http://www.globalsecurity.org

Semaphore as a Signaling Tool

Page 13: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Semaphore as a Communication Tool

http://www.wikipedia.org

Page 14: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

R O G E R

Page 15: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Nuclear Disarmament http://www.viewimages.com

Semaphore in Pop Culture

Page 16: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

http://prop1.org

1981

2007

Page 17: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.
Page 18: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Semaphore as a Process Sync Tool

Critical section tool (Mutex Lock)

Counting Semaphore

Page 19: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Mutex Lock

Wait(S);

Signal(S);

//Critical SectionCount++;

Wait(S);

Signal(S);

//Critical SectionCount++;

P1 P2

Semaphore S;

Page 20: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Counting Semaphore

Wait(S);

Signal(S);

// Access Shared// resources

P1

Semaphore S = 3;

Database

P1

Page 21: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Semaphore Implementation

Wait (S) { value--; if (value < 0) { sleep(); } }

Signal (S){ value++; if (value <= 0) {

wakeupSleepingProcesses(); }}

Page 22: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

Semaphore Example I: Client-Server information request

P3: DatabaseManager

P1: Client P2: Server

Request DB Connection

DB DataResponse

P3: Limit 2 connections

Page 23: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

P3: DatabaseManager

P1: Client P2: Server

Request DB Connection

DB DataResponse

Semaphore Client = 0, Server = 1,

Database = 2, DBData = 0

Wait(server);Signal(Client);Wait(DBData);// receives the dataSignal(server);

While (true) {Wait(Client);Wait(Database);// make DB connection// and get the dataSignal(Database);Signal(DBData);

}

Client Server

Page 24: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

The Restaurant Model

Kitchen

Waiter

Chef (max 3 servings)

Restaurant

Page 25: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

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

Customer Waiter

Page 26: Process Synchronization I Nov 27, 2007 CPE 261403 - Operating Systems  cpe-os@googlegroups.com.

While (true) {Wait (Customer)Signal (Waiter)// gets the orderWait (Chef)// food is cookedSignal (Chef)Signal (Food)

}

Wait(Seats)Signal(Customer)Wait(Waiter)/// Places orderWait(Food)/// Eat the foodSignal(Seats)

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

Customer Waiter


Recommended