我试图使用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;
}
在 Linux 内核版本为 2.6 上运行测试会成功并打印“OK!”,但在 3.8 或 3.9 内核上失败。
出了什么问题? 谢谢!