Inter-Process Communication
Manish Shrivastava
Cooperating Processes
The processes can be independent or cooperating processes.
Independent process cannot affect or be affected by the
execution of another process.
Cooperating process can affect or be affected by the execution
of another process
Advantages of process cooperation
Information sharing
Computation speed-up
Break into several subtasks and run in parallel
Modularity
Constructing the system in modular fashion.
Convenience
User will have many tasks to work in parallel
Editing, compiling, printing
Inter-process Communication
(IPC)
IPC facility provides a mechanism to allow processes
to communicate and synchronize their actions.
Processes can communicate through
shared memory
or message passing.
Both schemes may exist in OS.
The Shared-memory method requires communication
processes to share some variables.
The responsibility for providing communication rests with
the programmer.
The OS only provides shared memory.
Example: producer-consumer problem.
Communication Models
Msg Passing
Shared Memory
Producer-Consumer Problem: Shared memory
Producer process produces information that is
consumed by a consumer process.
unbounded-buffer places no practical limit on the
size of the buffer.
Producer can produce any number of items.
Consumer may have to wait
bounded-buffer assumes that there is a fixed buffer
size.
Consumer or producer may have to wait
Shared Memory
Shared memory is memory that may be simultaneously accessed
by multiple programs.
Life cycle of Shared Memory
Create Memory Block
Attach to number of process simultaneously
Read & Write
Detach Memory block
Free Memory block
Common chunk of read/write memory
among processes
MAX
Shared Memory
(unique key)
Create
ptr
Attach 0
Attach
ptr
Proc. 2
Proc. 1
ptr
Proc. 3
ptr
Proc. 4
ptr
Proc. 5
Shmget System Call
The shmget system call is used to create shared memory segment
Shmt System Call
Attaches block to process
int shmget (key_t key, int size, int shmflg);
Void * shmt(ShmID,..);
Arguments:
key_t key: key for creating or accessing shared memory
int size: size in bytes of shared memory segment to create. Use 0 for
accessing an existing segment.
int shmflg: segment creation condition and access permission.
Communicating Among Separate
Processes: 1/5
Define the structure of a shared memory
segment as follows:
//Status
#define
#define
#define
of memory block
NOT_READY (1)
FILLED
(0)
TAKEN
(1)
struct Memory {
int status; // used for Synchronization
int data[4]; // data to share
};
17
Communicating Among Separate
Processes: 2/5
The Server
Prepare for a shared memory
void main(int argc, char *argv[])
{
key_t
ShmKEY;
int
ShmID, i;
struct Memory *ShmPTR;
ShmKEY = ftok(./, x);
ShmID = shmget(ShmKEY, sizeof(struct Memory),
IPC_CREAT | 0666);
ShmPTR = (struct Memory *) shmat(ShmID, NULL, 0);
18
Communicating Among Separate
Processes: 3/5
shared memory not ready
ShmPTR>status = NOT_READY;
filling in data
for (i = 0; i < 4; i++)
ShmPTR>data[i] = atoi(argv[i]);
ShmPTR>status = FILLED;
while (ShmPTR>status != TAKEN)
sleep(1); /* sleep for 1 second */
shmdt((void *) ShmPTR);
shmctl(ShmID, IPC_RMID, NULL);
exit(0);
}
wait until the data is taken
detach and remove shared memory
19
Communicating Among Separate
Processes: 4/5
void main(void)
{
key_t
int
struct Memory
The Client
ShmKEY;
ShmID;
*ShmPTR;
prepare for shared memory
ShmKEY=ftok(./, x);
ShmID = shmget(ShmKEY, sizeof(struct Memory), 0666);
ShmPTR = (struct Memory *) shmat(ShmID, NULL, 0);
while (ShmPTR>status != FILLED)
;
printf(%d %d %d %d\n, ShmPTR>data[0],
ShmPTR>data[1], ShmPTR>data[2], ShmPTR>data[3]);
ShmPTR>status = TAKEN;
shmdt((void *) ShmPTR);
exit(0);
20
Important Notes
If you did not remove your shared memory
segments (e.g., program crashes before the
execution of shmctl() ), they will be in the
system forever. This will degrade the system
performance.
Use the Ipcs command to check if you have
shared memory segments left in the system.
Use the ipcrm command to remove your
shared memory segments.
22
Inter-process Communication (IPC):
Message Passing System
Message system processes communicate with
each other without resorting to shared variables.
If P and Q want to communicate, a communication
link exists between them.
OS provides this facility.
IPC
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)
We are concerned with logical link.
Implementation Questions
How are links established?
Can a link be associated with more than two processes?
How many links can there be between every pair of
communicating processes?
What is the capacity of a link?
Is the size of a message that the link can accommodate
fixed or variable?
Is a link unidirectional or bi-directional?
Fixed and variable message size
Fixed size message
Good for OS designer
Complex for programmer
Variable size messages
Complex for the designer
Good for programmer
Methods to implement a link
Direct or Indirect communication
Synchronous or asynchronous communication
Automatic or explicit buffering
Direct Communication
Symmetric: Processes must name each other explicitly:
send (P, message) send a message to process P
receive(Q, message) receive a message from process Q.
Asymmetric: Only sender names the recipient, the
recipient is not required to name the sender.
The send and receive primitives are as follows.
Send (P, message) send a message to process P.
Receive(id, message) receive a message from any process.
Properties of communication link
Links are established automatically.
A link is associated with exactly one pair of communicating
processes.
Between each pair there exists exactly one link.
The link may be unidirectional, but is usually bi-directional
Disadvantages: Changing a name of the process
creates problems.
Indirect Communication
The messages are sent and received from mailboxes (also
referred to as ports).
A mailbox is an object
Process can place messages
Process can remove messages.
Two processes can communicate only if they have a shared
mailbox.
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
send(A, message) send a message to mailbox A
receive(A, message) receive a message from mailbox A
Indirect Communication
Mailbox sharing
P1, P2, and P3 share mailbox A.
P1, sends; P2 and P3 receive.
Who gets a message ?
Solutions
Allow a link to be associated with at most two processes.
Allow only one process at a time to execute a receive operation.
Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.
Properties of a link:
A link is established if they have a shared mailbox
A link may be associated with more than two boxes.
Between a pair of processes they may be number of links
A link may be either unidirectional or bi-directional.
OS provides a facility
To create a mailbox
Send and receive messages through mailbox
To destroy a mail box.
The process that creates mailbox is a owner of that mailbox
The ownership and send and receive privileges can be passed to other processes
through system calls.
Synchronous or asynchronous
Message passing may be either blocking or non-
blocking.
Blocking is considered synchronous
Non-blocking is considered asynchronous
send and receive primitives may be either blocking or
non-blocking.
Blocking send: The sending process is blocked until the
message is received by the receiving process or by the mailbox.
Non-blocking send: The sending process sends the message
and resumes operation.
Blocking receive: The receiver blocks until a message is
available.
Non-blocking receive: The receiver receives either a valid
message or a null.
Automatic and explicit buffering
A link has some capacity that determines the number of messages that can
reside in it temporarily.
Queue of messages is attached to the link; implemented in one of three ways.
1. Zero capacity 0 messages
Sender must wait for receiver (rendezvous).
2. Bounded capacity finite length of n messages
-Sender must wait if link full.
3. Unbounded capacity infinite length
Sender never waits.
In non-zero capacity cases a process does not know whether a message has
arrived after the send operation.
The sender must communicate explicitly with receiver to find out whether the
later received the message.
Example: Suppose P sends a message to Q and executes only after the
message has arrived.
Process P:
send (Q. message) : send message to process Q
receive(Q,message) : Receive message from process Q
Process Q
Receive(P,message)
Send(P,ack)
Exception conditions
When a failure occurs error recovery (exception handling)
must take place.
Process termination
A sender or receiver process may terminate before a message
is processed. (may be blocked forever)
A system will terminate the other process or notify it.
Lost messages
Messages may be lost over a network
Timeouts; restarts.
Scrambled messages
Message may be scrambled on the way due to noise
The OS will retransmit the message
Error-checking codes (parity check) are used.
Client-Server Communication
Sockets
Remote Procedure Calls
Pipes
Sockets
A socket is defined as an endpoint for communication.
A pair of processes communicating over a network
employees a pair of sockets one for each process.
Socket: Concatenation of IP address and port
The socket 161.25.19.8:1625 refers to port 1625 on host
161.25.19.8
Servers implementing specific services listen to wellknown ports
telnet server listens to port 80
ftp server listens to port 21
http server listens to port 80.
The ports less than 1024 are used for standard services.
The port for socket is an arbitrary number greater
than1024.
Communication consists between a pair of sockets.
Socket Communication
Remote Procedure Calls
Remote procedure call (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
marshals the parameters.
Marshalling: Parameter marshalling involves
packaging the parameters into a form that can be
transmitted over a network.
The server-side stub receives this message,
unpacks the marshaled parameters, and
performs the procedure on the server.
Remote Procedure Calls
There are several issues
Local procedure call can fail and RPCs can fail and
re-executed
exactly once semantics: Local procedure calls may have
exactly once semantics
In RPC, the server must implement at most once
semantics send the ack to client to ensure no further
communication.
The server detects the repeated messages.
Even client sends more messages those will be ignored.
Binding issues: linking loading and execution
Fixed or rendezvous
Applications: distributed file systems
Execution of RPC
Pipes
Pipe: another IPC mechanism
uses the familiar file interface
not a special interface (like messages)
Connects an open file of one process to an open
file of another process
Often used to connect the standard output of one
process to the standard input of another process
Messages and pipes compared
More about Pipes
Acts as a conduit allowing two processes to
communicate
Issues
Is communication unidirectional or bidirectional?
In the case of two-way communication, is it half or fullduplex?
Must there exist a relationship (i.e. parent-child) between the
communicating processes?
Can the pipes be used over a network?
Ordinary Pipes
Ordinary Pipes allow communication in standard
producer-consumer style
Producer writes to one end (the write-end of the pipe)
Consumer reads from the other end (the read-end of
the pipe)
Ordinary pipes are therefore unidirectional
Require parent-child relationship between
communicating processes
Ordinary pipe can not be accessed from outside the
process that creates it.
Ordinary Pipes
Unix function to create pipes: pipe(int fd[])
fd[0] is the read end. fd[1] is write end.
Named Pipes
Named Pipes are more powerful than ordinary
pipes
Communication is bidirectional
No parent-child relationship is necessary between
the communicating processes
Several processes can use the named pipe for
communication
Provided on both UNIX and Windows systems
Pipes in Practice
ls | more