Is dup2 atomic with respect to writes/read

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Let's say I have a file descriptor 4 that is used for reading/writing with the O_APPEND flag for writes. If I have multiple threads writing to this file and let's say periodically I want to switch the file pointed by fd 4, I'd want to do a dup2 call to replace fd 4 with new file. Meanwhile multiple threads will still point at fd 4 for writing. Will I be losing any data or will threads who are writing face errors during the switch?







share|improve this question
























    up vote
    0
    down vote

    favorite












    Let's say I have a file descriptor 4 that is used for reading/writing with the O_APPEND flag for writes. If I have multiple threads writing to this file and let's say periodically I want to switch the file pointed by fd 4, I'd want to do a dup2 call to replace fd 4 with new file. Meanwhile multiple threads will still point at fd 4 for writing. Will I be losing any data or will threads who are writing face errors during the switch?







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Let's say I have a file descriptor 4 that is used for reading/writing with the O_APPEND flag for writes. If I have multiple threads writing to this file and let's say periodically I want to switch the file pointed by fd 4, I'd want to do a dup2 call to replace fd 4 with new file. Meanwhile multiple threads will still point at fd 4 for writing. Will I be losing any data or will threads who are writing face errors during the switch?







      share|improve this question












      Let's say I have a file descriptor 4 that is used for reading/writing with the O_APPEND flag for writes. If I have multiple threads writing to this file and let's say periodically I want to switch the file pointed by fd 4, I'd want to do a dup2 call to replace fd 4 with new file. Meanwhile multiple threads will still point at fd 4 for writing. Will I be losing any data or will threads who are writing face errors during the switch?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 27 at 19:16









      user135284

      1234




      1234




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The Linux man page and the POSIX description rather clearly say that it is, indeed, atomic:



          Linux man page on dup2():




          The steps of closing and reusing the file descriptor newfd are
          performed atomically.




          POSIX, under "Rationale":




          On the other hand, the dup2() function provides unique services, as no other interface is able to atomically replace an existing file descriptor.




          I can't see any reason it should be atomic with regard opening, closing and dup'ing the file descriptor, but not with regard to reads or writes.






          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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f427004%2fis-dup2-atomic-with-respect-to-writes-read%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            The Linux man page and the POSIX description rather clearly say that it is, indeed, atomic:



            Linux man page on dup2():




            The steps of closing and reusing the file descriptor newfd are
            performed atomically.




            POSIX, under "Rationale":




            On the other hand, the dup2() function provides unique services, as no other interface is able to atomically replace an existing file descriptor.




            I can't see any reason it should be atomic with regard opening, closing and dup'ing the file descriptor, but not with regard to reads or writes.






            share|improve this answer
























              up vote
              1
              down vote



              accepted










              The Linux man page and the POSIX description rather clearly say that it is, indeed, atomic:



              Linux man page on dup2():




              The steps of closing and reusing the file descriptor newfd are
              performed atomically.




              POSIX, under "Rationale":




              On the other hand, the dup2() function provides unique services, as no other interface is able to atomically replace an existing file descriptor.




              I can't see any reason it should be atomic with regard opening, closing and dup'ing the file descriptor, but not with regard to reads or writes.






              share|improve this answer






















                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                The Linux man page and the POSIX description rather clearly say that it is, indeed, atomic:



                Linux man page on dup2():




                The steps of closing and reusing the file descriptor newfd are
                performed atomically.




                POSIX, under "Rationale":




                On the other hand, the dup2() function provides unique services, as no other interface is able to atomically replace an existing file descriptor.




                I can't see any reason it should be atomic with regard opening, closing and dup'ing the file descriptor, but not with regard to reads or writes.






                share|improve this answer












                The Linux man page and the POSIX description rather clearly say that it is, indeed, atomic:



                Linux man page on dup2():




                The steps of closing and reusing the file descriptor newfd are
                performed atomically.




                POSIX, under "Rationale":




                On the other hand, the dup2() function provides unique services, as no other interface is able to atomically replace an existing file descriptor.




                I can't see any reason it should be atomic with regard opening, closing and dup'ing the file descriptor, but not with regard to reads or writes.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 27 at 19:28









                ilkkachu

                49.3k672136




                49.3k672136






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f427004%2fis-dup2-atomic-with-respect-to-writes-read%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)