linux共享内存实验


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

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

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

表1 msgget()函数语法要点

#include <sys/ipc.h>
#include <sys/shm.h>

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

表2 msgsnd()函数语法要点

#include <sys/ipc.h>
#include <sys/shm.h>
struct msgbuf
{
  long mtype; /* 消息类型,该结构必须从这个域开始 */
  char mtext[1]; /* 消息正文 */
}

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

表3 msgrcv()函数语法要点

#include <sys/ipc.h>
#include <sys/shm.h>

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

表4 msgctl()函数语法要点

#include <sys/ipc.h>
#include <sys/shm.h>

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

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

    #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);
    }

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

    #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”则两个进程都将结束。

    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应用程序开发》

    更多嵌入式电子书查看华清远见电子书频道>>

相关内容