Home > OS >  Linux the child close close to inherit the problem of file descriptors
Linux the child close close to inherit the problem of file descriptors

Time:09-27

This is I found on the Internet saying:
In general, the child to call close () will not what happened, the close () function closes the file, not under any circumstances close the file directly, but to find a struct f_count members in the file structure, perform the decrement operation; Until f_count is 0, is the real close the file, this is the famous technology, reference counting,

Now have the following code
Int main ()
{
Int fd=1;
Pid_t pid=1;
Fd=open (" 1. TXT ", O_RDWR | O_TRUNC) will be passed.
Pid=fork ();
If (pid & gt; 0 {
Printf (" parent. \ n ");
Write (fd, "hello! \ n", 6); }
If (pid==0) {
Printf (" child. \ n ");
Write (fd, "world \ n", 6); }
Close (fd);
exit(0);
}
According to the above, no matter who first close a parent and child process (fd), and will not affect other fd on files in the process of operation, but why I print only executable files get hello or the result of the world?
  • Related