Chapter 05 in os

64
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL

Transcript of Chapter 05 in os

Page 1: Chapter 05 in os

Chapter 5Concurrency: Mutual

Exclusion and Synchronization

Operating Systems:Internals and Design Principles, 6/E

William Stallings

Patricia RoyManatee Community College, Venice, FL©2008, Prentice Hall

Page 2: Chapter 05 in os

Concurrency Problem: A Simple Example

char chin, chout;void echo(){

chin = getchar();chout = chin;putchar(chout);

}

Page 3: Chapter 05 in os

Concurrency Problem: A Simple Example

Process P1 Process P2. .

chin = getchar(); .. chin =

getchar();chout = chin; chout = chin;putchar(chout); .

.putchar(chout);

. .

Page 4: Chapter 05 in os

Multiprogramming Concerns

• Output of process must be independent of the speed of execution of other concurrent processes

Page 5: Chapter 05 in os

Competition among Processes for Resources

• Mutual Exclusion–Critical sections

• Deadlock• Starvation

Page 6: Chapter 05 in os

Key Terms

Page 7: Chapter 05 in os

Race Condition Example

Process P1 Process P2. .

chin = getchar(); .. chin =

getchar();chout = chin; chout = chin;putchar(chout); .

.putchar(chout);

. .

Page 8: Chapter 05 in os

Requirements for Mutual Exclusion

• Only one process at a time is allowed in the critical section for a resource

• A process that halts in its noncritical section must do so without interfering with other processes

• No deadlock or starvation

Page 9: Chapter 05 in os

Requirements for Mutual Exclusion

• A process must not be delayed access to a critical section when there is no other process using it

• No assumptions are made about relative process speeds or number of processors

• A process remains inside its critical section for a finite time only

Page 10: Chapter 05 in os

Mutual Exclusion Approaches

• Software approach Appendix A

• Hardware support, using special-purpose machine instructions

• Support within OS or a programming language–Semaphore, Monitors, Message Passing

Page 11: Chapter 05 in os

Mutual Exclusion: Hardware Support

• Interrupt Disabling–A process runs until it invokes an operating

system service or until it is interrupted–Disabling interrupts guarantees mutual

exclusion• Disadvantages:

–Processor is limited in its ability to interleave programs

–Will not work in multiprocessor architecture

Page 12: Chapter 05 in os

Mutual Exclusion: Hardware Support

• Compare&Swap Instructionint compare_and_swap (int *word, int testval, int newval){

int oldval;oldval = *word;if (oldval == testval) *word = newval;return oldval;

}

Page 13: Chapter 05 in os

Mutual Exclusion

Page 14: Chapter 05 in os

Mutual Exclusion: Hardware Support

• Exchange instructionvoid exchange (int *register, int *memory){

int temp;temp = *memory;*memory = *register;*register = temp;

}

Page 15: Chapter 05 in os

Mutual Exclusion

Page 16: Chapter 05 in os

Mutual Exclusion Machine-Instruction

• Advantages–Applicable to any number of processes on a

single processor –Processes on multiple processors?– as long as processors share main memory–It is simple and therefore easy to verify–It can be used to support multiple critical

sections; each critical section can be defined by its own variable (e.g. bolt1, bolt2, etc.)

Page 17: Chapter 05 in os

Mutual Exclusion Machine-Instruction

• Disadvantages–Busy-waiting consumes processor time–Starvation?– Starvation is possible when a process leaves a critical

section and more than one process is waiting –Deadlock?

Page 18: Chapter 05 in os

Semaphores

• Special variable called a semaphore is used for signaling

• If a process is waiting for a signal, it is suspended until that signal is sent

Page 19: Chapter 05 in os

Semaphores

• Semaphore is a variable that has an integer value–May be initialized to a nonnegative number–Wait operation decrements the semaphore

value–Signal operation increments semaphore value

Page 20: Chapter 05 in os

Semaphore Primitives

Page 21: Chapter 05 in os

Example of Semaphore Mechanism

Page 22: Chapter 05 in os

Example of Semaphore Mechanism

Page 23: Chapter 05 in os

Mutual Exclusion Using Semaphores

Page 24: Chapter 05 in os

Mutual Exclusion Using Semaphores

Page 25: Chapter 05 in os

Processes Using Semaphore

Page 26: Chapter 05 in os

Binary Semaphore Primitives

Page 27: Chapter 05 in os

Producer/Consumer Problem

• One or more producers are generating data and placing these in a buffer

• One or more consumers are taking items out of the buffer one at time

• Only one producer or consumer may access the buffer at any one time

• Producer can’t add data into full buffer and consumer can’t remove data from empty buffer

Page 28: Chapter 05 in os

Buffer

Page 29: Chapter 05 in os

Producer

• producer:while (true) {

/* produce item v */b[in] = v;in++;

}

Page 30: Chapter 05 in os

Consumer

• consumer:while (true) { while (in <= out)

/*do nothing */;w = b[out];out++; /* consume item w */

}

Page 31: Chapter 05 in os

Incorrect Solution

Page 32: Chapter 05 in os

32

Page 33: Chapter 05 in os

Correct Solution

Page 34: Chapter 05 in os

Semaphores

Page 35: Chapter 05 in os

Circular Buffer

Page 36: Chapter 05 in os

Producer with Circular Buffer

• producer:while (true) {

/* produce item v */while ((in + 1) % n == out) /* do

nothing */;b[in] = v;in = (in + 1) % n

}

Page 37: Chapter 05 in os

Consumer with Circular Buffer

• consumer:while (true) {

while (in == out)/* do nothing */;

w = b[out];out = (out + 1) % n;/* consume item w */

}

Page 38: Chapter 05 in os

Semaphores

Page 39: Chapter 05 in os

Semaphores

Page 40: Chapter 05 in os

Semaphore Implementation: Atomic Primitives

Page 41: Chapter 05 in os
Page 42: Chapter 05 in os

Mutual Exclusion Machine-Instruction vs. Semaphore

Page 43: Chapter 05 in os

Mutual Exclusion Machine-Instruction vs. Semaphore

Page 44: Chapter 05 in os

Mutual Exclusion Approaches

• Software approach Appendix A

• Hardware support, using special-purpose machine instructions

• Support within OS or a programming language–Semaphore, Monitors, Message Passing

Page 46: Chapter 05 in os

Mutual Exclusion Approaches

• Software approach Appendix A

• Hardware support, using special-purpose machine instructions

• Support within OS or a programming language–Semaphore, Monitors, Message Passing

Page 47: Chapter 05 in os

Message Passing

• Enforce mutual exclusion• Exchange information

• send (destination, message)• receive (source, message)

Page 48: Chapter 05 in os

Synchronization

• The receiver cannot receive a message until it has been sent by another process

• What happens to a process after it issues a send or receive primitive–Sender and receiver may or may not be blocked

(waiting for message)

Page 49: Chapter 05 in os

Synchronization• Blocking send, blocking receive

–Both sender and receiver are blocked until message is delivered

• Nonblocking send, blocking receive–Sender continues on–Receiver is blocked until the requested

message arrives• Nonblocking send, nonblocking receive

–Neither party is required to wait

Page 50: Chapter 05 in os

Addressing

• Direct addressing–Send primitive includes a specific identifier of the

destination process–Receive primitive may/may not know ahead of

time from which process a message is expected–Receive primitive could use source parameter to

return a value when the receive operation has been performed

Page 51: Chapter 05 in os

Addressing

• Indirect addressing–Messages are sent to a shared data structure

consisting of queues–Queues are called mailboxes–One process sends a message to the mailbox

and the other process picks up the message from the mailbox

Page 52: Chapter 05 in os

Indirect Process Communication

Page 53: Chapter 05 in os

General Message Format

Page 54: Chapter 05 in os

Mutual Exclusion Using Messages (1)

• Assume–blocking receive primitive

&–nonblocking send primitive

Page 55: Chapter 05 in os

Mutual Exclusion Using Messages (2)

Page 56: Chapter 05 in os

Producer/Consumer Messages

Page 57: Chapter 05 in os

Readers/Writers Problem• Any number of readers may simultaneously read the file

• Only one writer at a time may write to the file• If a writer is writing to the file, no reader may read it

• How is this problem different from mutual exclusion problem?

• Cast it to a mutual exclusion problem?

Page 58: Chapter 05 in os

Any Problem with This Solution?

Page 59: Chapter 05 in os

Readers have Priority

Page 60: Chapter 05 in os

Writers Have Priority• The following semaphores and variables are added: –A semaphore rsem that inhibits all readers while

there is at least one writer desiring access to the data area

–A variable writecount that controls the setting of rsem

–A semaphore y that controls the updating of writecount

–A semaphore z that prevents a long queue of readers to build up on rsem

Page 61: Chapter 05 in os

Writers have Priority

Page 62: Chapter 05 in os

Writers have Priority

Page 63: Chapter 05 in os

Writers have Priority

Page 64: Chapter 05 in os

Message Passing

Note: count is initialized to the maximum possible number of readers, e.g., 100.