Professional Documents
Culture Documents
OS - Chapter # 9
OS - Chapter # 9
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
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
Sockets
Remote Procedure Calls
Remote Method Invocation (Java)
Sockets