当前位置: 首页 > 工具软件 > Open-MQ > 使用案例 >

mq_open 创建失败

咸浩初
2023-12-01

#include <stdio.h>
#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <mqueue.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/resource.h>

#define DEFAULT_MSG_MODE (0600)
#define MQ_PRIO_MAX     32768
typedef struct rb_node {
    unsigned long  __rb_parent_color;
    struct rb_node *rb_right;
    struct rb_node *rb_left;
}rb_node;

struct list_head {
    struct list_head *next, *prev;
};

struct posix_msg_tree_node {
    struct rb_node      rb_node;
    struct list_head    msg_list;
    int         priority;
};

struct msg_msgseg {
    struct msg_msgseg *next;
    /* the next part of the message follows immediately */
};

struct msg_msg {
    struct list_head m_list;
    long m_type;
    size_t m_ts;        /* message text size */
    struct msg_msgseg *next;
    void *security;
    /* the actual message follows immediately */
};


int main()
{
    struct rlimit limit;
    getrlimit(RLIMIT_MSGQUEUE, &limit);
    printf("rlim_cur=%d, rlim_max=%d\n", limit.rlim_cur, limit.rlim_max);

    int i = 1;
    char name[100] = {0};
    int sumall = 0;
    int bytes = 0;

    while(1)
    {
        struct mq_attr mqa = {0};
        snprintf(name, sizeof(name), "/name%d", i++);
        mqa.mq_maxmsg = 10;
        mqa.mq_msgsize = 6000;

        int ilen = 0;
        if (mqa.mq_maxmsg > MQ_PRIO_MAX){ ilen = MQ_PRIO_MAX; }
        else { ilen = mqa.mq_maxmsg;}

        bytes = bytes + mqa.mq_maxmsg * sizeof(struct msg_msg) 
            + ilen * sizeof(struct posix_msg_tree_node)+ mqa.mq_maxmsg * mqa.mq_msgsize;

        sumall = sumall + mqa.mq_maxmsg * mqa.mq_msgsize;
        printf("use :%d\t sumall:%d\t",mqa.mq_maxmsg * mqa.mq_msgsize, sumall);
        int mqt = mq_open(name, O_CREAT | O_RDWR |  O_NONBLOCK, DEFAULT_MSG_MODE, &mqa);
        printf("bytes = %d\ti = %d\tname:%s\tmqt:%d\n", bytes, i, name, mqt);
        if (mqt < 0)
        {
            printf("mqt:%d\terrno:%d\tstrerror:%s\n", mqt, errno, strerror(errno));
            break;
        }
    }

    while(1)
    {
        sleep(2);
    }

    return 0;
}

 类似资料: