What exactly does fork return?

2019-01-17 01:32发布

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?

标签: c linux fork
8条回答
爷的心禁止访问
2楼-- · 2019-01-17 01:40

Once fork is executed, you have two processes. The call returns different values to each process.

If you do something like this

int f;
f = fork();
if (f == 0) {
  printf("I am the child\n");
} else {
  printf("I am the parent and the childs pid is %d\n",f);

}

You will see both the messages printed. They're being printed by two separate processes. This is they way you can differentiate between the two processes created.

查看更多
小情绪 Triste *
3楼-- · 2019-01-17 01:44

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)

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-17 01:44

I think that it works like this: when pid = fork(), the code should be executed two times, one is in current process, one is in child process. So it explains why if/else both execute. And the order is, first current process, and then execute the child.

查看更多
Melony?
5楼-- · 2019-01-17 01:47

This is the cool part. It's equal to BOTH.

Well, not really. But once fork returns, you now have two copies of your program running! Two processes. You can sort of think of them as alternate universes. In one, the return value is 0. In the other, it's the ID of the new process!

Usually you will have something like this:

p = fork();
if (p == 0){
    printf("I am a child process!\n");
    //Do child things
}
else {
    printf("I am the parent process! Child is number %d\n", p);
    //Do parenty things
}

In this case, both strings will get printed, but by different processes!

查看更多
戒情不戒烟
6楼-- · 2019-01-17 01:53

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
}
查看更多
走好不送
7楼-- · 2019-01-17 01:57

fork() is invoked in the parent process. Then a child process is spawned. By the time the child process spawns, fork() has finished its execution.

At this point, fork() is ready to return, but it returns a different value depending on whether it's in the parent or child. In the child process, it returns 0, and in the parent process/thread, it returns the child's process ID.

查看更多
登录 后发表回答