【发布时间】:2013-08-20 19:02:55
【问题描述】:
我尝试使用 open() 设置 O_CLOEXEC 标志,但没有成功。
考虑以下微测试:
#include <stdio.h>
#include <fcntl.h>
int main() {
int fd = open("test.c", O_RDONLY | O_CLOEXEC);
int ret = fcntl(fd, F_GETFL);
if(ret & O_CLOEXEC) {
printf("OK!\n");
} else {
printf("FAIL!\n");
}
printf("fd = %d\n", fd);
printf("ret = %x, O_CLOEXEC = %x\n", ret, O_CLOEXEC);
return 0;
}
在内核版本为 2.6 的 linux 上运行时,测试成功并打印“OK!”,但在 3.8 或 3.9 内核上失败。
怎么了? 谢谢!
【问题讨论】:
-
请注意,即使在 2.6.38 支持 O_CLOEXEC 之前,该标志也可能为真
标签: c linux kernel system-calls fcntl