需要增加OpenWRT中IPC消息队列的缓冲区
我只是在学习如何使用消息队列,在使用它们时遇到了一些困难。我使用两个完全独立的应用程序进行测试——一个是“发送方”,另一个是“接收方” 当我运行发送器时,它向管道发送15个字符串,但随后失败并出现“资源暂时不可用”错误。我只需要使用接收方的消息,但为什么只有15条消息?我可能会发送很多信息,所以我想把这个数字增加到更大的数字,比如1000左右 我试图将消息队列大小设置为32767,因此我希望至少有31个,但显然需要增加OpenWRT中IPC消息队列的缓冲区,c,linux,openwrt,sysv-ipc,C,Linux,Openwrt,Sysv Ipc,我只是在学习如何使用消息队列,在使用它们时遇到了一些困难。我使用两个完全独立的应用程序进行测试——一个是“发送方”,另一个是“接收方” 当我运行发送器时,它向管道发送15个字符串,但随后失败并出现“资源暂时不可用”错误。我只需要使用接收方的消息,但为什么只有15条消息?我可能会发送很多信息,所以我想把这个数字增加到更大的数字,比如1000左右 我试图将消息队列大小设置为32767,因此我希望至少有31个,但显然msg_qbytes与可以缓冲的消息数量无关 发件人代码如下所示: #include
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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义消息大小为1024
类型定义结构msgbuf
{
长型;
字符多行文字[消息大小];
};
int main(int argc,char*argv[]){
int-msgid;
int ret;
结构msqid_ds msg_设置;
长键;
结构msgbuf msg;
key=strtol(argv[1],NULL,10);
//打印消息队列ID以便通过msgrcv读取
printf(“获取密钥为%ld\n的消息队列”,密钥);
美国LEEP(1000000);
msgid=msgget((key_t)key,0666 | IPC_CREAT);
如果(msgid==-1){
perror(“msgget因错误而失败”);
退出(退出失败);
}
//读入当前队列设置,然后设置新队列
//队列大小。
ret=msgctl(msgid、IPC_STAT和msg_设置);
msg_settings.msg_qbytes=32767;
msgctl(msgid、IPC设置和msg设置);
而(1){
msg.mtype=1;//我们将始终将其保留为1
memset(&(msg.mtext),0,消息大小;
sprintf(msg.mtext,“你好”);
printf(“发送数据:%s\n”,msg.mtext);
ret=msgsnd(1,&msg,消息大小,IPC\U NOWAIT);
美国LEEP(500000);
如果(ret==-1){
perror(“msgsnd失败\n”);
}
printf(“离开…\n”);
返回退出成功;
}
接收器代码如下所示:
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义消息大小为1024
类型定义结构msgbuf
{
长型;
字符多行文字[消息大小];
};
int main(int argc,char*argv[]){
长整数msgtyp=1;
int ret;
大小(msgsz);;
结构msgbuf mymsg;
int-msgid;
msgid=strtol(argv[1],NULL,10);
printf(“正在读取ID为%d\n的消息队列”,msgid);
美国LEEP(1000000);
而(1){
msgsz=(大小)消息大小;
ret=msgrcv(msgid、mymsg、msgsz、msgtyp、IPC_NOWAIT);
如果(ret==ENOMG){
美国LEEP(100000);
继续;
}
如果(ret==-1){
perror(“msgrcv失败”);
}否则{
printf(“读取数据:%s”,mymsg.mtext);
}
美国LEEP(100000);
}
返回退出成功;
}
我已经解决的原始问题的一部分:
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?
好的,我没有正确解释这些信息:
发件人:
我认为MSG_NOERROR是可选的。我想不是。问题2解决了!只需要以某种方式增加队列中的消息数…然后我可能会遇到另一个障碍。:)请参见
这描述了linux上的sysctl.conf文件,其他O/S将有其他机制实现同样的功能。我终于找到了信息: 您只需修改/etc/config/system并添加“option buffersize 65535”。不幸的是,您不能超过64k
我做了更改,它肯定更好,但并不完美。我将缩减邮件大小以容纳更多邮件。我以前看到过类似的信息,并尝试从OpenWRT查询当前值,但似乎没有以这种方式配置。