什么是叉返回?
回答
fork()
在父进程中被调用。然后产生一个子进程。子进程产生时,fork()
已完成执行。
在这一点上,fork()
已准备好返回,但它返回一个不同的值,具体取决于它是否在父级或子级。在子进程中,它返回0,并在父进程/线程中返回子进程ID。
我不确定手册如何更清晰! fork()
创建一个新的进程,所以你现在有两个相同的进程。为了区分它们,fork()
的返回值不同。在原始过程中,您将获得子进程的PID。在子过程中,你会得到0
因此,一个规范的使用方法如下:
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
}
这是最酷的部分。 它等于两者。
呃,不是真的。但是一旦fork
返回,您现在有两个程序正在运行!两个过程。你可以把它们想象成替代宇宙。其中一个返回值是0
。另一方面,这是新工艺的ID
!
通常你有这样的事情:
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
}
在这种情况下,两个字符串将获得印刷,但通过不同的工艺!
您有父母/子女倒退。 – interjay 2011-04-07 07:56:12
@interjay:哎呀!谢谢。固定。 – 2011-04-07 07:58:54
同时检查错误(-1 == p)。 – 2016-11-24 20:14:19
一旦fork
被执行,你有两个进程。该调用为每个进程返回不同的值。
如果你做这样的事情
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);
}
你会看到两个打印的信息。他们正在通过两个独立的流程进行打印。这是他们可以区分创建的两个过程的方式。
当它返回0时,意味着'fork'没有创建一个进程,对吧? – compiler 2011-04-07 08:01:06
不,这意味着你在孩子的过程中。 “不创建流程”意味着只有一个流程。如果返回“-1”,则意味着没有创建子项。手册页指出了这一点。 – 2011-04-07 08:04:20
什么时候不创建孩子?如果我在while循环中使用'fork()',我会获得-1值吗? – Heisenberg 2013-10-07 14:25:55
p = fork(); /* assume no errors */ /* you now have two */ /* programs running */ -------------------- if (p > 0) { | if (p == 0) { printf("parent\n"); | printf("child\n"); ... | ...
过程的执行向树,你只知道你的单亲(getppid()
)的结构。总之,fork()
像许多其他系统函数一样返回-1
错误,非零值对于fork调用(父级)的发起者知道其新子pid很有用。
没有什么作为例子一样好:
/* 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;
}
而结果(是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
如果你需要共享一些资源(文件,父pid等),看看clone()
(对于GNU C库,也可能是其他的)
Fork创建一个重复的进程和一个新的进程上下文。当它返回一个0值时,这意味着一个子进程正在运行,但是当它返回另一个值,这意味着一个父进程正在运行。我们通常使用wait语句,以便子进程完成并且父进程开始执行。
我认为它的工作原理是这样的: 当pid = fork()时,代码应该执行两次,一个在当前进程中,一个在子进程中。 所以它解释了为什么if/else都执行。 顺序是,第一个当前进程,然后执行子进程。
订单不保证(据我所知)。 – 2016-11-24 20:12:30
- 1. Jquery $() - 它返回什么,什么是$()[0]?
- 2. MongoDB:什么是getLastError()返回
- 3. 是什么PackageInfo.signatures返回?
- 4. 什么是返回以下
- 5. 什么是CvMatchShapes返回?
- 6. mysql_insert_id()返回的是什么?
- 7. 什么是“返回false”;做?
- 8. 什么是“返回Not(S是没有什么)”回归
- 9. 返回什么?
- 10. 什么是返回2不是0?
- 11. 为什么我的线段交叉测试不会返回true?
- 12. 什么是Scala中的“(x,y)”,实际返回的是什么?
- 13. 对于返回任务的过程,什么意思是什么都不返回?
- 14. compareTo()返回什么?
- 15. this.id返回什么?
- 16. getCurrentPosition返回什么?
- 17. wave.readframes返回什么?
- 18. TelephonyManager.getNetworkCountryIso()返回什么?
- 19. 什么“master.dbo.xp_fixeddrives”返回
- 20. main()返回什么?
- 21. classname.class返回什么?
- 22. 什么返回mysqli_insert_id()?
- 23. request.getParameter返回什么?
- 24. $(...)。map()返回什么?
- 25. mxGetPr返回什么?
- 26. IO.sysopen返回什么?
- 27. CGColorGetComponents()返回什么?
- 28. setTimeout返回什么?
- 29. int.class返回什么
- 30. adapter.getItem()返回什么?
有人可以在图片中包含'getpid()'吗? 'getpid()'在孩子返回0? – Shrinidhi 2011-04-07 08:47:02
@Shrinidhi:'fork()'也是。 – BoltClock 2011-04-07 08:50:51
它既是pid也是0.当fork被调用时,程序“分裂”成两个 - 它自己和它的邪恶的双胞胎。在原来的程序中,它是0.在邪恶的双胞胎计划中,它是pid。 – 2011-04-07 09:38:09