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 ?
files fork
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.
add a comment |
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 ?
files fork
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 callingopenbefore or afterfork?
– JigglyNaga
2 days ago
Yep parent is the one that opens the file and then calls fork
– x1b
yesterday
add a comment |
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 ?
files fork
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
files fork
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.
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 callingopenbefore or afterfork?
– JigglyNaga
2 days ago
Yep parent is the one that opens the file and then calls fork
– x1b
yesterday
add a comment |
2
Is the parent callingopenbefore or afterfork?
– 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
add a comment |
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)).
add a comment |
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.
add a comment |
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)).
add a comment |
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)).
add a comment |
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)).
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)).
edited 2 days ago
answered 2 days ago
ctrl-alt-delor
10.4k41955
10.4k41955
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 2 days ago
RalfFriedl
5,2473925
5,2473925
add a comment |
add a comment |
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.
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
2
Is the parent calling
openbefore or afterfork?– JigglyNaga
2 days ago
Yep parent is the one that opens the file and then calls fork
– x1b
yesterday