Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does fork return?

Tags:

c

linux

fork

On success, the PID of the child process is returned in the parent’s thread of execution, and a 0 is returned in the child’s thread of execution.

p = fork();

I'm confused at its manual page,is p equal to 0 or PID?

like image 464
compiler Avatar asked Apr 07 '11 07:04

compiler


People also ask

How does fork return two values?

fork does not return two values. Right after a fork system call you simply have two independent processes executing the same code, and the returned pid from fork is the only way to distinguish which process are you in - the parent or the child.

How does fork return 0 to child process?

RETURN VALUES The reason fork returns 0 to the child is that a process can have only a single parent, and the child can always call getppid to obtain the process ID of its parent. (Process ID 0 is reserved for use by the kernel, so it's not possible for 0 to be the process ID of a child.)

What does it mean when fork returns 0?

Below are different values returned by fork(). Negative Value: creation of a child process was unsuccessful. Zero: Returned to the newly created child process. Positive value: Returned to parent or caller.

Does fork return an int?

The fork() does not take any parameter, it returns integer values. It may return three types of integer values. Positive Value: The positive value is returned to the parent process.


3 Answers

I'm not sure how the manual can be any clearer! fork() creates a new process, so you now have two identical processes. To distinguish between them, the return value of fork() differs. In the original process, you get the PID of the child process. In the child process, you get 0.

So a canonical use is as follows:

p = fork(); if (0 == p) {     // We're the child process } else if (p > 0) {     // We're the parent process } else {     // We're the parent process, but child couldn't be created } 
like image 132
Oliver Charlesworth Avatar answered Sep 19 '22 15:09

Oliver Charlesworth


                              p = fork();                         /* assume no errors */                         /* you now have two */                         /* programs running */                          --------------------       if (p > 0) {                |            if (p == 0) {         printf("parent\n");       |              printf("child\n");         ...                       |              ... 
like image 21
pmg Avatar answered Sep 18 '22 15:09

pmg


Processes are structured in a directed tree where you only know your single-parent (getppid()). In short, fork() returns -1 on error like many other system functions, non-zero value is useful for initiator of the fork call (the parent) to know its new-child pid.

Nothing is as good as example:

/* fork/getpid test */
#include <sys/types.h>
#include <unistd.h>     /* fork(), getpid() */
#include <stdio.h>

int main(int argc, char* argv[])
{
    int pid;

    printf("Entry point: my pid is %d, parent pid is %d\n",
           getpid(), getppid());

    pid = fork();
    if (pid == 0) {
        printf("Child: my pid is %d, parent pid is %d\n",
               getpid(), getppid());
    }
    else if (pid > 0) {
        printf("Parent: my pid is %d, parent pid is %d, my child pid is %d\n",
               getpid(), getppid(), pid);
    }
    else {
        printf("Parent: oops! can not create a child (my pid is %d)\n",
               getpid());
    }

    return 0;
}

And the result (bash is pid 2249, in this case):

Entry point: my pid is 16051, parent pid is 2249
Parent: my pid is 16051, parent pid is 2249, my child pid is 16052
Child: my pid is 16052, parent pid is 16051

If you need to share some resources (files, parent pid, etc.) between parent and child, look at clone() (for GNU C library, and maybe others)

like image 34
levif Avatar answered Sep 21 '22 15:09

levif