fork到底返回什么?
成功时,子进程的 PID 进程返回到父进程中 执行线程,0 是 在子进程的执行线程中返回。
p = fork();
我对它的手册页感到困惑,p
等于0
还是PID
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不确定手册如何能更清晰!
fork()
创建一个新进程,因此您现在有两个相同进程。为了区分它们,fork()
的返回值有所不同。在原始进程中,您获取子进程的PID。在子进程中,你得到 0。所以规范的用法如下:
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 offork()
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:
进程采用有向树结构,您只知道您的单父进程 (
getppid()
)。简而言之,与许多其他系统函数一样,fork()
在出错时返回-1
,非零值对于 fork 调用的发起者很有用(父级)了解其新子级 pid。没有什么比例子更好的了:
结果(bash 是 pid 2249,在本例中):
如果您需要在父子之间共享一些资源(文件、父 pid 等),请查看
clone()
(对于 GNU C 库,也许还有其他库)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:
And the result (bash is pid 2249, in this case):
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)一旦执行了
fork
,就会有两个进程。该调用向每个进程返回不同的值。如果您执行类似的操作,
您将看到打印的两条消息。它们是由两个独立的过程打印的。这是您可以区分创建的两个进程的方式。
Once
fork
is executed, you have two processes. The call returns different values to each process.If you do something like this
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.
这是很酷的部分。 它等于两者。
嗯,不完全是。但是,一旦
fork
返回,您现在就有两个正在运行的程序副本!两个进程。你可以将它们视为替代宇宙。其中,返回值为0
。另一方面,它是新进程的ID
!通常你会得到这样的结果:
在这种情况下,两个字符串都会被打印,但是通过不同的进程!
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 is0
. In the other, it's theID
of the new process!Usually you will have something like this:
In this case, both strings will get printed, but by different processes!
fork()
在父进程中调用。然后生成一个子进程。当子进程生成时,fork()
已完成其执行。此时,
fork()
已准备好返回,但它返回不同的值,具体取决于它是在父级还是子级中。在子进程中,它返回0,在父进程/线程中,它返回子进程ID。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.Fork 创建一个重复的进程和一个新的进程上下文。当它返回 0 值时,表示子进程正在运行,但当它返回另一个值时,表示父进程正在运行。我们通常使用 wait 语句,以便子进程完成并父进程开始执行。
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.
我认为它的工作原理是这样的:
当pid = fork()时,代码应该执行两次,一次在当前进程中,一次在子进程中。
所以它解释了为什么 if/else 都会执行。
顺序是,先执行当前进程,然后执行子进程。
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.