Would a forked child always get same File ID as parent's file ID?











up vote
0
down vote

favorite












This might be a naive question, but given in parent open call returned me File ID 4.



Is it guaranteed that child process will always get a File ID of 4 as well ?










share|improve this question









New contributor




x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Is the parent calling open before or after fork?
    – JigglyNaga
    2 days ago










  • Yep parent is the one that opens the file and then calls fork
    – x1b
    yesterday















up vote
0
down vote

favorite












This might be a naive question, but given in parent open call returned me File ID 4.



Is it guaranteed that child process will always get a File ID of 4 as well ?










share|improve this question









New contributor




x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Is the parent calling open before or after fork?
    – JigglyNaga
    2 days ago










  • Yep parent is the one that opens the file and then calls fork
    – x1b
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











This might be a naive question, but given in parent open call returned me File ID 4.



Is it guaranteed that child process will always get a File ID of 4 as well ?










share|improve this question









New contributor




x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











This might be a naive question, but given in parent open call returned me File ID 4.



Is it guaranteed that child process will always get a File ID of 4 as well ?







files fork






share|improve this question









New contributor




x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









aruna

113




113






New contributor




x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









x1b

31




31




New contributor




x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






x1b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    Is the parent calling open before or after fork?
    – JigglyNaga
    2 days ago










  • Yep parent is the one that opens the file and then calls fork
    – x1b
    yesterday














  • 2




    Is the parent calling open before or after fork?
    – JigglyNaga
    2 days ago










  • Yep parent is the one that opens the file and then calls fork
    – x1b
    yesterday








2




2




Is the parent calling open before or after fork?
– JigglyNaga
2 days ago




Is the parent calling open before or after fork?
– JigglyNaga
2 days ago












Yep parent is the one that opens the file and then calls fork
– x1b
yesterday




Yep parent is the one that opens the file and then calls fork
– x1b
yesterday










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Yes all open file IDs are copied to the child, when you fork.



See man fork




The child inherits copies of the parent's set of open file descriptors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).







share|improve this answer






























    up vote
    1
    down vote













    A successful open call returns the lowest number as a file descriptor that is not already in use.



    So if open returns 4, it means that file descriptors 0-3 are already in use. As a fork gives the same file descriptors to the child, the child would also have fd 0-3 in use and fd 4 free, so two successful open calls in the parent and in the child immediately after a fork would return the same number. But if parent or child have opened or closed files after the fork, the numbers may be different.



    In general, you should not rely on predicting the fd of an open call, unless you have a good reason and you are very certain that you can predict it correctly.






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      x1b is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487145%2fwould-a-forked-child-always-get-same-file-id-as-parents-file-id%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Yes all open file IDs are copied to the child, when you fork.



      See man fork




      The child inherits copies of the parent's set of open file descriptors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).







      share|improve this answer



























        up vote
        1
        down vote



        accepted










        Yes all open file IDs are copied to the child, when you fork.



        See man fork




        The child inherits copies of the parent's set of open file descriptors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).







        share|improve this answer

























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Yes all open file IDs are copied to the child, when you fork.



          See man fork




          The child inherits copies of the parent's set of open file descriptors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).







          share|improve this answer














          Yes all open file IDs are copied to the child, when you fork.



          See man fork




          The child inherits copies of the parent's set of open file descriptors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          ctrl-alt-delor

          10.4k41955




          10.4k41955
























              up vote
              1
              down vote













              A successful open call returns the lowest number as a file descriptor that is not already in use.



              So if open returns 4, it means that file descriptors 0-3 are already in use. As a fork gives the same file descriptors to the child, the child would also have fd 0-3 in use and fd 4 free, so two successful open calls in the parent and in the child immediately after a fork would return the same number. But if parent or child have opened or closed files after the fork, the numbers may be different.



              In general, you should not rely on predicting the fd of an open call, unless you have a good reason and you are very certain that you can predict it correctly.






              share|improve this answer

























                up vote
                1
                down vote













                A successful open call returns the lowest number as a file descriptor that is not already in use.



                So if open returns 4, it means that file descriptors 0-3 are already in use. As a fork gives the same file descriptors to the child, the child would also have fd 0-3 in use and fd 4 free, so two successful open calls in the parent and in the child immediately after a fork would return the same number. But if parent or child have opened or closed files after the fork, the numbers may be different.



                In general, you should not rely on predicting the fd of an open call, unless you have a good reason and you are very certain that you can predict it correctly.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  A successful open call returns the lowest number as a file descriptor that is not already in use.



                  So if open returns 4, it means that file descriptors 0-3 are already in use. As a fork gives the same file descriptors to the child, the child would also have fd 0-3 in use and fd 4 free, so two successful open calls in the parent and in the child immediately after a fork would return the same number. But if parent or child have opened or closed files after the fork, the numbers may be different.



                  In general, you should not rely on predicting the fd of an open call, unless you have a good reason and you are very certain that you can predict it correctly.






                  share|improve this answer












                  A successful open call returns the lowest number as a file descriptor that is not already in use.



                  So if open returns 4, it means that file descriptors 0-3 are already in use. As a fork gives the same file descriptors to the child, the child would also have fd 0-3 in use and fd 4 free, so two successful open calls in the parent and in the child immediately after a fork would return the same number. But if parent or child have opened or closed files after the fork, the numbers may be different.



                  In general, you should not rely on predicting the fd of an open call, unless you have a good reason and you are very certain that you can predict it correctly.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  RalfFriedl

                  5,2473925




                  5,2473925






















                      x1b is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      x1b is a new contributor. Be nice, and check out our Code of Conduct.













                      x1b is a new contributor. Be nice, and check out our Code of Conduct.












                      x1b is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487145%2fwould-a-forked-child-always-get-same-file-id-as-parents-file-id%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Entries order in /etc/network/interfaces

                      新発田市

                      Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)