INTERPROCESS COMMUNICATION
INTRODUCTION
Topics covered
Pipes
popen and pclose functions
Coprocesses
FIFO’s
System V IPC
Messege queues
Semaphores
Shared memory
Pipes
2 limitations :
Half duplex
Can be used only between processes that have a common ancestor
Function prototype
#include<unistd.h>
int pipe(int filedes[2]);
returns : 0 if ok , -1 on error
Using 2 pipes for parent and child synchronisation
popen and pclose functions
Function prototype :
#include <stdio.h>
FILE *popen(const char *cmdstring , const char *type);
returns : file pointer if ok , NULL on error
int pclose (FILE *fp);
returns : termination status of cmdstring , or -1 on error
Coprocesses
Unix filter – program that reads from standard input and writes to
standard output
Filter – coprocess when the same program generates its input and
reads its output
Coprocess – background shell , standard input and standard output
are connected to another program using a pipe
two 1 way pipes to the other process
Sample filter to add 2 numbers
Filter to add 2 numbers using standard I/O
FIFO’s
FIFO’s – named pipes
Unrelated processes can exchange data
Function prototype
#include <sys/types.h>
#include < sys/stat.h>
int mkfifo (const char *pathname , mode_t mode);
returns : 0 of ok , -1 on error
When FIFO is opened the nonblocking flag affects what happens
Normal case – open for read only blocks until some other process
opens the FIFO for writing . Write only blocks untill some other
process opens FIFO for reading
O – NONBLOCK – specified – open for read only returns immediately
Uses of FIFO
Used by shell commands to pass data from 1 shell pipeline to another
Used in client server application to pass data between the clients and
the server
Client – server communication using a FIFO
System V IPC
Identifiers and keys
IPC structure – kernel referred by – nonnegative integer identifier
IPC structure created and removed – identifier associated with that
structure continually increases
IPC structure created – key specified
IPC private key – guarantees that a server creates a new IPC
structure
3 get functions – msgget , semget , shmget – key and integer flag
Permission structure
Struct ipc_perm
{
uid_t uid;
gid_t gid;
uid_t cuid;
gid_t cgid;
mode_t mode;
ulong seq;
key_t key;
};
Advantages and disadvantages
They are system wide and do not have a reference count
Cant access them and modify their properties with the functions
Cant use multiplexed I/O functions
Name space used is an advantage
Reliable , flow controlled , record oriented – message queue
Message Queues
They are linked list of messages stored within the kernel and
identified by the message queue identifier
New queue created or an existing queue opened – msgget
New messages – added to the end of the queue by msgsnd
Every message has a positive long integer type field , a nonnegative
length , and the actual data bytes all of which are specified to
msgsnd when the message is added to the queue
Semaphores
It is a counter used to provide access to a shared data object for
multiple processes
To obtain a shared resource , a process needs to do the following
test the semaphore that controls the resource
if the value of the semaphore is positive , the process can use the
resource
if the value of the semaphore is 0 , the process goes to sleep
until the semaphore value is greater than 0
E2BIG – value of nsops is too big
EACCESS – operation is denied to the caller
ERMID – semaphore identifier semid has been removed from the
system
EINTR – semop was interrupted by a signal
EINVAL – value of semid is invalid
Shared Memory
Allows 2 or more processes to share a given region of
memory
Fastest form of IPC
Synchronizing access to a given region using multiple
processes