We define a semaphore as a c struct the wait

This preview shows page 16 - 21 out of 39 pages.

We define a semaphore as a “C” struct : ! The wait() semaphore operation: typedef struct { int value; struct process *list; } semaphore; wait(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); } } 32 Semaphores (cont’d) ! The signal() semaphore operation: ! The list can be implemented as a FIFO queue or priority queue on certain condition value (for conditional wait). signal(semaphore *S) { S->value++; if (S->value <= 0) { remove a process P from S->list; wakeup(P); } }
Image of page 16

Subscribe to view the full document.

Synchronization 17 33 Semaphores (cont’d) ! We must guarantee that wait() and signal() are atomic. " No two processes can execute wait() and signal() operations on the same semaphore at the same time. ! In a single-processor environment, we can do it by inhibiting interrupts during the time of the operations are executing. " So that, instructions from different processes cannot be interleaved. " Until interrupts are re-enabled. 34 Semaphores – (cont’d) ! Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. " Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S); . . . . . . signal (S); signal (Q); signal (Q); signal (S); ! Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. ! Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process
Image of page 17
Synchronization 18 Classic Problems of Synchronization ! Bounded-Buffer Problem ! Readers and Writers Problem ! Dining-Philosophers Problem Threads 35 36 Bounded-Buffer Problem ! We assume that the pool consists of n buffers, each capable of holding one item. ! The mutex semaphore provides mutual exclusion for accesses to the buffer pool. " Initialized to the value 1. ! The empty and full semaphores count the number of empty and full buffers. " The semaphore empty is initialized to the value n . " The semaphore full is initialized to the value 0.
Image of page 18

Subscribe to view the full document.

Synchronization 19 37 Bounded Buffer Problem (cont’d) ! The structure of producer process: ! The structure of the consumer process: do { ... // produce an item in nextp ... wait(empty); wait(mutex); // add nextp to buffer signal(mutex); signal(full); } while (TRUE); do { wait(full); wait(mutex); // remove an item from buffer // to nextc signal(mutex); signal(empty); // consume the item in nextc } while (TRUE); 38 Readers and Writers Problem ! A database is to be shared among several concurrent processes. " Some processes may want only to read the database – readers . " Others may want to update (to read and write) – writers . ! If two readers access the shared data simultaneously, no adverse affects will result. ! However, if a writer and some other thread (either a reader or a writer) access the database simultaneously, chaos may ensue. ! We require that the writers have exclusive access to the shared database.
Image of page 19
Synchronization 20 39 Readers and Writers Problem (cont’d) !
Image of page 20

Subscribe to view the full document.

Image of page 21
You've reached the end of this preview.
  • Spring '12
  • GwangS.Jung
  • producer, monitor

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern