abkds
abkds

Reputation: 1794

a simple fork program

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
  pid_t child_pid;
  printf ("the main program process ID is %d\n", (int) getpid());
  child_pid = fork () ;
  if (child_pid != 0) 
  {
    printf ("this is the parent process, with id %d\n", (int) getpid ());
    printf ("the child's process ID is %d\n",(int) child_pid );
  }
  else
    printf ("this is the child process, with id %d\n", (int) getpid ());
  return 0;
}

I have written a simple fork program to create a process , when I run the program I get the output as follows :

the main program process ID is 3322
this is the child process , with ID 0

Now my point is , why isn't the if block getting executed as in, in the parent process the return value of fork() is not equal to 0 so why am I not getting any output for the if block ?

Upvotes: 1

Views: 6991

Answers (1)

Lee Duhem
Lee Duhem

Reputation: 15121

The following code have at least two problems:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
    pid_t child_pid;
    printf ("the main program process ID is %d\n", (int) getpid());
    child_pid = fork () ;
    if (child_pid > 0) 
    {
        printf ("this is the parent process, with id %d\n", (int) getpid ());
        printf ("the child's process ID is %d\n",(int) child_pid );
    }
    else if (child_pid == 0)
        printf ("this is the child process, with id %d\n", (int) getpid ());
    else
        printf ("fork failed\n");

    return 0;
}
  1. It did not flush the output buffer after the first printf().

    If the output of this program is redirected to a file, the output will become block buffered, then the output of this printf() may or may not be written to the output file, therefore, the output file could be five or four lines (actually, most of them are five lines).

    Add a fflush(stdout); after the first printf() can fix this problem.

  2. The parent process terminated before the child process.

    In this case, once or twice in several thousands of runs of this program, the output of the child process is lost, the output file only has three lines. But after that fflush() be added, the output file should be four lines.

    I do not really understand why this happens, or whether it is correct. But fortunately, it is quite easy to fix. Add a wait(NULL); after the third printf() is enough.


Here is some very helpful explanation from Art:

  1. POSIX mandates that the file descriptors in the child process after fork point to the same file descriptions (file descriptions are not the same thing as file descriptors). The file descriptions contain (among others) the file offset. printf will eventually go through write to write out its output and write is guaranteed to atomically update the file offset and write out the data. So redirecting output in a forking program is safe as long as the program doesn't seek (which you're not supposed to do on stdout and printf definitely shouldn't).

  2. references that didn't fit in the previous comment: file description, fork.

Upvotes: 2

Related Questions