歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux 進程間通信(posix消息隊列 簡單)實例

詳情見: http://www.linuxidc.com/Linux/2011-10/44828.htm

編譯:

gcc -o consumer consumer.c -lrt

gcc -o producer producer.c -lrt

/*

 *
 *       Filename:  producer.c
 *
 *    Description:  生產者進程
 *
 *        Version:  1.0
 *        Created:  09/30/2011 04:52:23 PM
 *       Revision:  none
 *       Compiler:  gcc(g++)
 *
 *         Author:  |Zhenghe Zhang|, |[email protected]|
 *        Company:  |Shenzhen XXX Technology Co., Ltd.|
 *
 */

#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>

#define MAXSIZE     10   //定義buf大小
#define BUFFER      8192 //定義Msg大小

struct MsgType{
    int len;
    char buf[MAXSIZE];
    char x;
    short y;
};

int main()
{
    /*消息隊列*/
    mqd_t msgq_id;
    struct MsgType msg;

    unsigned int prio = 1;
    unsigned int send_size = BUFFER;

    struct mq_attr msgq_attr;
    const char *file = "/posix";

    /*mq_open() for creating a new queue (using default attributes) */
    /*mq_open() 創建一個新的 POSIX 消息隊列或打開一個存在的隊列*/
    msgq_id = mq_open(file, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG, NULL);
    if(msgq_id == (mqd_t)-1)
    {
        perror("mq_open");
        exit(1);
    }

    /* getting the attributes from the queue        --  mq_getattr() */
    if(mq_getattr(msgq_id, &msgq_attr) == -1)
    {
        perror("mq_getattr");
        exit(1);
    }

    printf("Queue \"%s\":\n\t- stores at most %ld messages\n\t- \
        large at most %ld bytes each\n\t- currently holds %ld messages\n",
        file, msgq_attr.mq_maxmsg, msgq_attr.mq_msgsize, msgq_attr.mq_curmsgs);
  
    /*setting the attributes of the queue        --  mq_setattr() */
    /*mq_setattr() 設置消息隊列的屬性,設置時使用由 newattr 指針指向的 mq_attr 結構的信息。*/
    /*屬性中只有標志 mq_flasgs 裡的 O_NONBLOCK 標志可以更改,其它在 newattr 內的域都被忽略 */
    if(mq_setattr(msgq_id, &msgq_attr, NULL) == -1)
    {
        perror("mq_setattr");
        exit(1);
    }   

    int i = 0;
    while(i < 10)
    {
        msg.len = i;
        memset(msg.buf, 0, MAXSIZE);
        sprintf(msg.buf, "0x%x", i);
        msg.x = (char)(i + 'a');
        msg.y = (short)(i + 100);

        printf("msg.len = %d, msg.buf = %s, msg.x = %c, msg.y = %d\n", msg.len, msg.buf, msg.x, msg.y);

        /*sending the message      --  mq_send() */
        /*mq_send() 把 msg_ptr 指向的消息加入由 mqdes 引用的消息隊列裡。*/
        /*參數 msg_len 指定消息 msg_ptr 的長度:這個長度必須小於或等於隊列 mq_msgsize 屬性的值。零長度的消息是允許。*/
        if(mq_send(msgq_id, (char*)&msg, sizeof(struct MsgType), prio) == -1)
        {
            perror("mq_send");
            exit(1);
        }

        i++;
        sleep(1);  
    }

    sleep(30); //等待消費者進程退出

    /*closing the queue        -- mq_close() */
    /*mq_close() 關閉消息隊列描述符 mqdes。如果調用進程在消息隊列 mqdes 綁定了通知請求,*/
    /*那麼這個請求被刪除,此後其它進程就可以綁定通知請求到此消息隊列。*/
    if(mq_close(msgq_id) == -1)
    {
        perror("mq_close");
        exit(1);
    }

    /*mq_unlink() 刪除名為 name 的消息隊列。消息隊列名將被直接刪除。*/
    /*消息隊列本身在所有引用這個隊列的描述符被關閉時銷毀。*/
    if(mq_unlink(file) == -1)
    {
        perror("mq_unlink");
        exit(1);
    }

    return 0;
}

 


/*
 *
 *       Filename:  consumer.c
 *
 *    Description:  消費者進程
 *
 *        Version:  1.0
 *        Created:  09/30/2011 04:52:23 PM
 *       Revision:  none
 *       Compiler:  gcc(g++)
 *
 *         Author:  |Zhenghe Zhang|, |[email protected]|
 *        Company:  |Shenzhen XXX Technology Co., Ltd.|
 *
 */

#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>

#define MAXSIZE     10   //定義buf大小
#define BUFFER      8192 //定義Msg大小

struct MsgType{
    int len;
    char buf[MAXSIZE];
    char x;
    short y;
};

int main()
{
    /*消息隊列*/
    mqd_t msgq_id;
    struct MsgType msg;

    unsigned int sender;
    struct mq_attr msgq_attr;

    unsigned int recv_size = BUFFER;
    const char *file = "/posix";

    /* mq_open() for opening an existing queue */
    msgq_id = mq_open(file, O_RDWR);
    if(msgq_id == (mqd_t)-1)
    {
        perror("mq_open");
        exit(1);
    }

    /* getting the attributes from the queue        --  mq_getattr() */
    if(mq_getattr(msgq_id, &msgq_attr) == -1)
    {
        perror("mq_getattr");
        exit(1);
    }

    printf("Queue \"%s\":\n\t- stores at most %ld messages\n\t- \
        large at most %ld bytes each\n\t- currently holds %ld messages\n",
        file, msgq_attr.mq_maxmsg, msgq_attr.mq_msgsize, msgq_attr.mq_curmsgs);

    if(recv_size < msgq_attr.mq_msgsize)
        recv_size = msgq_attr.mq_msgsize;

    int i = 0;
    while(i < 10) //運行一個consumenr,為 10 ,同時運行兩個consumer進程,為 5 
    {
        msg.len = -1;
        memset(msg.buf, 0, MAXSIZE);
        msg.x = ' ';
        msg.y = -1;
 
        /* getting a message */

        /*mq_receive() 從由描述符 mqdes 引用的隊列時刪除優先級最高的最老的消息,並把放置到 msg_ptr 的緩存區內。*/
        /*參數 msg_len 指定緩沖區 msg_ptr 的大小:它必須大於隊列的 mq_msgsize 屬性(參數 mq_getattr)。*/
        /*如果 prio 不是 NULL,那麼它指向的內存用於返回收到消息相關的優先級。*/
        if (mq_receive(msgq_id, (char*)&msg, recv_size, &sender) == -1)
        {
            perror("mq_receive");
            exit(1);
        }

        printf("msg.len = %d, msg.buf = %s, msg.x = %c, msg.y = %d\n", msg.len, msg.buf, msg.x, msg.y);

        i++;
        sleep(2);
    }

    if(mq_close(msgq_id) == -1)
    {
        perror("mq_close");
        exit(1);
    }

    return 0;
}

Copyright © Linux教程網 All Rights Reserved