Top Banner
Concurrency: Mutual Concurrency: Mutual Exclusion and Exclusion and Synchronization Synchronization Chapter 5 1
56

Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Jan 17, 2016

Download

Documents

Lee Casey
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Concurrency: Mutual Concurrency: Mutual Exclusion and Exclusion and SynchronizationSynchronizationChapter 5

1

Page 2: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

ConcurrencyConcurrencyMultiple applicationsStructured applicationsOperating system structure

2

Page 3: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

ConcurrencyConcurrency

3

Page 4: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Difficulties of ConcurrencyDifficulties of ConcurrencySharing of global resourcesOperating system managing the

allocation of resources optimallyDifficult to locate programming

errors

4

Page 5: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

CurrencyCurrencyCommunication among processesSharing resourcesSynchronization of multiple

processesAllocation of processor time

5

Page 6: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

ConcurrencyConcurrencyMultiple applications

◦MultiprogrammingStructured application

◦Application can be a set of concurrent processes

Operating-system structure◦Operating system is a set of

processes or threads

6

Page 7: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

A Simple ExampleA Simple Example

void echo(){chin = getchar();chout = chin;putchar(chout);

}

7

Page 8: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

A Simple ExampleA Simple Example

Process P1 Process P2. .chin = getchar(); .. chin = getchar();

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

8

Page 9: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Operating System Operating System ConcernsConcernsKeep track of various processesAllocate and deallocate resources

◦ Processor time◦ Memory◦ Files◦ I/O devices

Protect data and resourcesOutput of process must be

independent of the speed of execution of other concurrent processes

9

Page 10: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Process InteractionProcess InteractionProcesses unaware of each otherProcesses indirectly aware of

each otherProcess directly aware of each

other

10

Page 11: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

11

Page 12: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Competition Among Competition Among Processes for ResourcesProcesses for ResourcesMutual Exclusion

◦Critical sections Only one program at a time is allowed in

its critical section Example only one process at a time is

allowed to send command to the printer

DeadlockStarvation

12

Page 13: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Requirements for Mutual Requirements for Mutual ExclusionExclusionOnly 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

13

Page 14: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Requirements for Mutual Requirements for Mutual ExclusionExclusionA 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 processes

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

14

Page 15: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual Exclusion:Mutual Exclusion:Hardware SupportHardware SupportInterrupt Disabling

◦A process runs until it invokes an operating system service or until it is interrupted

◦Disabling interrupts guarantees mutual exclusion

◦Processor is limited in its ability to interleave programs

◦Multiprocessing disabling interrupts on one processor

will not guarantee mutual exclusion

15

Page 16: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual Exclusion:Mutual Exclusion:Hardware SupportHardware SupportSpecial Machine Instructions

◦Performed in a single instruction cycle

◦Access to the memory location is blocked for any other instructions

16

Page 17: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual Exclusion:Mutual Exclusion:Hardware SupportHardware SupportTest and Set Instruction

boolean testset (int i) {if (i == 0) {

i = 1;return true;

}else {

return false;}

}

17

Page 18: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual Exclusion:Mutual Exclusion:Hardware SupportHardware SupportExchange Instructionvoid exchange(int register,

int memory) {int temp;temp = memory;memory = register;register = temp;

}

18

Page 19: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual ExclusionMutual Exclusion

19

Page 20: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual Exclusion Machine Mutual Exclusion Machine InstructionsInstructionsAdvantages

◦Applicable to any number of processes on either a single processor or multiple processors sharing main memory

◦It is simple and therefore easy to verify

◦It can be used to support multiple critical sections

20

Page 21: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual Exclusion Machine Mutual Exclusion Machine InstructionsInstructionsDisadvantages

◦Busy-waiting consumes processor time◦Starvation is possible when a process

leaves a critical section and more than one process is waiting.

◦Deadlock If a low priority process has the critical

region and a higher priority process needs, the higher priority process will obtain the processor to wait for the critical region

21

Page 22: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

SemaphoresSemaphoresSpecial variable called a

semaphore is used for signalingIf a process is waiting for a

signal, it is suspended until that signal is sent

22

Page 23: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

SemaphoresSemaphoresSemaphore 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

23

Page 24: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Semaphore PrimitivesSemaphore Primitives

24

Page 25: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Binary Semaphore Binary Semaphore PrimitivesPrimitives

25

Page 26: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Mutual Exclusion Using Mutual Exclusion Using SemaphoresSemaphores

26

Page 27: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

27

Page 28: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

28

Page 29: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Producer/Consumer Producer/Consumer ProblemProblemOne or more producers are

generating data and placing these in a buffer

A single consumer is taking items out of the buffer one at time

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

29

Page 30: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

ProducerProducer

producer:while (true) {/* produce item v */b[in] = v;in++;

}

30

Page 31: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

ConsumerConsumerconsumer:while (true) { while (in <= out)

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

}

31

Page 32: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Producer/Consumer Producer/Consumer ProblemProblem

32

Page 33: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Producer with Circular Producer with Circular BufferBufferproducer:while (true) {/* produce item v */while ((in + 1) % n == out)

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

}

33

Page 34: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Consumer with Circular Consumer with Circular BufferBufferconsumer:while (true) {while (in == out)

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

}

34

Page 35: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

35

Page 36: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

36

Page 37: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

37

Page 38: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

38

Page 39: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

39

Page 40: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

MonitorsMonitorsMonitor is a software moduleChief characteristics

◦Local data variables are accessible only by the monitor

◦Process enters monitor by invoking one of its procedures

◦Only one process may be executing in the monitor at a time

40

Page 41: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

41

Page 42: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

42

Page 43: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

43

Page 44: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

44

Page 45: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Message PassingMessage PassingEnforce mutual exclusionExchange information

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

45

Page 46: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

SynchronizationSynchronizationSender and receiver may or may

not be blocking (waiting for message)

Blocking send, blocking receive◦Both sender and receiver are

blocked until message is delivered◦Called a rendezvous

46

Page 47: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

SynchronizationSynchronizationNonblocking send, blocking

receive◦Sender continues on◦Receiver is blocked until the

requested message arrivesNonblocking send, nonblocking

receive◦Neither party is required to wait

47

Page 48: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

AddressingAddressingDirect addressing

◦Send primitive includes a specific identifier of the destination process

◦Receive primitive could know ahead of time which process a message is expected

◦Receive primitive could use source parameter to return a value when the receive operation has been performed

48

Page 49: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

AddressingAddressingIndirect 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

49

Page 50: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

50

Page 51: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Message FormatMessage Format

51

Page 52: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

52

Page 53: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

53

Page 54: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

Readers/Writers ProblemReaders/Writers ProblemAny number of readers may

simultaneously read the fileOnly one writer at a time may

write to the fileIf a writer is writing to the file, no

reader may read it

54

Page 55: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

55

Page 56: Concurrency: Mutual Exclusion and Synchronization Chapter 5 1.

56