Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

INTERPROCESS

COMMUNICATION
CHAPTER # 9 Operating Systems
Chapter 9 - Outline
 Inter-process Communication
 Semaphores
 Monitors
 Signals
 Pipes
 Message passing
 Shared memory
 Object Linking & Embedding
 Client-Server Communication
 Sockets
 Remote Procedure Calls
 Remote Method Invocation (Java)
Inter-process Communication
 Mechanism for processes to communicate and to synchronize their
actions
 Message system
 processes communicate with each other without resorting to shared
variables
 Generally 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
 Implementation of communication link
 physical (e.g., shared memory, hardware bus)
 logical (e.g., logical properties)
Semaphores
 In 1965, Dijkstra (Dutch Scientist) invented synchronize variable that takes
non negative integer variable
 A semaphore has two operations
 Wait (P)
 An atomic operation that waits for semaphore to become positive, then decrements
its by 1
 Signal (V)
 An atomic operation that increments semaphore by 1 after completing operation and
releasing semaphore
 Entry to critical sections of active processes is controlled by the P
operation and exit is controlled by V operation
 A semaphore such as S, indicates the availability of some resource;
 If it has a non-zero positive value, it is available
 If it is zero, it is unavailable, i.e. it is being accessed by another process
Semaphores
 The wait operation allows a process to access a resource (S>0) or may
cause the process to be blocked
 The signal operation signals that some process has released resource and
now it can be used by a process waiting for it
 These operations are used to bracket the critical sections of the process;
wait(S)
<critical section>
signal(S)
 Assume S is initially set to value 1 (resource is available)
 The first process to execute the wait will set S to S-1 and will enter to its critical
section
 If another process reaches its critical section, the wait now finds S set to 0 and
process is blocked
 When first process is exit from its critical section, it executes the signal, S
would set to 1
Semaphores

 For each semaphore, the system must maintain a queue of waiting


processes
 The semaphore operations are implemented as OS services
 The OS will guarantee that wait and signal are invisible
 i.e. once started they cannot be interrupted
 The semaphore with values 1 or 0 are termed as binary semaphore
 Counting semaphore may have any non-negative value
 The value indicate the available quantity of a particular resource type
Semaphores Properties
 Attractive Properties
 Machine independent
 Simple
 Work with many
 For more critical section use different semaphores
 Acquire many resources simultaneously
 Permit multiple processes into critical section at once if desirable
 Limits of semaphores
 Semaphores are not provided by hardware
 Programmers must use them correctly
 Cannot test busy without blocking
 Indefinite blocking
Semaphores – Example (5c)
 Producer Consumer problem
 A producer process generates data and store them in the next
available buffer in array
 Process consumer is removing items from buffer and processing
them
 Assume number of positions in the buffer is finite, say N
 A particular example is print routine, which is sending characters
to a printer driver for printing
 We need to protect buffer since both process can access it
 The additional problems are;
 The producer must wait for an empty space if buffer is full
 The consumer must wait for data available if buffer is empty
Semaphores : Producer Consumer Problem

Semaphore Purpose Initial value


Free Mutual exclusion for buffer access 1
Space Space available in buffer N
Data Data available in buffer 0
Producer Explanation
Produce item Application produces data item
wait(space) If buffer full, wait for space signal
wait(free) If buffer being used, wait for free signal
add item to buffer Put item in next buffer slot
signal(free) Signal that buffer no longer in use
signal(data) Signal that data has been put in buffer
Semaphores : Producer Consumer Problem (5d), (5a)

Consumer Explanation
wait(data) Wait until at least one item in buffer
wait(free) If buffer being used, wait for free signal
get item from buffer Get item from buffer
signal(free) Signal that buffer no longer in use
Signal(space) Signal that at least one space exist in buffer
Consume item Application specific processing of item

 The counting semaphore data and space are also used as counters to
monitor the occupancy of buffer
 Wait and signal operations decrement and increment these counters
 The producer’s signal (data) will increment the count of the number of
data item
Semaphores in Unix
 Unix provide facility for creation and handling of semaphores
 The semget function call is used in a program to create semaphore
variables, it creates an array of semaphores
 The semget call specifies
 The number of semaphores required
 A unique key value to identify semaphore
 Some access and control information
 The function call semctrl provides a range of semaphore maintenance
services; such as
 Examining the value of a semaphore
 Removing a semaphore set, and
 Initializing the semaphore
 The detailed processing of the semaphore is carried out by semop
function call
Dining Philosophers Problem
 Philosophers spend their
lives thinking and eating
 Don’t interact with their
neighbors, occasionally try
to pick up 2 chopsticks
(one at a time) to eat from
bowl
 Need both to eat, then
release both when done
 In the case of 5
philosophers
 Shared data
 Bowl of rice (data set)
 Semaphore chopstick [5]
Monitors
 The monitors were developed by C.A.R. Hoare in 1974
 A monitor is a facility;
 Provided by a language system, probably implemented using semaphores
 Not a service provided by operating system
 Implemented in Concurrent Pascal, Pascal-Plus, Modula-2 and Modula-3
 A monitor is like C++ class with built-in synchronization
 It requires a lock while executing a member function
 The Mesa language, developed at Xerox PARC, had a high quality monitor
implementation
 It is a higher-level synchronization primitive
 Can be use more reliably than semaphores
Monitors
 A monitor encapsulates both
 the shared data, and
 the procedures used to access and manipulate data
 The monitor data can only be accessed via the monitor’s procedures,
which is called by application processes
 Only one process can use any monitor procedure at one time
 If data is already being accessed, the request is placed in a queue
 Key properties of monitors are;
 Only one thread or process can execute monitor code at a time
 An implicit mutual exclusion or monitor lock is associated with every monitor
 The monitor lock must be acquired to call a routine of monitor
 Upon exiting the monitor, the monitor lock is released
Monitors

 We would employ a monitor to the producer-consumer


problem
 Which defines the buffer as an array of data items and have two
procedure, say
 get_item
 put_item
 The producer processes would simply execute
put_item(item)
 The consumer process would invoke get_item() to obtain any
stored data
 The monitor procedure code ensures that only one procedure
is executing at one time
 This reduces the probability of programming error
Monitors
 Monitors introduce condition variables
 Wait
 Release the monitor lock and put the process to sleep
 when the process wakes up again, try to re-acquire the monitor lock
immediately
 Signal
 Wake up one process waiting on the condition variable (produces FIFO
semantics)
 Broadcast
 Wake up all processes waiting on the condition variable
 If no one is waiting do nothing
Monitors - Characteristics

 Monitors increase the flexibility and efficiency


 Monitors used two mechanism and condition
variables
 They are not generally present in current popular
languages
 Monitors force programmer to isolate complex
synchronization in special monitor module
Signals
 Continuous testing of a variable is wasteful
 Signals are used to avoid busy waiting
 It is low level IPC mechanism
 A signal alerts a process to the occurrence of some event
 It’s a notification
 The signals are sent from
 Kernel to user processes, or
 One process to other process
 If floating point instruction cause overflow or underflow
 The hardware will notify the kernel
 Kernel sends a SIGFPE signal to erring process
 It would abort that process
 Unix supports 32 different signals
Pipes (5c)

 A mechanism in which the output of one process is


directed into input of another
 A pipe file is created by using pipe() system call
 A pipe can be accessed like an ordinary file (FIFO queue)
 The read and write operations may become blocked if
the queue is empty (on read) and full (on write)
 Pipes exist only as along as the processes which creates
them
 When a pipe is closed, the pipe file is destroyed
Message Passing
 Message passing is a high level IPC mechanism
 Message is a piece of information that is passed from one
process to another
 Mailbox is a place where messages are stored
 Many system built based on message passing
 Examples: Mach and Windows
 Sharing of data is easy within same address space
 Message operations:
 Send
 Copy a message into a mailbox
 Receive
 Remove message out of mailbox
Shared Memory
 It enables memory segments to be shared by multiple processes
 The Unix shared memory facility requires hardware support
 The shared memory facility consist of four system calls
 shmget
 A process use it to create the shared segment in memory
 It returns a segment id
 shmat
 It is used to access shared memory
 It returns the address of shared memory, enabling a reference
 shmdt
 It detaches the shared memory from calling process
 shmctl
 It sets control parameter or destroys a shared segment
Object Linking & Embedding
 OLE is used to able the integration of application objects from
different software packages
 OLE permits the creation of compound documents that consist of
objects created and managed by different programs
 OLE consists of two mechanism, named linking and embedding
 With linking, the source document contains only a a reference to
the object; the object exists separately out with the source
document
 The reference specifies the name and location of the object file and
associated application
 With embedding, the object is actually stored as part of the data of
the source document
Client-Server Communication

 Sockets
 Remote Procedure Calls
 Remote Method Invocation (Java)
Sockets

 A socket is defined as an endpoint for


communication
 Concatenation of IP address and port
 The socket 161.25.19.8:1625 refers to port 1625 on
host 161.25.19.8
 Communication consists between a pair of sockets
Socket Communication
Remote Procedure Calls (RPC)

 In Remote procedure call (RPC) , a procedure on one machine calls


another procedure on some other machine
 When a process on machine A call a procedure on machine B
 the calling procedure on A is suspended
 procedure on B executes
 In RPC, caller and callee should be agreed upon the format of the
messages they exchange
 RPC abstracts procedure calls between processes on networked
systems
 Stubs
 client-side proxy for the actual procedure on the server
 The client-side stub locates the server and marshalls the parameters
 The server-side stub receives this message, unpacks the marshalled
parameters, and performs the procedure on the server
Execution of RPC
Steps of a Remote Procedure Call

Steps involved in doing remote computation through RPC


Remote Method Invocation (RMI)

 Remote Method Invocation (RMI) is a Java


mechanism similar to RPCs
 RMI allows a Java program on one machine to invoke
a method on a remote object
Execution of RMI
Remote Object Invocation (5d,5c,5a,5b)

Common organization of a remote object with client-side proxy

You might also like