Is dup2 atomic with respect to writes/read

Clash 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?
linux
add a comment |Â
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?
linux
add a comment |Â
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?
linux
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?
linux
asked Feb 27 at 19:16
user135284
1234
1234
add a comment |Â
add a comment |Â
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
newfdare
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.
add a comment |Â
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
newfdare
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.
add a comment |Â
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
newfdare
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.
add a comment |Â
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
newfdare
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.
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
newfdare
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.
answered Feb 27 at 19:28
ilkkachu
49.3k672136
49.3k672136
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password