Saturday, September 26, 2020

Message Queue - Basic

 Today, we see the message queues.  The advantages is available everywhere on the internet.  Summary is one process can communicate with another process.  Better picture is available in this link:

https://www.tutorialspoint.com/inter_process_communication/images/multiple_message_queue.jpg

Here we have two programs 'write.c' and 'read1.c'.  Write.c will write a message into message queue of type 1.  It will be taken by read1.c.

Write.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

struct msgbuf {
	long mtype;
	char mtext[20];
}mymsg;

int main()
{
	key_t key;
	int msgid;
	
	key=ftok("dummy.txt",65);
	msgid = msgget(key,0666 | IPC_CREAT);
	printf("Key - %x ;MessageId - %d \n",key,msgid);

	mymsg.mtype = 1;	
	strcpy(mymsg.mtext,"its my world 1 \0");
	
	msgsnd(msgid,&mymsg,strlen(mymsg.mtext),0);	
	
	printf("Sent Data\n");

	return 0;
}
  • Line 6 - This is the typical template of message used in message queues.  First long will be message type.  Rest of it is message.  It can be further defined as any data type.
  • Line 16 - We need a unique key for creating a msgq.  We use 'ftok' command that takes in two arguments. File that the process can read and any character.  Retriever process should also use the same arguments and create the key.
  • Line 17 - 'msgget' creates a message queue(because we use IPC_CREAT mask) with key from 'ftok' and some permissions.  It gives message queue identifier, just like file identifier to communicate
  • Line 20-21 - Populate the message with message type '1' and the text.
  • Line 23 - 'msgsnd' to send the message to queue to let others take it.  Arguments will be message identifier, Address of message, size of message(excluding the 'long' type)

Read1.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct msgbuf {
	long mtype;
	char mtext[20];
} mymsg;

int main()
{
	key_t key;
	int msgid;
	
	key=ftok("dummy.txt",65);
	msgid=msgget(key,0644);
	printf("Key - %x ;MessageId - %d \n",key,msgid);
	
	msgrcv(msgid,&mymsg,sizeof(mymsg),1,0);
	printf("Received Data,%s\n",mymsg.mtext);
	printf("Received Data type, %d\n",mymsg.mtype);
	
	msgctl(msgid,IPC_RMID,NULL);
	return 0;
}
  • Line 5-8 - Same as in write.c
  • Line 15 - To 'ftok' we pass the same arguments as in write.c.
  • Line 16 - 'msgget' to get message identifier.  We do not use IPC_CREAT as message queue already created by write.c
  • Line 19 - 'msgrcv' to read the message into mymsg.  4th argument is message type that we wish to read.
  • Line 23 - 'msgctl' to delete the memory of message queue.

Demonstrate

Lets take the programs for a ride

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
[root@msgq]# gcc write.c -o write
[root@msgq]# ./write
Key - 41014da0 ;MessageId - 327680
Sent Data
[root@msgq]# ipcs

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages
0x41014da0 327680     root       666        15           1

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x00000000 131072     root       600        393216     2          dest
0x00000000 262146     root       600        524288     2          dest

------ Semaphore Arrays --------
key        semid      owner      perms      nsems

[root@svtap01end1 msgq]#

We have sent the message to message queue. 'ipcs' is the linux command that shows the details of message queue. 'ipcrm -a' is one of the commands that will delete all the details of the shared memory listed here.  We haven't used it though.  In another terminal or the same terminal lets run read1.c.

1
2
3
4
5
[root@msgq]# gcc read1.c -o read1
[root@msgq]# ./read1
Key - 41014da0 ;MessageId - 294912
Received Data,its my world 1
Received Data type, 1

You see that the message 'its my world 1' is received by read1.c

No comments:

Post a Comment