Linux inter-process communication - message queue application example

    A message queue is a linked list of messages, including the Posix message queue system V message queue. Processes with sufficient permissions can add messages to the queue, and processes granted read permissions can read messages from the queue. The message queue overcomes the shortcomings of the signal carrying a small amount of information, the pipeline can only carry unformatted byte streams, and the buffer size is limited. Below are the two test modules

receiving module

The code of msgreceive.c is as follows

/*=============================================================================
#     FileName: msgreceive.c
#         Desc: receice message from message queue
#       Author: Licaibiao
#      Version:  
#   LastChange: 2017-01-20
#      History:
=============================================================================*/

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/msg.h>

struct msg_st
{
	long int msg_type;
	char text[128];
};

intmain()
{
	int running = 1;
	int msgid = -1;
	int len ​​= 0;
	long int msgtype = 5;
	struct msg_st data;
		
	msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
	if(-1 == msgid )
	{
		fprintf(stderr, "msgget failed with error: %d\n", errno);
		exit(EXIT_FAILURE);
	}
	
	while(running)
	{
		memset(&data.text, 0, 128);
		len = msgrcv(msgid, (void*)&data, 128, msgtype, 0);
		if(-1 == len)
		{
			fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
			exit(EXIT_FAILURE);
		}
		printf("You wrote: %s\n",data.text);

		if(0 == strncmp(data.text, "end", 3))
			running = 0;
	}

	//remove message queue
	if(-1 == msgctl(msgid, IPC_RMID, 0))
	{
		fprintf(stderr, "msgctl(IPC_RMID) failed\n");
		exit(EXIT_FAILURE);
	}
	exit(EXIT_SUCCESS);
}



sending module

/*=============================================================================
#     FileName: msgsend.c
#         Desc: send data to message queue
#       Author: Licaibiao
#      Version:
#   LastChange: 2017-01-20
#      History:
=============================================================================*/

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#include <errno.h>

#define MAX_TEXT 512
struct msg_st
{
	long int msg_type;
	char text[MAX_TEXT];
};

intmain()
{
	int running = 1;
	struct msg_st data;
	char buffer[BUFSIZ];
	int msgid = -1;
	int len;

	msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
	if(msgid == -1)
	{
		fprintf(stderr, "msgget failed with error: %d\n", errno);
		exit(EXIT_FAILURE);
	}

	while(running)
	{
		
		printf("Enter data : ");
		fgets(buffer, BUFSIZ, stdin);
		data.msg_type = 5;    
		strcpy(data.text, buffer);
	    len = strlen(data.text);	
		if(msgsnd(msgid, (void*)&data, len-1, 0) == -1)
		{
			fprintf(stderr, "msgsnd failed\n");
			exit(EXIT_FAILURE);
		}
		
		if(strncmp(buffer, "end", 3) == 0)
			running = 0;
		usleep(100000);
	}
	exit(EXIT_SUCCESS);
}

The compilation and execution results are as follows:

root@ubuntu:/home/test/msg_test# gcc msgsend.c  -o send
root@ubuntu:/home/test/msg_test# gcc msgreceive.c -o recv
root@ubuntu:/home/test/msg_test# ./recv &
[1] 106594
root@ubuntu:/home/ysj000/quanzhiSDK/F25_test/test/msg_test# ./send
Enter data : hello
You wrote: hello
Enter data : test
You wrote: test
Enter data : hello world
You wrote: hello world
Enter data : end
You wrote: end
[1]+  Done                    ./recv
root@ubuntu:/home/test/msg_test#
    It should be noted here that if msg_type in the message queue type of the receiver is set to 0, it means that all messages are received. If non-zero, only receive messages of the same type as the sender. For example, if the receiver is set to 3, then the receiver only receives messages sent by the sender whose msg_type is 3.

Compared with named pipes, the advantages of message queues are:

1. Message queues can also exist independently of sending and receiving processes, thereby eliminating the difficulties that may arise when synchronizing the opening and closing of named pipes.

2. At the same time, the synchronization and blocking problems of named pipes can be avoided by sending messages, and the process itself does not need to provide synchronization methods.

3. The receiving program can selectively receive data by message type, instead of receiving it by default like in named pipes.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324756463&siteId=291194637