开发者

C: close(2) after fork()?

开发者 https://www.devze.com 2023-03-06 10:21 出处:网络
Quick question, hoping someone can verify. After a fork, if you call close(2) in the parent, stderr in the child is unaffected. However, if you call close(2) in the child, stderr in the parent is clos

Quick question, hoping someone can verify. After a fork, if you call close(2) in the parent, stderr in the child is unaffected. However, if you call close(2) in the child, stderr in the parent is closed. Does that seem right? I tested this in FreeBSD and it seems to be the case, but I'm not sure why. I would expect that either they both don't affect each other or they do, but not this.

Any ins开发者_高级运维ight?


After a fork, every open file descriptor in the parent gets dup'ed, so any close after the fork won't affect either the parent or the child.

Unless, you're doing it not properly (i.e. not checking the output of the fork() system call).

0

精彩评论

暂无评论...
验证码 换一张
取 消