如何解决无法解决此问题:错误 [EINVAL 无效参数] mq_open
我正在从书中示例代码的示例中学习消息队列: https://github.com/bradfa/tlpi-dist/blob/master/pmsg/pmsg_create.c
但是,在编译代码并运行之后
./pmsg_create -c aaa
我得到了
ERROR [EINVAL Invalid argument] mq_open
使用 sudo 运行它也不能解决问题。我试图从命令行用不同的参数指定 maxmsg 和 msgsize 参数,仍然是同样的错误。
我过去曾在 stackoverflow 中四处寻找问题,但似乎没有一个能解决我的问题。我在 ubuntu 20.04 上运行它。贴出下面的代码。
#include <mqueue.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h"
static void
usageError(const char *progName)
{
fprintf(stderr,"Usage: %s [-cx] [-m maxmsg] [-s msgsize] mq-name "
"[octal-perms]\n",progName);
fprintf(stderr," -c Create queue (O_CREAT)\n");
fprintf(stderr," -m maxmsg Set maximum # of messages\n");
fprintf(stderr," -s msgsize Set maximum message size\n");
fprintf(stderr," -x Create exclusively (O_EXCL)\n");
exit(EXIT_FAILURE);
}
int
main(int argc,char *argv[])
{
int flags,opt;
mode_t perms;
mqd_t mqd;
struct mq_attr attr,*attrp;
/* If 'attrp' is NULL,mq_open() uses default attributes. If an
option specifying a message queue attribute is supplied on the
command line,we save the attribute in 'attr' and set 'attrp'
pointing to 'attr'. We assign some (arbitrary) default values
to the fields of 'attr' in case the user specifies the value
for one of the queue attributes,but not the other. */
attrp = NULL;
attr.mq_maxmsg = 10;
attr.mq_msgsize = 2048;
flags = O_RDWR;
/* Parse command-line options */
while ((opt = getopt(argc,argv,"cm:s:x")) != -1) {
switch (opt) {
case 'c':
flags |= O_CREAT;
break;
case 'm':
attr.mq_maxmsg = atoi(optarg);
attrp = &attr;
break;
case 's':
attr.mq_msgsize = atoi(optarg);
attrp = &attr;
break;
case 'x':
flags |= O_EXCL;
break;
default:
usageError(argv[0]);
}
}
if (optind >= argc)
usageError(argv[0]);
perms = (argc <= optind + 1) ? (S_IRUSR | S_IWUSR) :
getInt(argv[optind + 1],GN_BASE_8,"octal-perms");
printf("optind = %d,argv[%d] = %s,flags = %x,perms = %o\n",optind,argv[optind],flags,perms);
mqd = mq_open(argv[optind],perms,attrp);
if (mqd == (mqd_t) -1)
errExit("mq_open");
exit(EXIT_SUCCESS);
}
解决方法
如果您阅读了 mq_open
(here's an online copy) 的联机帮助页,它会说:
EINVAL 名称不遵循 mq_overview(7) 中的格式。
mq_format(7) 表示名称需要以斜杠 (/
) 开头,而 aaa
不需要。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。