Title: Outline
1Outline
- Announcements
- High Level Synchronization Constructs
- Simultaneous semaphores
- Monitors
- Conditional variables
- Inter-process Communication
2Announcements
- The schedule for this week and next week
- There will be no class this Thursday
- Remember that you may need to demonstrate your
program during the class time - This Wednesday I will give a make-up lecture
- On Oct. 21 we will have a review
- Homework 3 is due at the beginning of class so
that solutions will be made available during
class - Note that no late submission will be accepted for
Homework 3 - On Oct. 23 we will have the midterm
- The midterm covers chapters 1-9
3Announcements
- About the second quiz
- Most of you did well
- However, some of you did poorly
- Please make sure that you put efforts now if we
want to earn a good / passing grade - A preview of the last programming assignment
4The Dining Philosophers
5First Try Solution
philosopher(int i) while(TRUE) //
Think // Eat P(forki)
P(fork(i1) mod 5) eat()
V(fork(i1) mod 5) V(forki)
semaphore fork5 (1,1,1,1,1) fork(philosop
her, 1, 0) fork(philosopher, 1,
1) fork(philosopher, 1, 2) fork(philosopher, 1,
3) fork(philosopher, 1, 4)
Deadlock
6Nesting Semaphore Operations
pr P Operation Order ps P Operation
Order P(mutex1) P(mutex2) P(mutex2)
P(mutex1) ltaccess R1gt ltaccess
R1gt ltaccess R2gt ltaccess R2gt
V(mutex2) V(mutex1) V(mutex1)
V(mutex2) (a) (b)
7Simultaneous Semaphores
- The orders of P operations on semaphores are
critical - Otherwise deadlocks are possible
- Simultaneous semaphores
- Psimultaneous(S1, ...., Sn)
- The process gets all the semaphores or none of
them
8Simultaneous Semaphores
9A Solution
philosopher(int i) while(TRUE) //
Think // Eat Psim(forki,fork(i1)
mod 5) eat() Vsim(forki,fork(i1
) mod 5) semaphore fork5
(1,1,1,1,1) fork(philosopher, 1,
0) fork(philosopher, 1, 1) fork(philosopher, 1,
2) fork(philosopher, 1, 3) fork(philosopher, 1,
4)
10Monitors
- High-level synchronization construct that allows
the safe sharing of an abstract data type among
concurrent processes. - class monitor
- variable declarations
- semaphore mutex 1
- public P1 ()
- P(mutex)
- ........
- V(mutex)
-
- ........
-
11Monitors 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.waitmeans 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.
12Monitors cont.
13Conditional Variables in UNIX
- POSIX conditional variables
- pthread_cond_wait
- pthread_cond_signal
- Solaris conditional variables
- cond_wait
- cond_signal
14Bounded buffer monitor
- monitor BoundedBufferType private
BufferItem buffer int NumberOfBuffers int
next_in, nextout int current_size condition
NotEmpty, NotFullpublic BoundedBufferType(
int size ) buffers new BufferItemsize
NumberOfBuffers size next_in 0
next_out 0 - current_size 0
15Bounded buffer monitor cont.
- void Put( BufferItem item ) if(
current_size NumberOfBuffers ) wait(
NotFull ) buffernext_in item
next_in (next_in1) NumberOfBuffers if(
current_size 1 ) signal( NotEmpty )
BufferItem Get( void ) if( current_size
0 ) wait( NotEmpty ) BufferItem
item buffernext_out next_out
(next_out1) NumberOfBuffers if(
--current_size NumberOfBuffers-1 )
signal( NotFull ) return item
16Using a bounded buffer monitor
- BoundedBufferType BoundedBufferint main()
// the Producer while( 1 ) BufferItem
item ProduceItem() BoundedBuffer.Put( item
) int main() // the Consumer while( 1
) BufferItem item BoundedBuffer.Get()
ConsumeItem( item )
17Readers-writers through monitor
18Example Readers Writers
monitor readerWriter_1 int numberOfReaders
0 int numberOfWriters 0 boolean busy
FALSE public startRead()
while(numberOfWriters ! 0)
numberOfReaders finishRead()
numberOfReaders-
startWrite() numberOfWriters
while( busy
(numberOfReaders gt 0) ) busy
TRUE finishWrite()
numberOfWriters-- busy FALSE
19Example Readers Writers
monitor readerWriter_1 int numberOfReaders
0 int numberOfWriters 0 boolean busy
FALSE public startRead()
while(numberOfWriters ! 0)
numberOfReaders finishRead()
numberOfReaders--
startWrite() numberOfWriters
while( busy
(numberOfReaders gt 0) ) busy
TRUE finishWrite()
numberOfWriters-- busy FALSE
20Readers-writers through monitor cont.
21Readers-writers through monitor cont.
22Traffic Synchronization
- One-way tunnel
- Can only use tunnel if no oncoming traffic
- OK to use tunnel if traffic is already flowing
the right way
23Traffic Synchronization
24Dining-philosopher Monitor
25Dining-philosopher Monitor cont.
26Thread 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
27Inter-Process Communication
- Inter-Process Communication (IPC)
- Allow running processes to communicate with each
other - IPC special cases
- Parent and child
- Parent passes arguments to child
- Parent collect returned status from child using
wait - Processes with common ancestors
- pipe
28Inter Process Communication cont.
29Inter-Process Communication cont.
- Three styles of inter-process communication
- Communication through messages
- Through named pipes (using mknod system call)
- Like reading and writing files
- Through shared memory
- Among threads
- Among processes
- Remote procedure call (RPC)
- Between client and server
30Using Messages to Share Information
31Communication through Messages
- Messages and message queues
- The most common method
- Send messages to message queues
- Receive messages from message queues
- Message system
- processes communicate with each other without
resorting to shared variables
32Message Queues
- The operating system buffers incoming messages in
a mailbox
33Message Queues cont.
34Communication through Messages cont.
- IPC facility provides two operations
- send(message) message size fixed or variable
- receive(message)
- If P and Q wish to communicate, they need to
- establish a communication link between them
- exchange messages via send/receive
35Send and receive actions
36Message Related System Calls in UNIX
- msgget
- Get a message queue
- msgsnd
- Message send operation
- msgrcv
- Message receive operation
- msgctl
- Message control operations
37Example of message passing paths
38Mutual exclusion pattern
39Two-process mutual exclusion
- // Convenience procedurevoid WaitForEmptyMsg(
int msg_queue ) int msgMsgSize
ReceiveMessage( msg_queue, msg )void main(int
argc,char argv) //Process A or B int
mutex_queue AttachMessageQueue("/usr/queue/F
Mutex") // Start with one message in the queue
(the ticket) if( IAmProcessA() ) SendMsgTo(
mutex_queue ) while( 1 )
DoOtherThings() // Not using file F // Enter
critical section by getting message
WaitForEmptyMsg( mutex_queue ) UseFileF()
SendMsgTo( mutex_queue ) // Leave critical
section by returning message
40Mutual exclusion issues
- Similar to a solution based on semaphores
- The solution is simple
- The solution generalizes to N processes
- Processes must follow the protocol
41Process signaling
42Two-process rendezvous
43Two-process rendezvous
- void main( int argc, char argv ) // Game
Player A int b_queue AttachMessageQueue("/usr/
queue/gpb") SendMsgTo( b_queue, ReadyToStart
) int a_queue AttachMessageQueue("/usr/queue/
gpa") WaitForEmptyMsg( a_queue )void
main( int argc, char argv ) // Game
Player B int a_queue AttachMessageQueue("/usr/
queue/gpa") SendMsgTo( a_queue, ReadyToStart
) int b_queue AttachMessageQueue("/usr/queue/
gpb") WaitForEmptyMsg( b_queue )
44Many-process rendezvous
- void main(int argc, char argv ) //
Coordinator int msgMsgSize int
playerNumberOfPlayers, coordinator_queue
AttachMessageQueue( "/usr/queue/coordq" ) for(
i 0 i lt NumberOfPlayers i )
ReceiveMessage( coordinator_queue, msg )
playeri msg1 for( i 0 i lt
NumberOfPlayers i ) SendMsgTo( playeri,
BeginPlaying )void main( int argc, char
argv ) // Player I int coordinator_queue
AttachMessageQueue( "/usr/queue/coordq" )
char qname32 sprintf( qname,
"/usr/queue/s", argv1 ) int my_queue
AttachMessageQueue( qname ) SendMsgTo(coordinat
or_queue,ReadyToStart,my_queue)
WaitForEmptyMsg( my_queue )
45Three-process rendezvous
46Events
- Events can be used to coordinate processes
- An event represents the occurrence of some
condition - A process can synchronize with an event by
blocking itself until the event occurs - When the event occurs, the OS informs the blocked
process of the occurrence
47UNIX Signal
- A signal in UNIX is a mechanism by which the OS
can inform a process of the occurrence of some
event - A signal can be raised by one process using kill
system call, thus causing another process to be
interrupted and to optionally catch the signal - Signals can also be used among user processes
48UNIX Signal cont.
49UNIX Signal cont.
50Remote Procedure Call
- RPC is designed to hide all the details from
programmers - Overcome the difficulties with message-passing
model - It extends the conventional local procedure calls
to calling procedures on remote computers
51Conventional Procedure Call
- Parameter passing in a local procedure call the
stack before the call to read - The stack while the called procedure is active
52Client and Server Stubs
- Principle of RPC between a client and server
program.
53Remote Procedure Calls
54Remote Procedure Calls cont.
55Summary
- Monitors are an abstract data type
- Can be used to share data safely
- Inter-process communications
- Through mailboxes
- Through messages
- Through signals (especially in UNIX)