mq_unlink的限制是多少?
问题描述:
Documentation for mq_unlink说mq_unlink的限制是多少?
ENAMETOOLONG 名太长。
但这是什么限制?我认为这是NAME_MAX
,但事实并非如此。下面的代码永远运行(只要有内存,我猜)。
#include <mqueue.h>
#include <string>
#include <errno.h>
#include <unistd.h>
int main(void)
{
std::string tooLong = "long";
do
{
usleep(10);
tooLong.append("longer");
mq_unlink(tooLong.c_str());
}
while(errno != ENAMETOOLONG);
}
那么什么是极限?这个函数何时返回ENAMETOOLONG
?
答
谢谢,你说得对。
问题是缺少斜线!
#include <mqueue.h>
#include <string>
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <limits.h>
int main(void)
{
std::string tooLong = "/long";
do
{
usleep(10);
tooLong.append("longer");
mq_unlink(tooLong.c_str());
}
while(errno != ENAMETOOLONG);
std::cout << tooLong.length() << " " << tooLong << std::endl;
}
这个工作和长度是257,这正是NAME_MAX正上方。
+0
“问题是缺少斜杠!“,不,问题是你没有看到'mq_unlink()'的错误值。你可以避免这个问题,你有很好的做法,请阅读[一本C++](https://*.com/questions/388242/the-definitive-c-book-guide-and-list?noredirect=1&lq= 1) – Stargateur
您应该在假定出现错误之前检查函数的返回值。它也似乎是你的名字可能是不正确的格式。 http://man7.org/linux/man-pages/man7/mq_overview.7.html –
就我可以测试的情况而言,当字符串变为257个字符长时,即比NAME_MAX255更长时,它停止。 – ilkkachu