mq_unlink的限制是多少

What is the limit in mq_unlink?

本文关键字:多少 unlink mq      更新时间:2023-10-16

mq_unlink的文档说

搪瓷太龙 名字太长了。

但是这个限制是什么?我以为是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。