wait(): release monitor lock on current object; put thread to
sleep.
notify(): wake up one process waiting on the condition; this
process will try to reacquire the monitor lock.
notifyall(): wake up all processes waiting on the condition; each
process will try to reacquire the monitor lock. (Of course, only one at a timewill acquire the lock.)
Show how wait and notify solve the semaphore
implementation problem. Mention that they can be used to implement anyscheduling mechanism at all. How do wait and notify compare to P and V?
Do the readers' and writers' problem with monitors.
Summary:
Not present in very many languages (yet), but extremely useful.
Java is making monitors much more popular and well known.
Semaphores use a single structure for both exclusion and
scheduling, monitors use different structures for each.
A mechanism similar to wait/notify is used internally to Unix for
scheduling OS processes.
Monitors are more than just a synchronization mechanism. Basing an
operating system on them is an important decision about the structure of theentire system.
Message passing
Up until now, discussion has been about communication
using shared data. Messages provide for communication without shared data. Oneprocess or the other owns the data, never two at the same time.
Message = a piece of information that is passed from
one process to another.
Mailbox = a place where messages are stored between
the time they are sent and the time they are received.
Operations:
Send: place a message in a mailbox. If the mailbox is full, wait
until there is enough space in the mailbox.
Receive: remove a message from a mailbox. If the mailbox is empty,
then wait until a message is placed in it.
There are two general styles of message
communication:
1-way: messages flow in a single direction (Unix pipes, or
producer/consumer):
2-way: messages flow in circles (remote procedure call, or
client/server):
Producer&consumer example:
Producer
Consumer
int buffer1[1000];while (1) {-- prepare buffer1 --mbox.send(&buffer1);};
int buffer2[1000];while (1) {mbox.receive(&buffer2);--
process buffer2 --};
Note that buffer recycling is implicit, whereas it
was explicit in the semaphore implementation.
Client&Server example:
Client
Server
int buffer1[1000];mbox1.send("readrutabaga");mbox2.receive(&buffer);
int buffer2[1000];intcommand[1000];mbox1.receive(&command);-- decode command ---- read file into
buffer2 --mbox2.send(&buffer2);
Note that this looks a lot like a procedure
call&return. Explain the various analogs between procedure calls and message
operations:
Parameters:
Result:
Name of procedure:
Return address:
Why use messages?
Many kinds of applications fit into the model of processing a
sequential flow of information, including all of the Unix filters.
The component parties can be totally separate, except for the
mailbox:
Less error-prone, because no
invisible side effects: no process has access to another's memory.
They might not trust each other (OS vs. user).
They
might have been written at different times by different programmers who knewnothing about each other.
They might be running on different
processors on a network, so procedure calls are out of the question.