Outline - PowerPoint PPT Presentation

About This Presentation
Title:

Outline

Description:

Outline Announcements High Level Synchronization Constructs Simultaneous semaphores Monitors Conditional variables Inter-process Communication – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 56
Provided by: Xiu95
Learn more at: http://www.cs.fsu.edu
Category:
Tags: outline | psim

less

Transcript and Presenter's Notes

Title: Outline


1
Outline
  • Announcements
  • High Level Synchronization Constructs
  • Simultaneous semaphores
  • Monitors
  • Conditional variables
  • Inter-process Communication

2
Announcements
  • 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

3
Announcements
  • 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

4
The Dining Philosophers
5
First 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
6
Nesting 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)
7
Simultaneous 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

8
Simultaneous Semaphores
9
A 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)
10
Monitors
  • 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)
  • ........

11
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.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.

12
Monitors cont.
13
Conditional Variables in UNIX
  • POSIX conditional variables
  • pthread_cond_wait
  • pthread_cond_signal
  • Solaris conditional variables
  • cond_wait
  • cond_signal

14
Bounded 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

15
Bounded 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

16
Using 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 )

17
Readers-writers through monitor
18
Example 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
19
Example Readers Writers
  • Deadlock can happen

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
20
Readers-writers through monitor cont.
21
Readers-writers through monitor cont.
22
Traffic Synchronization
  • One-way tunnel
  • Can only use tunnel if no oncoming traffic
  • OK to use tunnel if traffic is already flowing
    the right way

23
Traffic Synchronization
24
Dining-philosopher Monitor
25
Dining-philosopher Monitor cont.
26
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

27
Inter-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

28
Inter Process Communication cont.
29
Inter-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

30
Using Messages to Share Information
31
Communication 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

32
Message Queues
  • The operating system buffers incoming messages in
    a mailbox

33
Message Queues cont.
34
Communication 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

35
Send and receive actions
36
Message Related System Calls in UNIX
  • msgget
  • Get a message queue
  • msgsnd
  • Message send operation
  • msgrcv
  • Message receive operation
  • msgctl
  • Message control operations

37
Example of message passing paths
38
Mutual exclusion pattern
39
Two-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

40
Mutual exclusion issues
  • Similar to a solution based on semaphores
  • The solution is simple
  • The solution generalizes to N processes
  • Processes must follow the protocol

41
Process signaling
42
Two-process rendezvous
43
Two-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 )

44
Many-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 )

45
Three-process rendezvous
46
Events
  • 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

47
UNIX 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

48
UNIX Signal cont.
49
UNIX Signal cont.
50
Remote 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

51
Conventional Procedure Call
  1. Parameter passing in a local procedure call the
    stack before the call to read
  2. The stack while the called procedure is active

52
Client and Server Stubs
  • Principle of RPC between a client and server
    program.

53
Remote Procedure Calls
54
Remote Procedure Calls cont.
55
Summary
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com