Standard streams and vfork

I have been playing around with fork / vfork functions a bit and there is something that puzzles me. Stevens's book states that:

Note that in Figure 8.3 we are calling _exit instead of exiting.

As we described in section 7.3, _exit does not perform any flushing of the standard I / O buffers. If we call exit, the results are undefined. Depending on the implementation of the standard I / O library, we might not see a difference in the output, or we might find that the output from the parent printf is gone. If the child calls exit, the implementation flushes the standard I / O streams. If this is the only action taken by the library, then we will not see any difference with the result generated if the child called _exit. However, if the implementation also closes standard I / O streams, the memory representing the FILE object for standard output will be flushed. Since the child borrows the parent's address space when the parent resumes and calls printf,no output appears and printf will return -1. Note that the parent STDOUT_FILENO is still valid, as the child receives a copy of the parent file descriptor array (see Figure 8.2). Most modern exit implementations don't bother to close threads. As the process is about to terminate, the kernel will close all file descriptors open in the process. Closing them in a library just adds overhead without any benefit.discovered in this process. Closing them in a library just adds overhead without any benefit.discovered in this process. Closing them in a library just adds overhead without any benefit.

so i tried to check if i could get printf error, my vfork manual has:

All open streams of stdio (3) are flushed and closed. Files created by tmpfile (3) are removed.

but when I compile and execute this program:

  #include<stdio.h>
  #include<stdlib.h>
  #include<unistd.h>
  #include<sys/types.h>
  #include<sys/wait.h>

  int main()
{
  int s;
  pid_t ret;
  if (vfork() == 0)
  {
      //abort();
      exit(6);
  }
  else
  {
      ret=wait(&s);
      printf("termination status to %d\n",s);
      if (WIFEXITED(s))
          printf("normalnie, status to %d\n",WEXITSTATUS(s));
  }
  return 0;
}

      

everything works fine, i don't get any printf errors. Why is this?

+3


source to share


1 answer


At the end of the paragraph you quoted it says:

Most modern exit implementations will not make it difficult to close streams. As the process is about to terminate, the kernel will close all file descriptors open in the process. Closing them in a library just adds overhead without any benefit.

This is most likely what is happening. Your OS doesn't actually close the stream (but it probably hides it).



What matters exit

here is its basic concept. The child shares parent memory and stack stack. This means that it is very easy for a child to change something that the parent did not expect, which could cause the parent to crash or malfunction when it starts up again. The man page for vfork

says that the only thing a process can do is a call exit()

or exec

. In fact, the child shouldn't even have to allocate memory or change any variables.

To see the effect of this, try putting the call vfork

inside a function and letting the child return or change some variables there and see what happens.

+3


source







All Articles