1 / 40

Today’s class

Learn about mutual exclusion and synchronization techniques in computer systems, including hardware support, semaphores, and solving producer/consumer and readers/writers problems.

lthomas
Download Presentation

Today’s class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s class • Mutual exclusion and synchronization • Hardware support • Semaphores • Producer/Consumer problem • Readers/Writers problem Computer Systems/Operating Systems - Class 9

  2. 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 • Processor is limited in its ability to interleave programs • Multiprocessing - disabling interrupts on one processor will not guarantee mutual exclusion Computer Systems/Operating Systems - Class 9

  3. Mutual Exclusion:Hardware Support while (true) { /* disable interrupts */ /* critical section */ /* enable interrupts */ /* remainder */ } Computer Systems/Operating Systems - Class 9

  4. Mutual Exclusion:Hardware Support • Special Machine Instructions • Performed in a single instruction cycle • Access to the memory location is blocked for any other instructions Computer Systems/Operating Systems - Class 9

  5. Mutual Exclusion:Hardware Support • Test and Set Instruction boolean testset (int i) { if (i == 0) { i = 1; return true; } else { return false; } } Computer Systems/Operating Systems - Class 9

  6. Mutual Exclusion Based on Test and Set Computer Systems/Operating Systems - Class 9

  7. Mutual Exclusion:Hardware Support • Exchange Instruction void exchange(int register, int memory) { int temp; temp = memory; memory = register; register = temp; } Computer Systems/Operating Systems - Class 9

  8. Mutual Exclusion Based on Exchange Computer Systems/Operating Systems - Class 9

  9. Mutual Exclusion Machine Instructions • 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 Computer Systems/Operating Systems - Class 9

  10. Mutual Exclusion Machine Instructions • 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 Computer Systems/Operating Systems - Class 9

  11. 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 • semSignal(s) transmits a signal via semaphore s • semWait(s) receives a signal via semaphore s; if the signal has not yet been sent, the process is suspended until the transmission takes place Computer Systems/Operating Systems - Class 9

  12. Semaphores • Semaphore is a variable that has an integer value • May be initialized to a nonnegative number • semWait operation decrements the semaphore value; if it becomes negative then the process executing semWait is blocked, otherwise the process continues execution • semSignal operation increments the semaphore value; if the value is less than or equal to 0 then a process blocked by semWait is unblocked Computer Systems/Operating Systems - Class 9

  13. Semaphore Primitives Computer Systems/Operating Systems - Class 9

  14. Binary Semaphore Primitives Computer Systems/Operating Systems - Class 9

  15. Mutual Exclusion Using Semaphores Computer Systems/Operating Systems - Class 9

  16. Computer Systems/Operating Systems - Class 9

  17. Producer/Consumer Problem • One 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 Computer Systems/Operating Systems - Class 9

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

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

  20. Producer/Consumer Problem Computer Systems/Operating Systems - Class 9

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

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

  23. Computer Systems/Operating Systems - Class 9

  24. Computer Systems/Operating Systems - Class 9

  25. Computer Systems/Operating Systems - Class 9

  26. Computer Systems/Operating Systems - Class 9

  27. Computer Systems/Operating Systems - Class 9

  28. Monitors • Monitor is a software module • Chief 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 Computer Systems/Operating Systems - Class 9

  29. Computer Systems/Operating Systems - Class 9

  30. Computer Systems/Operating Systems - Class 9

  31. Computer Systems/Operating Systems - Class 9

  32. Message Passing • Enforce mutual exclusion • Exchange information send (destination, message) receive (source, message) Computer Systems/Operating Systems - Class 9

  33. Synchronization • 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 Computer Systems/Operating Systems - Class 9

  34. Synchronization • 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 Computer Systems/Operating Systems - Class 9

  35. Addressing • 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 Computer Systems/Operating Systems - Class 9

  36. 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 Computer Systems/Operating Systems - Class 9

  37. Message Format Computer Systems/Operating Systems - Class 9

  38. 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 Computer Systems/Operating Systems - Class 9

  39. Computer Systems/Operating Systems - Class 9

  40. Computer Systems/Operating Systems - Class 9

More Related