openwrt消息通知方式linux,需要增加OpenWRT中IPC消息队列的缓冲区

我正在学习如何使用消息队列,并且在使用它们时遇到了一些困难.我正在使用两个完全独立的应用程序进行测试-一个是“发送方”,另一个是“接收方”.

当我运行发送器时,它将15个字符串发送到管道,但是随后失败并出现“资源暂时不可用”错误.我只需要在接收方使用消息,但是为什么只有15条消息呢?我可能正在发送很多消息,所以我想将其增加到更大的数量,例如1000左右.

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

发件人代码如下所示:

#include

#include

#include

#include

#include

#include

#include

#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

#include

#include

#include

#include

#include

#include

#include

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值