Message queue for inter-process communication under Linux

1. Message queue
    1) Create a message queue
        prototype:
            int msgget(key_t key, int msgflg);
        Role:
            Create or open a message queue.
        Parameters:
            key: Set the key value of the queue, the message queue sender and receiver use the same value.
            msgflg: Can do or operation with IPC_CREAT, indicating that a message queue is created when the message queue named by key does not exist.
                    If the message queue named by key exists, the IPC_CREAT flag will be ignored and only an identifier will be returned.
        Return value:
            The message queue ID value is returned on success, -1 on failure.
        
        
    2) Set the message attribute
        prototype:
            int msgctl(int msqid, int cmd, struct msqid_ds *buf);
        Function:
            Set the message attribute
        attribute:
            msqid: msgget return value.
            cmd: is the action to be taken, it can take 3 values,
                    IPC_STAT: Set the data in the msgid_ds structure to the current associated value of the message queue, that is, overwrite the value of msgid_ds with the current associated value of the message queue.
                    IPC_SET: If the process has sufficient permissions, set the current associated value of the message queue to the value given in the msgid_ds structure
                    IPC_RMID: Delete the message queue
            buf: is a pointer to the msgid_ds structure, which points to the structure of the message queue mode and access rights . The msgid_ds structure includes at least the following members:
                    struct msgid_ds  
                    {  
                        uid_t shm_perm.uid;  
                        uid_t shm_perm.gid;  
                        mode_t shm_perm.mode;  
                    };
        Return value:
            Returns 0 for success, -1 for failure.            
        
        
    3) Send message
        prototype:
            int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
        Function:
            Send message value.
        Properties:
            msqid: msgget return value.
            msgp: is a pointer to a message ready to send
            msgsz: is the length of the message pointed to by msgp
            msgflg: is used to control what will happen when the current message queue is full or the queue message reaches the system-wide limit
        Return value:
            0 for success, 0 for failure -1.
            
        
    4) Receive message
        prototype:
            ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
        Function:
            Receive message value
        attribute:
            msqid: msgget return value.
            msgp: pointer to the
            received message msgsz: length of the received message
            msgtyp: A simple receive priority can be implemented. If msgtype is 0, get the first message in the queue.
                    If its value is greater than zero, the first message with the same message type will be fetched.
                    If it is less than zero, get the first message whose type is equal to or less than the absolute value of msgtype.
            msgflg: Used to control what happens when there are no messages of the corresponding type in the queue to receive.
        Return value:
            0 for success, -1 for failure.
            
2. Example:

Send message process:

#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
using namespace std;

struct mymsg
{
    //long type;
    char data[512];
};

intmain()
{
    key_t key = 12345;
    int msgid = msgget(key, 0666|IPC_CREAT);

    char data[512];
    mymsg msg;
    //msg.type = 1;
    while(1)
    {
        cin >> data;
        memcpy(msg.data, data, 512);

        msgsnd(msgid, &msg, 512, 0);
    }

    // cancel the message queue
    msgctl(msgid, IPC_RMID, 0);
    
    return 0;
}


            

Receive message process:

#include <iostream>
#include <string.h>
#include <syspes.h>
#include <sys/ipc.h>
#include <sys/msg.h>
using namespace std;

struct mymsg
{
    //long type;
    char data[512];
};

intmain()
{
    key_t key = 12345;
    int msgid = msgget(key, 0666|IPC_CREAT);

    mymsg msg;
    //msg.type = 1;
    //memcpy(msg.data, "hello", sizeof("hello"));
    while(1)
    {
        memset(&msg, 0, sizeof(msg));
        msgrcv(msgid, &msg, 512, 0, 0);
        cout << "data = " << msg.data << endl;
    }

  
    // cancel the message queue
    msgctl(msgid, IPC_RMID, 0);

  return 0;
}



Reference document:
http://blog.csdn.net/ljianhui/article/details/10287879






Guess you like

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