小编Iva*_*mov的帖子

open()不设置O_CLOEXEC标志

我尝试使用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内核失败.

怎么了?谢谢!

c linux kernel system-calls fcntl

12
推荐指数
1
解决办法
1919
查看次数

标签 统计

c ×1

fcntl ×1

kernel ×1

linux ×1

system-calls ×1