linux 消息队列

分享到:
           

    本文关键字: 消息队列,linux 消息队列

    顾名思义,消息队列就是一些消息的列表,用户可以在消息队列中添加消息和读取消息等。从这点上看,消息队列具有一定的FIFO特性,但是它可以实现消息的随机查询,比FIFO具有更大的优势。同时,这些消息又是存在于内核中的,由“队列ID”来标识。

    消息队列的实现包括创建或打开消息队列、添加消息、读取消息和控制消息队列4种操作,其中创建或打开消息队列使用的函数是msgget(),这里创建的消息队列的数量会受到系统消息队列数量的限制;添加消息使用的函数是msgsnd(),它把消息添加到已打开的消息队列末尾;读取消息使用的函数是msgrcv(),它把消息从消息队列中取走,与FIFO不同的是,这里可以取走指定的某一条消息;控制消息队列使用的函数是msgctl(),它可以完成多项功能。

    表1列举了msgget()函数的语法要点。

表1 msgget()函数语法要点

所需头文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函数原型 int msgget(key_t key, int msgflg)
函数传入值 key:消息队列的键值,多个进程可以通过它访问同一个消息队列,其中有个特殊值IPC_PRIVATE,用于创建当前进程的私有消息队列
msgflg:权限标志位
函数返回值 成功:消息队列ID
出错:-1

    表2列举了msgsnd()函数的语法要点。

表2 msgsnd()函数语法要点

所需头文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函数原型 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
函数传入值 msqid:消息队列的队列ID
msgp:指向消息结构的指针,该消息结构msgbuf通常如下。
struct msgbuf
{
  long mtype; /* 消息类型,该结构必须从这个域开始 */
  char mtext[1]; /* 消息正文 */
}
msgsz:消息正文的字节数(不包括消息类型指针变量)
msgflg IPC_NOWAIT:若消息无法立即发送(如当前消息队列已满),函数会立即返回
0:msgsnd调用阻塞直到发送成功为止
函数返回值 成功:0
出错:-1

    表3列举了msgrcv()函数的语法要点。

表3 msgrcv()函数语法要点

所需头文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函数原型 int msgrcv(int msgid, void *msgp, size_t msgsz, long int msgtyp, int msgflg)
函数传入值 msqid:消息队列的队列ID
msgp:消息缓冲区,同msgsnd()函数的msgp
msgsz:消息正文的字节数(不包括消息类型指针变量)
msgtyp 0:接收消息队列中第一个消息
大于0:接收消息队列中第一个类型为msgtyp的消息
函数传入值 小于0:接收消息队列中第一个类型值不小于msgtyp绝对值且类型值最小的消息
msgflg MSG_NOERROR:若返回的消息比msgsz字节多,则消息就会截短到msgsz字节,且不通知消息发送进程
IPC_NOWAIT:若在消息队列中并没有相应类型的消息可以接收,则函数立即返回
0:msgsnd()调用阻塞直到接收一条相应类型的消息为止
函数返回值 成功:0
出错:-1

    表4列举了msgctl()函数的语法要点。

表4 msgctl()函数语法要点

所需头文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函数原型 int msgctl (int msgqid, int cmd, struct msqid_ds *buf )
函数传入值 msqid:消息队列的队列ID
cmd:命令参数 IPC_STAT:读取消息队列的数据结构msqid_ds,并将其存储在buf指定的地址中
IPC_SET:设置消息队列的数据结构msqid_ds中的ipc_perm域(IPC操作权限描述结构)值,这个值取自buf参数
IPC_RMID:从系统内核中删除消息队列
buf:描述消息队列的msqid_ds结构类型变量
函数返回值 成功:0
出错:-1

    下面的实例体现了如何使用消息队列进行两个进程(发送端和接收端)之间的通信,包括消息队列的创建、消息发送与读取、消息队列的撤销和删除等多种操作。

    消息发送端进程和消息接收端进程间不需要额外实现进程间的同步。在该实例中,发送端发送的消息类型设置为该进程的进程号(可以取其他值),因此接收端根据消息类型来确定消息发送者的进程号。注意这里使用了fotk()函数,它可以根据不同的路径和关键字产生标准的key。消息队列发送端的代码如下:

    /* msgsnd.c */
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define BUFFER_SIZE 512


    struct message
    {
        long msg_type;
        char msg_text[BUFFER_SIZE];
    };
    int main()
    {
        int qid;
        key_t key;
        struct message msg;

        /* 根据不同的路径和关键字产生标准的key */
        if ((key = ftok(".", 'a')) == -1)
        {
            perror("ftok");
            exit(1);
        }
        /* 创建消息队列 */
        if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
        {
            perror("msgget");
            exit(1);
        }
        printf("Open queue %d\n",qid);
        while(1)
        {
            printf("Enter some message to the queue:");
            if ((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)
            {
                puts("no message");
                exit(1);
            }

            msg.msg_type = getpid();
            /* 添加消息到消息队列 */
            if ((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0)
            {
                perror("message posted");
                exit(1);
            }
            if (strncmp(msg.msg_text, "quit", 4) == 0)
            {
                break;
            }
        }
        exit(0);
    }

    消息队列接收端的代码如下:

    /* msgrcv.c */
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define BUFFER_SIZE 512

    struct message
    {
        long msg_type;
        char msg_text[BUFFER_SIZE];
    };
    int main()
    {
        int qid;
        key_t key;
        struct message msg;

        /* 根据不同的路径和关键字产生标准的key */
        if ((key = ftok(".", 'a')) == -1)
        {
            perror("ftok");
            exit(1);
        }
        /* 创建消息队列 */
        if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
        {
            perror("msgget");
            exit(1);
        }
        printf("Open queue %d\n", qid);
        do
        {
            /* 读取消息队列 */
            memset(msg.msg_text, 0, BUFFER_SIZE);
            if (msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0) < 0)
            {
                perror("msgrcv");
                exit(1);
            }
            printf("The message from process %d : %s", msg.msg_type, msg.msg_text);

        } while(strncmp(msg.msg_text, "quit", 4));
        /* 从系统内核中移走消息队列 */
        if ((msgctl(qid, IPC_RMID, NULL)) < 0)
        {
            perror("msgctl");
            exit(1);
        }
        exit(0);
    }

    以下是程序的运行结果,输入“quit”则两个进程都将结束。

    $ ./msgsnd
    Open queue 327680
    Enter some message to the queue:first message
    Enter some message to the queue:second message
    Enter some message to the queue:quit
    $ ./msgrcv
    Open queue 327680
    The message from process 6072 : first message
    The message from process 6072 : second message
    The message from process 6072 : quit

    本文选自华清远见嵌入式培训教材《从实践中学嵌入式Linux应用程序开发》

   热点链接:

   1、linux 共享内存
   2、linux下的信号量
   3、linux下的信号处理实例
   4、信号处理函数signal()和信号集函数组
   5、信号捕捉函数alarm()和pause()

更多新闻>>