gpt4 book ai didi

c - mq_open 给出 "too many open files"

转载 作者:行者123 更新时间:2023-12-04 04:38:46 37 4
gpt4 key购买 nike

我使用以下代码创建了一个消息队列。前几次它工作正常。

int main()
{
mqd_t mqdes;
char mq_name[10] = "/mq";
int oflag = O_CREAT | O_RDWR, ret;
struct mq_attr attr;

attr.mq_maxmsg = 1024;
attr.mq_msgsize = 2048;

mqdes = mq_open(mq_name, oflag, 0766, &attr);
if(mqdes == -1) {
perror("mq_open");
if(errno == EMFILE)
perror("EMFILE");
exit(1);
}

printf("mqueue created, mq_descriptor: %d\n", mqdes);

ret = mq_close(mqdes);
if(ret == -1) {
perror("mq_close");
exit(2);
}
printf(" mq closed successful\n");


return 0;
}

之后报如下错误

mq_open: Too many open files
EMFILE: Too many open files

但为什么会出现此错误?我如何才能看到诸如 ipcs 之类的 possix 消息队列适用于系统 V?

最佳答案

尝试设置资源限制:

#include <sys/resource.h>

struct rlimit rlim;
memset(&rlim, 0, sizeof(rlim));
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;

setrlimit(RLIMIT_MSGQUEUE, &rlim);

关于c - mq_open 给出 "too many open files",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250492/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com