Outline
This presentation is the property of its rightful owner.
Sponsored Links
1 / 37

Outline PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on
  • Presentation posted in: General

Outline. Basic Synchronization Principles - continued Review Bounded-Buffer problem Readers-writers problem Dining-philosophers problem Semaphore implementations Issues with semaphores Monitors. - Please pick up Homework #2 from the front. Bounded-Buffer using Semaphores.

Download Presentation

Outline

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Outline

Outline

  • Basic Synchronization Principles - continued

    • Review

      • Bounded-Buffer problem

      • Readers-writers problem

    • Dining-philosophers problem

    • Semaphore implementations

    • Issues with semaphores

    • Monitors

- Please pick up Homework #2 from the front


Bounded buffer using semaphores

Bounded-Buffer using Semaphores

COP4610


Bounded buffer using semaphores cont

Bounded-Buffer using Semaphores – cont.

COP4610


Bounded buffer using semaphores cont1

Bounded-Buffer using Semaphores – cont.

COP4610


The readers writers problem

The Readers-Writers Problem

  • A resource is shared among readers and writers

    • A reader process can share the resource with any other reader process but not with any writer process

    • A writer process requires exclusive access to the resource whenever it acquires any access to the resource

COP4610


First and second policy

First and Second Policy

COP4610


Readers writers with active readers cont

Readers-writers with active readers – cont.

COP4610


Readers writers with an active writer cont

Readers-writers with an active writer – cont.

COP4610


Readers writers with an active writer cont1

Readers-writers with an active writer – cont.

COP4610


Dining philosophers problem

Dining-Philosophers Problem

  • Shared data

    varchopstick: array [0..4] ofsemaphore;(=1 initially)

COP4610


Dining philosophers problem cont

Dining-Philosophers Problem - cont.

  • Philosopher i:

    repeat

    wait(chopstick[i])

    wait(chopstick[i+1 mod 5])

    eat

    signal(chopstick[i]);

    signal(chopstick[i+1 mod 5]);

    think

    untilfalse;

COP4610


Semaphore implementation

Semaphore Implementation

COP4610


Semaphore implementation cont

Semaphore Implementation – cont.

  • Binary semaphore through test-and-set instruction

COP4610


Semaphore implementation cont1

Semaphore Implementation – cont.

COP4610


Semaphore implementation cont2

Semaphore Implementation – cont.

  • Semaphores implemented using interrupt disabling and test-and-set require busy waiting

    • This type of semaphores is often called spinlock

COP4610


Semaphore implementation cont3

Semaphore Implementation – cont.

  • Define a semaphore as a structure

    typedef struct {

    int value;

    queue L;

    } semaphore;

  • Assume two simple operations:

    • block suspends the process that invokes it.

    • wakeup(P) resumes the execution of a blocked process P.

COP4610


Semaphore implementation cont4

Semaphore Implementation - cont.

  • Semaphore operations now defined as

    P(S):S.value = S.value– 1;

    ifS.value < 0

    then begin

    add this process to S.L;block;

    end;

COP4610


Semaphore implementation cont5

Semaphore Implementation - cont.

V(S):

S.value = S.value + 1;

ifS.value  0

then begin

remove a process P from S.L;wakeup(P);

end;

COP4610


Semaphore implementation cont6

Semaphore Implementation - cont.

  • Semaphores are resources in the above implementation

COP4610


Constraints of acceptable solutions

Constraints of Acceptable Solutions

  • An acceptable solution to the critical section problem needs to meet the following constraints

    • Mutual exclusion

    • Progress: If a critical section is free, a set of processes are trying to enter the critical section, only those processes participate in the selection of the next process to enter the critical section and the selection cannot be postponed indefinitely

    • Bounded waiting: After a process requests entry into its critical section, only a bounded number of other processes may be allowed to enter their related critical sections before the original process enters its critical section

COP4610


Issues using semaphores

Issues Using Semaphores

  • 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

    P0P1

    P(S);P(Q);

    P(Q);P(S);

     

    V(S);V(Q);

    V(Q)V(S);

  • Starvation – indefinite blocking

    • A process may never be removed from the semaphore queue in which it is suspended.

COP4610


Active and passive semaphores

Active and Passive Semaphores

  • There is a subtle problem related to semaphore implementation

    • Bounded waiting may not be satisfied

      • when the passive V operation is used where the implementation increments the semaphore with no opportunity for a context switch

COP4610


Active and passive semaphores cont

Active and Passive Semaphores – cont.

COP4610


Active and passive semaphores cont1

Active and Passive Semaphores – cont.

  • Active semaphores

    • In contrast to passive semaphores, a yield or similar procedure will be called after incrementing the semaphore for a context switch in a V operation implementation

COP4610


Monitors

Monitors

  • High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes.

    classmonitor {

    variable declarations

    semaphore mutex = 1;

    public P1 :(…)

    { P(mutex);

    ........

    V(mutex);

    };

    ........

    }

COP4610


Monitors cont

Monitors – cont.

  • To allow a process to wait within the monitor, a condition variable must be declared, as

    condition x, y;

  • Condition variable can only be used with the operations wait and signal.

    • The operation

      x.wait;means that the process invoking this operation is suspended until another process invokes

      x.signal;

    • The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect.

COP4610


Bounded buffer monitor

Bounded buffer monitor

monitor BoundedBufferType {private: BufferItem * buffer; int NumberOfBuffers; int next_in, nextout; int current_size; condition NotEmpty, NotFull;public: BoundedBufferType( int size ) { buffers = new BufferItem[size]; NumberOfBuffers = size; next_in = 0; next_out = 0;

current_size = 0; }

COP4610


Bounded buffer monitor cont

Bounded buffer monitor – cont.

void Put( BufferItem item ) { if( current_size == NumberOfBuffers ) wait( NotFull ); buffer[next_in] = item; next_in = (next_in+1) % NumberOfBuffers; if( ++current_size == 1 ) signal( NotEmpty ); } BufferItem Get( void ) { if( current_size == 0 ) wait( NotEmpty ); BufferItem item = buffer[next_out]; next_out = (next_out+1) % NumberOfBuffers; if( --current_size == NumberOfBuffers-1 ) signal( NotFull ); return item; }}

COP4610


Using a bounded buffer monitor

Using a bounded buffer monitor

BoundedBufferType BoundedBuffer;int main() { // the Producer while( 1 ) { BufferItem item = ProduceItem(); BoundedBuffer.Put( item ); }}int main() { // the Consumer while( 1 ) { BufferItem item = BoundedBuffer.Get(); ConsumeItem( item ); }}

COP4610


Readers writers through monitor

Readers-writers through monitor

COP4610


Readers writers through monitor cont

Readers-writers through monitor – cont.

COP4610


Readers writers through monitor cont1

Readers-writers through monitor – cont.

COP4610


Traffic synchronization

Traffic Synchronization

COP4610


Dining philosopher monitor

Dining-philosopher Monitor

COP4610


Dining philosopher monitor cont

Dining-philosopher Monitor – cont.

COP4610


Thread synchronization in java

Thread Synchronization in Java

  • Synchronized

    • Only one synchronized method for a particular object can be called

    • Each object contains a monitor, which is automatically part of an object

COP4610


Summary

Summary

  • Processes that share data need to be synchronized

    • Otherwise, a race condition may exist

  • Semaphores are the basic mechanism underlying synchronization

    • can be used to solve process synchronization problems

    • need to be implemented carefully

  • Monitor is an abstract data type

    • For mutual exclusion

COP4610


  • Login