Semaphores, cont

How does the Operating System implement semaphores? Like the process table, it disables interrupts when manipulating the semaphore table.

Look at Fig. 2-11 in text. Why the mutex semaphore? Because there could be multiple producers and consumers and the buffer size is greater than one.

Mutual Exclusion using Monitors

Some programming languages, such as Concurrent Pascal, Modula-2 and Java provide mutual exclusion facilities called monitors.

They are similar to modules in languages that provide abstract data types in that:

Monitors differ in that they support guard procedures. Java programmers can use the keyword synchronized to indicate methods of a class where only one method can execute at a time.

Guard procedures (synchronized methods) have the property that:

Monitor Example

Increment example revisited

MONITOR incr;
EXPORT IncrementX, ReadX;

int x;     /* variable shared by monitor procedures */

GUARD PROCEDURE IncrementX()
{
    int Temp; /* local variable */
    Temp = x;
    Temp = Temp + 1;
    x = Temp;
}

GUARD PROCEDURE ReadX()
{
    return(x);
}

INITIALIZE  /* initialize variable */
{
    x = 0;
}

Also look at Java example.

Monitors are a higher-level, making parallel programming less-error prone than with semaphores.

Note, however, that they are implemented using a lower level facility provided by the hardware or operating system (such as semaphores).

Synchronization using Monitors

As described above, monitors solve the mutual exclusion problem. Monitors use conditions to solve the synchronization problem:

Look at Fig. 2-14 as an example. Java provides wait(), notify(), and notifyAll(). Look at an example later.

Like semaphores, but no counters and do not accumulate signals. Must use own counters to keep track of states.

Problem:

Precise definitions vary in the literature. One solution:

Other primitives: event counters, sequencers, path expressions

Message Passing

System calls for direct message passing between processes

send(destpid, &message)

receive(srcpid, &message). srcpid can be ANY to receive from any destination.

Can also use indirect message passing where messages are sent to mailboxes or ports.

Design issues:

Look at Fig. 2-15.

Summary

Equivalence of primitives. Assignment is to build a message passing system on top of semaphores and shared memory.

Talked about: