Linux – IPC


I would like to define process as portion of program under execution!

The communication between two process is mentioned as Inter Process Communication.

In Linux an obivous method of communication between process is Message Queue.

The steps involved are:

1. we should have msgbuf structure defined

struct msgbuf{

long msgtype;

char msgtext[80];


2. we should create a message queue specifing an unique id for each of our mq.

key_t unique_id;

unique_id = ftok(‘.’,’a’);

int id = msgget(unique_id, IPC_CREAT | 777 );

Now the message queue has been created and the handle for programmer is in ‘id’.

3. We can use the id for sending and recieveing messages from message queue.

Sending: msgsnd(id, (struct msgbuf *)&mq_buf,sizeof(mq_buf), 0)

Recieving/ reading from Message queue :

msgrcv(id, (struct msgbuf*)&mq_buf, 80,123, IPC_NOWAIT);

4. Remove the message queue after use.

msgctl(id, IPC_RMID, 0);

Let us think on the number ‘123’ in msgrcv function..

Message queue in Linux, usually operates in a directory level basis. The programmer can classify his message according to content or any other paramenters. This is mentioned in mq_buf.msgtype parameter. In this example the sender could have classified the message to a category ‘123’. There by the receiver trying to retrieve the message form the message queue should specify the category / message type.

Sender has to specify as mq_buf.msgtype = 123;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s