0

I've decided to learn C, and here is the snippet from one of the books that I use:

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

int main()
{
    pid_t result = fork();

    if (result == -1){
        fprintf(stderr, "Error\n");
        return 1;
    }

    if (result == 0)
        printf("I'm a child with PID = %d\n", getpid());
    else
        printf("I'm a parent with PID = %d\n", getpid());

    return 0;
}

Its output is:

I'm a parent with PID = 5228
I'm a child with PID = 5229

Everything's clear, but how could it be that result == 0 and result != 0 at the same time? It looks like this variable stores two values, because the printf instruction is executed twice. I know, that fork() returns 0 and a parent's PID, but how does result check if it returns true for different conditions?

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Yurkol
  • 1,414
  • 1
  • 19
  • 28
  • System call [fork()](http://en.wikipedia.org/wiki/Fork_%28system_call%29) is used to create processes. It takes no arguments and returns a process ID. – Dayal rai May 15 '15 at 05:52

5 Answers5

7

Because it's not the same variable. When you fork a process, you end up with two totally different processes (see this answer for more detail).

Hence the result variable in the parent is not the same as the one in the child. What you're seeing is two processes, both attached to the same output device, each writing their own message.

In fact, the fork documentation specifically covers that:

On success, the PID of the child process is returned in the parent, and 0 is returned in the child.

So you can use the return value from fork (as you do) to see if you're the parent or child (and to see if it worked as well, it'll return -1 if it fails and you'll be the parent with no child).

The idea is that the parent gets the process ID of the child so it can do something with it (like wait() for it to finish) and the child gets zero. The child can always get the process ID of the parent by calling getppid().

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

A variable can only hold a single value at a time. What you're seeing is happening because fork() is creating another process: there's now two instances of your program running; one in which result == 0 (the spawned process), and another where result != 0 (the original process)

Schlaus
  • 18,144
  • 10
  • 36
  • 64
2

fork replicates a child from a parent. So the newly created child inherits several properties like shared memory, message queue, file streams etc from the parent. So when you call fork, another process with another variable result is created.

2

The fork() function create a new process, after this line your program split to 2 from that spot. Because you need to know which process are you, the function return 0 if you are the child process, and some pid if you are the father process.

from the man page:

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Noam M
  • 3,156
  • 5
  • 26
  • 41
1

fork() function will create a new process. in parent process, fork() will return the pid of child process, so "result" variable will not equal with 0, and in child process, fork() just return 0, so "result" is 0.

bingxian
  • 11
  • 2