Unit 6 - Interprocess Communication
Unit 6 - Interprocess Communication
Unit 6 - Interprocess Communication
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.
1. Sockets
2. Pipe
Sockets
A network socket is an endpoint of an inter-process
communication flow across a computer network.
They have been half duplex (i.e., data flows in only one
direction).
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].
Popen and pclose
The simplest way of passing data between two programs is with the
popen and pclose functions.
The command string is the name of the program to run, together with
any parameters. open_mode must be either “r” or “w”.
Popen and pclose
Flow of data is in both directions
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");