Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:45:29+00:00 2026-06-01T10:45:29+00:00

I am playing a bit with fork/vfork functions, and there is something that is

  • 0

I am playing a bit with fork/vfork functions, and there is something that is puzzling to me. In Stevens book it is written that:

Note in Figure 8.3 that we call _exit instead of exit.

As we described in Section 7.3, _exit does not perform any flushing of standard I/O buffers. If we call exit instead, the results are indeterminate.
Depending on the implementation of the standard I/O library, we might see no difference in the output, or we might find that the output from the parent’s printf has disappeared.
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 see no difference with the output generated if the child called _exit.
If the implementation also closes the standard I/O streams, however, the memory representing the FILE object for the standard output will be cleared out.
Because the child is borrowing the parent’s address space, when the parent resumes and calls printf, no output will appear and printf will return -1.
Note that the parent’s STDOUT_FILENO is still valid, as the child gets a copy of the parent’s file descriptor array (refer back to Figure 8.2).
Most modern implementations of exit will not bother to close the streams.
Because the process is about to exit, the kernel will close all the file descriptors open in the process.
Closing them in the library simply adds overhead without any benefit.

so I tried to test if I can get printf error, in my manual of vfork there is:

All open stdio(3) streams 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 is working fine, I don’t get any printf errors. Why is that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:45:30+00:00Added an answer on June 1, 2026 at 10:45 am

    The end of the paragraph you quoted says:

    Most modern implementations of exit will not bother to close the streams. Because the process is about to exit, the kernel will close all the file descriptors open in the process. Closing them in the library simply adds overhead without any benefit.

    This is most likely what’s happening. Your OS doesn’t actually close the stream (but it does probably flush it).

    The important thing isn’t what exit does here, its the underlying concept. The child is sharing the parent’s memory and stack frame. That means that the child can very easily change something that the parent did not expect, which could easily cause the parent to crash or misbehave when it starts running again. The man page for vfork says the only thing a process can do is call exit() or an exec. In fact, the child should not even allocate memory or modify any variables.

    To see the impact of this, try putting the vfork call inside of a function and let the child return or modify some variables there and see what happens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was playing around a bit with functions with variable arguments, and decided to
I've been playing around with Active Record a bit, and I have noticed that
I've been playing around with Haskell a fair bit, including practising writing functions in
I'm playing a bit with beta version of PHP Storm (PHP version of WebIDE)
Recently I've been playing a bit with Groovy and Grails and I have to
I finished NerdDinner tutorial and now I'm playing a bit with project. Index page
After playing quite a bit of Bad Company 2 over the last month, I'm
I'm playing a little bit with the new StackOverflow API . Unfortunately, my JSON
I have been playing around a bit with a fairly simple, home-made search engine,
Was playing around a little bit with the TimeSpan class and I started wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.