Back to Blog

Linux Inter-Process Communication via Message Queues

#Linux#System#Struct#DataStructure#Communication#Null

Message Queues:

Benefits of using message queues: You can attach specific message types to messages. Message queues are used for inter-process communication on the same computer.

Related system functions:

       #include <sys/types.h>

       #include <sys/ipc.h>

       key_t ftok(const char *pathname, int proj_id);

This function generates an ID based on a file name. When the system establishes IPC communication (message queues, semaphores, and shared memory), an ID value must be specified.

       #include <sys/types.h>

       #include <sys/ipc.h>

       #include <sys/msg.h>

       int msgget(key_t key, int msgflg);

This function creates a new message queue or retrieves an existing one.

       #include <sys/types.h>

       #include <sys/ipc.h>

       #include <sys/msg.h>

       int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

This function sends a message to the message queue. Here, msqid is the message queue ID, msgp is a user-defined message data type, msgsz is the size of the message payload, and msgflg is the message flag.

      ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,

                      int msgflg);

msqid is the message queue ID; msgp is a pointer to the message data structure; msgsz is the length of the message data.

type == 0: Returns the first message in the queue.

type > 0: Returns the first message in the queue with the message type equal to type.

type < 0: Returns the first message in the queue with a message type less than or equal to the absolute value of type.

The value of msgflg can be set to IPC_NOWAIT or 0. When set to IPC_NOWAIT, msgrcv returns immediately if the queue is empty. When set to 0, msgrcv blocks until a matching message arrives or the message queue is removed.

send.c function

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

struct my_msg{

long type;

char mtext[512];

};

int main()

{

        struct my_msg my_msg;

        int msg_id;

        key_t key;

        key = ftok("/usr/local", 1);

        if(key == -1)

                key = 1234;

        msg_id = msgget(key, IPC_CREAT|0600);

        if(msg_id == -1)

        {

                perror("error msgget\n");

                return -1;

        }

        my_msg.type = 1;

        memset(my_msg.mtext, 0, sizeof(my_msg.mtext));

        strcpy(my_msg.mtext, "write someting about my_msg.type=1");

        if(msgsnd(msg_id, (void *)&my_msg, (size_t)strlen(my_msg.mtext), IPC_NOWAIT))

        {

                perror("error msgsnd\n");

                return  -1;

        }

        system("ipcs -q");

        sleep(20);

        msgctl(msg_id, IPC_RMID, NULL);

        return 0;

}

 

recv.c function

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

struct my_msg{

long type;

char mtext[512];

};

int main()

{

        struct my_msg my_msg;

        int msg_id;

        key_t key;

        int len;

        key = ftok("/usr/local", 1);

        if(key == -1)

                key = 1234;

        msg_id = msgget(key, IPC_CREAT|0600);

        if(msg_id == -1)

        {

                perror("msgget error\n");

                return -1;

        }

        len = msgrcv(msg_id, (void *)&my_msg, sizeof(my_msg.mtext), 0, 0);

        if(len == -1)

        {

                perror("error msgrcv\n");

        }

        printf("type is %ld \nmtext is %s\n", my_msg.type, my_msg.mtext);

        system("ipcs -q");

        sleep(20);

        system("ipcs -q");

        return 0;

}

This article was originally published on Linux公社 (www.linuxidc.com)
Original link: http://www.linuxidc.com/Linux/2011-09/42429.htm