Inter Process Communication

Processes communicate with each other and with the kernel to coordinate their activities. Linux supports a number of Inter-Process Communication (IPC) mechanisms

In numerous applications there is clearly a need for these processes to communicate with each exchanging data or control information. There are a few methods which can accomplish this task. We will consider:

  • Pipes
  • Named Pipe(FIFO)
  • Message Queues
  • Semaphores
  • Shared Memory
  • Sockets


 PIPE :   It is the simplest technique to communicate between two processes.The term pipe to mean connecting a data flow from one process to another.Generally you attach, or pipe, the output of one process to the input of another. Lets us understand the pipe concept from our daily life  example, we use pipe fitting from the water tank tank to the wash-room to get water from one end i.e. water tank to the other end i.e. tap in the wash-room. So the pipe is communicating between water tank and the tap in the wash-room. Similarly, pipe in linux is used to communicate between two processes. It is a unidirectional communication i.e. at one time only from one side communication is possible only.

Note: Pipes are used for communication between related processes(parent-child-sibling) on the same linux machine.

Syntax :

Pipe is passed (a pointer to) an array of two integer file descriptors. It fills the array with two new file descriptors and returns a zero. On failure, it returns -1.

The two file descriptors returned are connected in a special way. Any data written to file_descriptor[1] can be read back from file_descriptor[0]. The data is processed in a first in, first out basis, usually abbreviated to FIFO. This means that if you write the bytes 1, 2, 3 to file_descriptor[1], reading from file_descriptor[0] will produce 1, 2, 3. This is different from a stack, which operates on a last in, first out basis, usually abbreviated to LIFO.



To create a pipe –

Program Example for PIPE communication –

Output : –

#gcc -o pipe pipe.c


Pipe Created Successfully…

Descriptors are: 3   4

Parent Process….

Enter Data To Be Written To Pipe


Pipe Written Successfully..

Child Process

Pipe Read Successfully


Flow of Code :


 Named PIPE (FIFO’s) :

Drawbacks of PIPE :

1.In PIPE communication, only intra-communication between processes is possible i.e. related processes only.

2.Scope of the PIPE is temporary i.e. only upto the program is running.

3.We have to handle file descriptors for communication between two processes.

So, the next inter process communication mechanism is Named PIPE (FIFO).

You can create named pipes from the command line and from within a program.

The preferred command-line method is to use-

From inside a program, you can use two different calls:

Note: Named Pipes(FIFO) are used for communication between related and unrelated processes on the sane LINUX machine.




File Permissions Are OK

Fifo Created Successfully…

Enter Data To Be Written..

Hello Linux

Written Successfully…

Data From Named Pipe Is:Hello Linux

Message Queue :

Drawbacks of Named-PIPE :

  1. In named pipe, communication is only possible if there is a receiver at the opposite to sender side. i.e. receiver must be present to receive message otherwise message will be lost.

So, the next inter process communication mechanism is Message Queue.

Advantages to use Message Queue :

  1. If the receiver is not present even then also the sender can send a message and the messages will be received  when the receiver is present or online we can say in FIFO manner. i.e. the message send first will be received first.
  2. Communication is very fast using message queue. Client-Serve can be implemented easily.

Message queues provide a way of sending a block of data from one process to another. Message queues provide a reasonably easy and efficient way of passing data between two unrelated processes. They have the advantage over named pipes that the message queue exists independently of both the sending and receiving processes, which removes some of the difficulties that occur in synchronizing the opening and closing of named pipes.

message queue

Message Queue Functions :

msgget :

This function is used to create a message queue and get a key to access this message queue.

Syntax :

A special bit defined by IPC_CREAT must be bitwise ORed with the permissions to create a new message queue. The IPC_CREAT flag is silently ignored if the message queue already exists. The msgget function returns a positive number, the queue identifier, on success or –1 on failure.

msgsnd :

The msgsnd function allows you to add a message to a message queue:

Syntax :

  • The first parameter, msqid, is the message queue identifier returned from a msgget function.
  • The second parameter, msg_ptr, is a pointer to the message to be sent, which must start with a long int type as described previously.
  • The third parameter, msg_sz, is the size of the message pointed to by msg_ptr. This size must not include the long int message type.
  • The fourth parameter, msgflg, controls what happens if either the current message queue is full or the systemwide limit on queued messages has been reached. If msgflg has the IPC_NOWAIT flag set, the function will return immediately without sending the message and the return value will be –1.
    If the msgflg has the IPC_NOWAIT flag clear, the sending process will be suspended, waiting for space to become available in the queue.
  • On success, the function returns 0, on failure –1. If the call is successful, a copy of the message data has been taken and placed on the message queue.

msgrcv :

The msgrcv function retrieves messages from a message queue:

Syntax :

  • The first parameter, msqid, is the message queue identifier returned from a msgget function.
  • The second parameter, msg_ptr, is a pointer to the message to be received, which must start with a long int type as described previously in the msgsnd function.
  • The third parameter, msg_sz, is the size of the message pointed to by msg_ptr, not including the long int message type.
  • The fourth parameter, msgtype, is a long int, which allows a simple form of reception priority to be implemented.


Example :

To under stand the message queue let  us take an example with a sender and a receiver.

sender.c –

Compile it –

# gcc -0 sender sender.c


receiver.c –

Output :


Explanation – When we compile the sender.c a message queue is created with key 1234 and write data to enter into queue and then compile receiver.c that has the same key 1234. The data send from the sender.c will be received from the receiver.c using mssgrcv function.

Disadvantage of Message Queue :-

  1. The only disadvantage of message queue is that we can send data only of 1024 bytes. But in shared memory the more size of data can be transfer between two processes and that depends on the physical memory of the system.

So to overcome this problem we use shared memory concept.

Shared Memory :

In this concept two unrelated processes can access the same logical memory. Shared memory is a very efficient way of transferring data between two running processes. Shared memory is a special range of addresses that is created by IPC for one process and appears in the address space of that process. Other processes can then “attach” the same shared memory segment into their own address space. All processes can access the memory locations just as if the memory had been allocated by malloc. If one process writes to the shared memory, the changes immediately become visible to any other process that has access to the same shared memory.

shared memry

Functions for shared memory

shmget :

This function is defined in sys/shm.h header file and used to create shared memory.

Syntax :

It returns a shared memory identifier and flay used is IPC_PRIVATE that creates a private shared memory to the processes and IPC_CREAT flag to create memory. The second argument is size which tells the amount of memory required for processes in bytes.

shmat :

To enable access to the memory that is created we have to attach it to the address space of a process, so this is done with the help of shmat function.

Syntax :


The shmdt function detaches the shared memory from the current process. It takes a pointer to the address returned by shmat. On success, it returns 0, on error –1.


First at the sender side we will create a logical memory by using shmget function of size say 1024 bytes.

sender.c –



Here the shared memory is created to communication between two processes and the memory address is returned by a pointer. Second process is say receiver.c that will receive the data from that shared memory.

receiver.c –



Disadvantage of Shared Memory : 

By itself, shared memory doesn’t provide any synchronization facilities. So you usually need to use some other mechanism to synchronize access to the shared memory. Typically, you might use shared memory to provide efficient access to large areas of memory and pass small messages to synchronize access to that memory. There are no automatic facilities to prevent a second process from starting to read the shared memory before the first process has finished writing to it. It’s the responsibility of the programmer to synchronize access.


A socket is a communication mechanism that allows client/server systems to be developed either locally, on a single machine, or across networks. Linux functions such as printing, connecting to databases, and serving web pages as well as network utilities such as rlogin for remote login, http for hyper text transfer protocol and ftp for file transfer usually use sockets to communicate.
Sockets are created and used differently from pipes because they make a clear distinction between client and server. The socket mechanism can implement multiple clients attached to a single server.


Socket Connections

First, a server application creates a socket, which like a file descriptor is a resource assigned to the server process and that process alone. The server creates it using the system call socket, and it can’t be shared with other processes.

Next, the server process gives the socket a name. For example, a web server typically creates a socket on port 80, an identifier reserved for the purpose. Web browsers know to use port 80 for their HTTP connections and port 443 for HTTPS connections.

  • A socket is named using the system call bind.
  • The system call, listen, creates a queue for incoming connections.
  • The server can accept them using the system call accept.

When the server calls accept, a new socket is created that is distinct from the named socket. This new socket is used solely for communication with this particular client.

The client side of a socket-based system is more straightforward. The client creates an unnamed socket by calling socket.

Socket Attributes

Sockets are characterized by three attributes: domain, type, and protocol.

Socket Domains :Domains specify the network medium that the socket communication will use. The most common socket domain is AF_INET, which refers to Internet networking that’s used on many Linux local area networks and, of course, the Internet itself.

Protocols : Internet protocols provide two communication mechanisms with distinct levels of service: streams and datagrams.

  • Stream Sockets : Stream sockets (in some ways similar to standard input/output streams) provide a connection that is a sequenced and reliable two-way byte stream. Thus, data sent is guaranteed not to be lost, duplicated, or
    reordered without an indication that an error has occurred.
  • Datagram Sockets :   A datagram socket, specified by the type SOCK_DGRAM, doesn’t establish and maintain a connection. There is also a limit on the size of a datagram that can be sent. It’s transmitted as a single network message that may get lost, duplicated, or arrive out of sequence.

Creating a Socket

The socket system call creates a socket and returns a descriptor that can be used for accessing the socket.

Syntax :

Naming a Socket

To make a socket (as created by a call to socket) available for use by other processes, a server program needs to give the socket a name. The bind system call assigns the address specified in the parameter, address, to the unnamed socket associated with the file descriptor socket. The length of the address structure is passed as address_len.

Syntax :

Creating a Socket Queue

To accept incoming connections on a socket, a server program must create a queue to store pending requests. It does this using the listen system call.

Syntax :

A value of 5 for backlog is very common.

Accepting Connections

Once a server program has created and named a socket, it can wait for connections to be made to the socket by using the accept system call.

Syntax :

Requesting Connections

Client programs connect to servers by establishing a connection between an unnamed socket and the server listen socket. They do this by calling connect.

Syntax :

Closing a Socket

You can terminate a socket connection at the server and client by calling close, just as you would for low level file descriptors.

Example of SOCKET Communication in LINUX

Let us take a server-client example to illustrate socket communication between two processes.

  • client.c



On compiling client.c, we provide localhost as a command line argument to connect the system with localhost i.e., But the connection was refused because the server is unavaliable.

  • server.c

Output :

Now again run client .c  –


compile server.c and run simultaneously ,