fork 到底返回什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5577564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What exactly does fork return?
提问by compiler
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.
成功时,在父执行线程中返回子进程的PID,在子执行线程中返回0。
p = fork();
I'm confused at its manual page,is p
equal to 0
or PID
?
我对它的手册页感到困惑,是p
等于0
还是PID
?
回答by BoltClock
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.
fork()
在父进程中调用。然后产生一个子进程。到子进程产生时,fork()
已完成执行。
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.
此时,fork()
已准备好返回,但根据它是在父级还是子级中,它返回不同的值。在子进程中返回0,在父进程/线程中返回子进程ID。
回答by Oliver Charlesworth
I'm not sure how the manual can be any clearer! fork()
creates a new process, so you now have two identicalprocesses. 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.
我不确定手册如何更清晰! fork()
创建一个新进程,因此您现在有两个相同的进程。为了区分它们,返回值fork()
不同。在原始进程中,您获得子进程的PID。在子进程中,您得到 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
}
回答by Chris Cooper
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!
嗯,不是真的。但是一旦fork
返回,您现在就有两个正在运行的程序副本!两个过程。您可以将它们视为备用宇宙。其一,返回值是0
。另一方面,它ID
是新进程的!
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!
在这种情况下,两个字符串都将被打印,但通过不同的过程!
回答by Noufal Ibrahim
Once fork
is executed, you have two processes. The call returns different values to each process.
一旦fork
被执行,你有两个进程。该调用向每个进程返回不同的值。
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.
您将看到打印的两条消息。它们由两个独立的过程打印。这是您可以区分创建的两个进程的方式。
回答by pmg
p = fork(); /* assume no errors */ /* you now have two */ /* programs running */ -------------------- if (p > 0) { | if (p == 0) { printf("parent\n"); | printf("child\n"); ... | ...
回答by levif
Processes are structured in a directed treewhere 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.
进程被构建在一个有向树中,你只知道你的单亲(getppid()
)。简而言之,像许多其他系统函数一样错误fork()
返回-1
,非零值对于 fork 调用的发起者(父)了解其新子 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):
结果(在这种情况下,bash 为 pid 2249):
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)
如果您需要在父子之间共享某些资源(文件、父 pid 等),请查看clone()
(对于 GNU C 库,也许还有其他)
回答by mannukaushikece
Fork creates a duplicate process and a new process context. When it returns a 0 value it means that a child process is running, but when it returns another value that means a parent process is running. We usually use wait statement so that a child process completes and parent process starts executing.
Fork 创建一个重复的进程和一个新的进程上下文。当它返回一个 0 值时,它意味着一个子进程正在运行,但当它返回另一个值时意味着一个父进程正在运行。我们通常使用wait语句,以便子进程完成并且父进程开始执行。
回答by arswt
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.
我认为它是这样工作的:当pid = fork()时,代码应该执行两次,一次在当前进程中,一次在子进程中。所以它解释了为什么 if/else 都执行。顺序是,先当前进程,然后执行子进程。