user3291818
user3291818

Reputation: 203

Fork() in parent process

What happens if I were to call fork() in a parent process?

A general example:

int ret;
int fd[2];

ret = pipe(fd);
pid = fork();

if (pid == -1){
    perror("fork failed");
    exit(1);
}

else if (pid > 0){    //PARENT 
    //writes or reads
    fork();

}

Does this mean that fork() will create a new process of the parent?

I'm pretty new to this, so help would be much appreciated. Thanks.

Upvotes: 0

Views: 195

Answers (3)

Josh Watzman
Josh Watzman

Reputation: 7260

Every time you call fork, it "returns twice" -- once in the parent, once in the child. The terms "parent" and "child" in that description are largely for convenience of disambiguation. (Though there are some differences when you start getting into discussions about waitpid.) They are both "perfectly good processes" which can go on to fork other processes, or exec, or just both continue on their merry way doing something totally different.

In your example, let's suppose we start with a process with pid 1. Here's my attempt at a diagram of what happens, since tables aren't supported :\

  • We fork the first time. Process 2 is created.
    • In process 1, fork returns 2.
    • We continue on to the else if, which is true. We call fork again. Process 3 is created.
      • In process 1, fork returns 3.
      • Process 1 continues off the end of your code snippet.
      • In process 3, fork return 0.
      • Process 3 continues off the end of your code snippet.
    • In process 2, fork returns 0.
    • Process 2 continues on to the else if, which is false.
    • Process 2 continues off the end of your code snippet.

So you end up with 3 processes; process 1 is the parent of both 2 and 3. Any of those three could go on to fork again, creating more children, etc etc.

Upvotes: 1

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

What happens if I were to call fork() in a parent process?

Every process in which you call fork() can be considered a parent process.

From the Linux man page fork(2):

   fork() creates a new process by duplicating the calling process.  The
   new process, referred to as the child, is an exact duplicate of the
   calling process, referred to as the parent, except for the following
   points:

   *  The child has its own unique process ID, and this PID does not
      match the ID of any existing process group (setpgid(2)).

   *  The child's parent process ID is the same as the parent's process
      ID.
   ...

The result of fork() is two identical copies of the original process running. The one who originally called fork() is the parent, and the resulting process is the child. The child's PPID (parent process ID) is equal to the parent's PID.

original process
    |
    |    fork() called
    |\
    | \
    |  \
    |   \
parent  child

Upvotes: 1

sandymatt
sandymatt

Reputation: 5612

Does this mean that fork() will create a new process of the parent?

fork() creates a new process whose memory is a duplicate (although not shared) of the one it was called from.

Keep in mind that processes form a tree, where each successive process is the child of the one it was forked from (the parent).

Upvotes: 0

Related Questions