Interprocess Communication (1)
Interprocess Communication (1)
1. Shared memory
2. Message Passing / Message Queue
3. Sockets
4. Pipes
5. FIFO (Named Pipes)
Interprocess Communication
Methods
Shared memory: Shared memory is an efficient
means of passing data between programs.
An area is created in memory by a process, which
is accessible by another process.
Processes communicate by reading and writing to
that memory space.
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.
IPC: Message Passing
Sending Process could be:
1.Sockets
2.Pipe
Communication in Client-Server Systems
Sockets
Defined as end point of communication
Pair of processes communicate over the
network user sockets- one socket for each
process
Socket is identified by an IP address which is
concatenated with port number
Server listen to client via a port, server receives
the request and complete the connection.
Communication in Client-Server Systems
Sockets
It created using socket() system call.
It takes:
Communication Domain
Socket type
Protocol to be used: as argument
Sockets
A network socket is an endpoint of an inter-
process communication flow across a computer
network.
Named or FIFO
Created by using mkfifo command
It is a permanent file.
FIFO is same to unnamed file but with a facility
that it can be accessed from anywhere.
Interprocess Communication
If a process tries to read before something is
written to the pipe, the process is suspended
until something is written.
Syntax :
Interprocess Communication
Two file descriptors are returned through the
fileds argument: fd[0] is open for reading, and
fd[1] is open for writing.
The output of filedes[1] is the input for filedes[0].
Figure : Pipe from parent to child
Example
pip[0] - the read end of the pipe - is a file
descriptor used to read from the pipe
pip[1] - the write end of the pipe - is a file
descriptor used to write to the pipe
When the process started with popen has finished, you can
close the file stream associated with it using pclose.
The pclose call will return only when the process started
with popen finishes.
If it’s still running when pclose is called, the pclose call will
wait for the process to finish.
Popen and pclose
If type is "r", the file pointer is connected to the
standard output of cmd string. (i.e read into)
Arguments :
1. command that will be executed by the child process.
2. The type of communication by the parent, i.e. whether
the parent is going to read from the pipe or write into the
pipe.
Return : It returns a file descriptor that gets created as a
result of call to the pipe.
rd=popen("ls","r");
FIFOs (Named Pipes)
FIFOs are sometimes called named pipes.
There are two fifos one of the server and the other of the
client, then the client can send request to the server on the
server fifo which the server will read and respond back
with the reply on the client's fifo.
Un-named Pipe with popen() and pclose()
1. Write into a pipe
Syntax: File pointer = popen(“process”, “mode”);
rd=popen("wc -c","w");
fwrite(buffer,sizeof(char),strlen(buffer),rd);