【问题标题】:Need to increase buffer for IPC message queue in OpenWRTOpenWRT中需要为IPC消息队列增加缓冲区
【发布时间】:2013-05-28 06:57:51
【问题描述】:

我只是在学习如何使用消息队列,但对它们有点困难。我正在使用两个完全独立的应用程序进行测试——一个是“发送者”,另一个是“接收者”。

当我运行发件人时,它会向管道发送 15 个字符串,但随后失败并出现“资源暂时不可用”错误。我只需要在接收方消费消息,但为什么只有 15 条消息?我可能会发送大量消息,因此我想将其增加到更大的数量,例如 1000 条左右。

我尝试将消息队列大小设置为 32767,因此我期望至少为 31,但显然msg_qbytes 与可以缓冲的消息数量无关。

发件人代码如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>

#define MESSAGE_SIZE 1024

typedef struct msgbuf
{
    long mtype;
    char mtext[MESSAGE_SIZE];
};

int main(int argc, char *argv[]) {
    int msgid;
    int ret;
    struct msqid_ds msg_settings;
    long key;
        struct msgbuf msg;

    key = strtol(argv[1], NULL, 10);
    // print the message queue ID for reading via msgrcv
    printf( "Getting message queue with key = %ld\n", key);
    usleep( 1000000);

    msgid = msgget( (key_t)key, 0666 | IPC_CREAT);

    if (msgid == -1) {
        perror("msgget failed with error");
        exit(EXIT_FAILURE);
    }

    // read in current queue settings and then set the new
    // queue size.
    ret = msgctl(msgid, IPC_STAT, &msg_settings);
    msg_settings.msg_qbytes = 32767;
    msgctl( msgid, IPC_SET, &msg_settings);

    while( 1) {
        msg.mtype = 1; // we'll always leave this as 1
        memset( &(msg.mtext), 0, MESSAGE_SIZE);
        sprintf( msg.mtext, "hi");
        printf( "Sending data: %s\n", msg.mtext);
        ret = msgsnd( 1, &msg, MESSAGE_SIZE, IPC_NOWAIT);
        usleep( 500000);
        if( ret == -1) {
            perror( "msgsnd failed\n");
    }

    printf( "leaving...\n");

    return EXIT_SUCCESS;
}

接收器代码如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define MESSAGE_SIZE 1024

typedef struct msgbuf
{
    long mtype;
    char mtext[MESSAGE_SIZE];
};

int main(int argc, char *argv[]) {
    long int msgtyp = 1;
    int ret;
    size_t msgsz;
    struct msgbuf mymsg;

    int msgid;
    msgid = strtol(argv[1], NULL, 10);
    printf( "Reading message queue with ID = %d\n", msgid);
    usleep( 1000000);
    while( 1) {
        msgsz = (size_t)MESSAGE_SIZE;
        ret = msgrcv( msgid, &mymsg, msgsz, msgtyp, IPC_NOWAIT);
        if( ret == ENOMSG) {
            usleep( 100000);
            continue;
        }

        if( ret == -1) {
            perror( "msgrcv failed");
        } else {
            printf( "Read data: %s", mymsg.mtext);
            }

        usleep( 100000);
    }
    return EXIT_SUCCESS;
}

【问题讨论】:

    标签: c linux openwrt sysv-ipc


    【解决方案1】:

    终于找到资料了:http://wiki.openwrt.org/doc/uci/system

    您只需要修改 /etc/config/system 并添加 `option buffersize 65535'。不幸的是,你不能超过 64k。

    我做出了改变,它肯定更好,但并不完美。我将减少消息大小以尝试容纳更多消息。

    【讨论】:

      【解决方案2】:

      我已经解决的部分原始问题:

      On the receiver side, I always get a "Argument list too long" error.
      I changed my code to just use a #define so that I'm (hopefully) not
      making any errors when passing the size as an argument.  But when I
      looked online for solutions to the E2BIG error, it says that the
      size of the message is too large.  How can that be?
      

      好的,我未能正确解释此信息:

      来自http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/apis/ipcmsgrc.htm

      [E2BIG]
      
      Argument list too long.
      
      The size in bytes of the message is greater than msgsz and the MSG_NOERROR flag is not set in the msgflg parameter.
      

      我认为 MSG_NOERROR 是可选的。估计不是。问题 #2 解决了!只需要以某种方式增加队列中的消息数量......然后我可能会遇到另一个障碍。 :)

      【讨论】:

        【解决方案3】:

        http://publib.boulder.ibm.com/infocenter/cicsts/v3r1/index.jsp?topic=%2Fcom.ibm.cics.uc.doc%2Fcucunx%2Fmqlinux.html

        这描述了linux上的sysctl.conf文件,其他O/S会有其他机制来做同样的事情。

        【讨论】:

        • 之前看到类似的信息,尝试从OpenWRT查询当前值,但是好像没有这样配置。
        猜你喜欢
        • 2012-09-19
        • 2021-03-12
        • 2012-10-20
        • 2014-11-21
        • 2011-11-04
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        • 2021-06-24
        相关资源
        最近更新 更多