zoukankan      html  css  js  c++  java
  • fcntl的closeonexec标志

    每个文件描述符都有一个close-on-exec标志。默认情况下,这个标志最后一位被设置为 0。这个标志符的具体作用在于当开辟其他进程调用exec()族函数时,在调用exec函数之前为exec族函数释放对应的文件描述符。


    我们来看一下具体的实例。这是两个程序:

    1. //file:fcntl
    2. int main()
    3. {
    4. pid_t pid;
    5. fd = open("test.txt",O_RDWR|O_APPEND);
    6. if (fd == -1)
    7. ##printf("open err/n");
    8. printf("fd = %d",fd);
    9. printf("fork!/n");
    10. fcntl(fd, F_SETFD, 1);
    11. char *s="ooooooooooooooooooo";
    12. pid = fork();
    13. if(pid == 0)
    14. execl("ass", "./ass", &fd, NULL);
    15. wait(NULL);
    16. write(fd,s,strlen(s));
    17. close(fd);
    18. return 0;
    19. }
    20. //ass 源代码
    21. int main(int argc, char *argv[])
    22. {
    23. int fd;
    24. printf("argc = %d ",argc);
    25. fd = *argv[1];
    26. printf("fd = %d",fd);
    27. char *s = "zzzzzzzzzzzzzzzzzzz";
    28. write(fd, (void *)s, strlen(s));
    29. close(fd);
    30. return 0;
    31. }

    PS:那个test.txt提前已经创建 为空文件~

    fcntl(fd, F_SETFD, 1) 此句将fd的close-on-exec 标志设置为1,开启此标志。那么当子进程调用execl函数时,execl执行ass,ass是不能向fd内写入的,因为在调用execl函数之前系统已经讲子进程的此文件描述符关闭了。(attention:这里是子进程!)
    但是如果将 fcntl(fd, F_SETFD, 1)改为fcntl(fd, F_SETFD, 0),或者直接将此句注释掉,那么,ass便可以向这个文件描述符中任意添写东西了~~

    PS:如果将fcntl设置为开启,即设置为1,那么,此文件描述符依然是可以被主进程操作的。

    下面将程序执行的结过给大家:
    当执行此句fcntl(fd, F_SETFD, 1)
    fd = 3fork!
    argc = 2 fd = 3
    test.txt中的内容为:
    ooooooooooooooooooo

    当将fcntl(fd, F_SETFD, 1)注释掉或者将 1改为 0时
    结果:
    fd = 3fork!
    argc = 2 fd = 3lost
    test.txt中的内容为:
    zzzzzzzzzzzzzzzzzzoooooooooooooooooooooo

  • 相关阅读:
    LeetCode 226. Invert Binary Tree
    LeetCode 221. Maximal Square
    LeetCode 217. Contains Duplicate
    LeetCode 206. Reverse Linked List
    LeetCode 213. House Robber II
    LeetCode 198. House Robber
    LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
    LeetCode 171. Excel Sheet Column Number
    LeetCode 169. Majority Element
    运维工程师常见面试题
  • 原文地址:https://www.cnblogs.com/xiayong123/p/3717492.html
Copyright © 2011-2022 走看看