我尝试使用open()设置O_CLOEXEC标志并且没有成功.
考虑以下microtest:
#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;
}
Run Code Online (Sandbox Code Playgroud)
当在具有内核版本2.6的Linux上运行时,测试成功并打印"OK!",但是使用3.8或3.9内核失败.
怎么了?谢谢!