c - 需要在OpenWRT中为IPC消息队列增加缓冲区

标签 c linux openwrt sysv-ipc

我刚刚开始学习如何使用消息队列,但使用起来有点困难。我使用两个完全独立的应用程序来进行测试——一个是“发送方”,另一个是“接收方”。

当我运行发送器时,它向管道发送了 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;
}

最佳答案

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

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

我进行了更改,它确实更好,但并不完美。我将缩减消息大小以尝试容纳更多消息。

关于c - 需要在OpenWRT中为IPC消息队列增加缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16874483/

相关文章:

c++ - 生成用户定义素数的相对素数?

linux - 在运行 Perl 脚本 IF 语句时遇到一个小问题。

c - 谁调用了 atexit()?

android - 客户的 Logo 作为默认启动动画

linux - 面对shell脚本中的文件夹路径问题

在 Open WRT 菜单配置 --> 语言中找不到 Python

python - 在 python 变量中获取 wget 输出

linux - 使用 socat 将串行端口连接到 tcp 时出错

c - 将多维可变长度数组传递给函数

c - 初始化 void 指针指向一个数组