Informationsteknologi Todays class n Mutual exclusion and synchronization

  • Slides: 40
Download presentation
Informationsteknologi Today’s class n Mutual exclusion and synchronization ® Hardware support ® Semaphores ®

Informationsteknologi Today’s class n Mutual exclusion and synchronization ® Hardware support ® Semaphores ® Producer/Consumer problem ® Readers/Writers problem Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 1

Informationsteknologi Mutual Exclusion: Hardware Support n Interrupt Disabling ®A process runs until it invokes

Informationsteknologi Mutual Exclusion: Hardware Support n Interrupt 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 Wednesday, September 26, Computer Systems/Operating Systems - Class 9 2 2007

Informationsteknologi Mutual Exclusion: Hardware Support while /* /* } (true) { disable interrupts */

Informationsteknologi Mutual Exclusion: Hardware Support while /* /* } (true) { disable interrupts */ critical section */ enable interrupts */ remainder */ Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 3

Informationsteknologi Mutual Exclusion: Hardware Support n Special Machine Instructions ® Performed in a single

Informationsteknologi Mutual Exclusion: Hardware Support n Special Machine Instructions ® Performed in a single instruction cycle ® Access to the memory location is blocked for any other instructions Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 4

Informationsteknologi Mutual Exclusion: Hardware Support n Test and Set Instruction boolean testset (int i)

Informationsteknologi Mutual Exclusion: Hardware Support n Test and Set Instruction boolean testset (int i) { if (i == 0) { i = 1; return true; } else { return false; } } Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 5

Informationsteknologi Mutual Exclusion Based on Test and Set Wednesday, September 26, 2007 Computer Systems/Operating

Informationsteknologi Mutual Exclusion Based on Test and Set Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 6

Informationsteknologi Mutual Exclusion: Hardware Support Exchange Instruction void exchange(int register, int memory) { int

Informationsteknologi Mutual Exclusion: Hardware Support Exchange Instruction void exchange(int register, int memory) { int temp; temp = memory; memory = register; register = temp; } Wednesday, September 26, Computer Systems/Operating Systems - Class 9 n 2007 7

Informationsteknologi Mutual Exclusion Based on Exchange Wednesday, September 26, 2007 Computer Systems/Operating Systems -

Informationsteknologi Mutual Exclusion Based on Exchange Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 8

Informationsteknologi Mutual Exclusion Machine Instructions n Advantages ® Applicable to any number of processes

Informationsteknologi Mutual Exclusion Machine Instructions n Advantages ® 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 Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 9

Informationsteknologi Mutual Exclusion Machine Instructions n Disadvantages ® Busy-waiting consumes processor time ® Starvation

Informationsteknologi Mutual Exclusion Machine Instructions n Disadvantages ® 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 it, the higher priority process will obtain the processor to wait for the critical region Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 10

Informationsteknologi Semaphores n n Special variable called a semaphore is used for signaling If

Informationsteknologi Semaphores n n 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 sem. Signal(s) transmits a signal via semaphore s sem. Wait(s) receives a signal via semaphore s; if the signal has not yet been sent, the process is suspended until the transmission takes place Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 11

Informationsteknologi Semaphores n Semaphore is a variable that has an integer value May be

Informationsteknologi Semaphores n Semaphore is a variable that has an integer value May be initialized to a nonnegative number ® sem. Wait operation decrements the semaphore value; if it becomes negative then the process executing sem. Wait is blocked, otherwise the process continues execution ® sem. Signal operation increments the semaphore value; if the value is less than or equal to 0 then a process blocked by sem. Wait is unblocked ® Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 12

Informationsteknologi Semaphore Primitives Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 13

Informationsteknologi Semaphore Primitives Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 13

Informationsteknologi Binary Semaphore Primitives Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9

Informationsteknologi Binary Semaphore Primitives Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 14

Informationsteknologi Mutual Exclusion Using Semaphores Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class

Informationsteknologi Mutual Exclusion Using Semaphores Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 15

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 16

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 16

Informationsteknologi Producer/Consumer Problem One or more producers are generating data and placing these in

Informationsteknologi Producer/Consumer Problem One or more producers are generating data and placing these in a buffer n A single consumer is taking items out of the buffer one at time n Only one producer or consumer may access the buffer at any one time n Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 17

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

Informationsteknologi Producer producer: while (true) { /* produce item v */ b[in] = v; in++; } Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 18

Informationsteknologi Consumer consumer: while (true) { while (in <= out) /*do nothing */; w

Informationsteknologi Consumer consumer: while (true) { while (in <= out) /*do nothing */; w = b[out]; out++; /* consume item w */ } Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 19

Informationsteknologi Producer/Consumer Problem Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 20

Informationsteknologi Producer/Consumer Problem Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 20

Informationsteknologi Producer with Circular Buffer producer: while (true) { /* produce item v */

Informationsteknologi Producer with Circular Buffer producer: while (true) { /* produce item v */ while ((in + 1) % n == out) /* do nothing */; b[in] = v; in = (in + 1) % n } Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 21

Informationsteknologi Consumer with Circular Buffer consumer: while (true) { while (in == out) /*

Informationsteknologi Consumer with Circular Buffer consumer: while (true) { while (in == out) /* do nothing */; w = b[out]; out = (out + 1) % n; /* consume item w */ } Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 22

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 23

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 23

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 24

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 24

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 25

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 25

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 26

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 26

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 27

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 27

Informationsteknologi Monitors Monitor is a software module n Chief characteristics n ® Local data

Informationsteknologi Monitors Monitor is a software module n Chief characteristics n ® 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 Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 28

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 29

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 29

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 30

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 30

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 31

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 31

Informationsteknologi Message Passing Enforce mutual exclusion n Exchange information n send (destination, message) receive

Informationsteknologi Message Passing Enforce mutual exclusion n Exchange information n send (destination, message) receive (source, message) Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 32

Informationsteknologi Synchronization n Message communication requires some level of synchronization – a message cannot

Informationsteknologi Synchronization n Message communication requires some level of synchronization – a message cannot be received before it is sent Sender 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 ® Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 33

Informationsteknologi Synchronization n Nonblocking send, blocking receive ® Sender continues on ® Receiver is

Informationsteknologi Synchronization n Nonblocking send, blocking receive ® Sender continues on ® Receiver is blocked until the requested message arrives n Nonblocking send, nonblocking receive ® Neither party is required to wait Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 34

Informationsteknologi Addressing n Direct addressing ® Send primitive includes a specific identifier of the

Informationsteknologi Addressing n Direct 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 Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 35

Informationsteknologi Addressing n Indirect addressing ® Messages are sent to a shared data structure

Informationsteknologi Addressing n 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 Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 36

Informationsteknologi Message Format Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 37

Informationsteknologi Message Format Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 37

Informationsteknologi Readers/Writers Problem Any number of readers may simultaneously read the file n Only

Informationsteknologi Readers/Writers Problem Any number of readers may simultaneously read the file n Only one writer at a time may write to the file n If a writer is writing to the file, no reader may read it n Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 38

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 39

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 39

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 40

Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 9 40