fork 和 exec 的区别 [英] Differences between fork and exec
问题描述
fork
和 exec
有什么区别?
推荐答案
fork
和 exec
的使用体现了 UNIX 的精神,因为它提供了一种非常简单的方法启动新流程.
The use of fork
and exec
exemplifies the spirit of UNIX in that it provides a very simple way to start new processes.
fork
调用基本上复制了当前进程,几乎在各方面都相同.并非所有内容都被复制(例如,某些实现中的资源限制),但其想法是创建尽可能接近的副本.
The fork
call basically makes a duplicate of the current process, identical in almost every way. Not everything is copied over (for example, resource limits in some implementations) but the idea is to create as close a copy as possible.
新进程(子进程)获得不同的进程 ID(PID),并将旧进程(父进程)的 PID 作为其父进程 PID(PPID).因为这两个进程现在运行完全相同的代码,他们可以通过 fork
的返回码来判断哪个是哪个 - 子进程为 0,父进程得到子进程的 PID.当然,假设 fork
调用有效,这就是全部 - 如果没有,则不会创建子级,父级会收到错误代码.
The new process (child) gets a different process ID (PID) and has the PID of the old process (parent) as its parent PID (PPID). Because the two processes are now running exactly the same code, they can tell which is which by the return code of fork
- the child gets 0, the parent gets the PID of the child. This is all, of course, assuming the fork
call works - if not, no child is created and the parent gets an error code.
exec
调用是一种基本上用新程序替换整个当前进程的方法.它将程序加载到当前进程空间并从入口点运行它.
The exec
call is a way to basically replace the entire current process with a new program. It loads the program into the current process space and runs it from the entry point.
因此,fork
和 exec
通常按顺序使用,以使新程序作为当前进程的子进程运行.每当您尝试运行诸如 find
之类的程序时,Shell 通常都会执行此操作 - Shell 分叉,然后子程序将 find
程序加载到内存中,设置所有命令行参数,标准I/O 等.
So, fork
and exec
are often used in sequence to get a new program running as a child of a current process. Shells typically do this whenever you try to run a program like find
- the shell forks, then the child loads the find
program into memory, setting up all command line arguments, standard I/O and so forth.
但它们不需要一起使用.例如,如果程序同时包含父代码和子代码(你需要小心你做什么,每个实施可能有限制).这在守护进程中被大量使用(现在仍然如此),它们只是在 TCP 端口上侦听并fork
自己的副本来处理特定请求,而父进程则返回侦听.
But they're not required to be used together. It's perfectly acceptable for a program to fork
itself without exec
ing if, for example, the program contains both parent and child code (you need to be careful what you do, each implementation may have restrictions). This was used quite a lot (and still is) for daemons which simply listen on a TCP port and fork
a copy of themselves to process a specific request while the parent goes back to listening.
同样,知道自己已经完成并且只想运行另一个程序的程序不需要fork
、exec
然后wait
给孩子.他们可以直接将子进程加载到他们的进程空间中.
Similarly, programs that know they're finished and just want to run another program don't need to fork
, exec
and then wait
for the child. They can just load the child directly into their process space.
一些 UNIX 实现有一个优化的 fork
,它使用他们所谓的 copy-on-write.这是一个技巧,可以延迟 fork
中进程空间的复制,直到程序尝试更改该空间中的某些内容.这对于那些只使用 fork
而不是 exec
的程序很有用,因为它们不必复制整个进程空间.
Some UNIX implementations have an optimized fork
which uses what they call copy-on-write. This is a trick to delay the copying of the process space in fork
until the program attempts to change something in that space. This is useful for those programs using only fork
and not exec
in that they don't have to copy an entire process space.
如果在 fork
之后调用 exec
(这是最常发生的情况),则会导致写入进程空间,并且然后为子进程复制.
If the exec
is called following fork
(and this is what happens mostly), that causes a write to the process space and it is then copied for the child process.
请注意,有一整套 exec
调用(execl
、execle
、execve
等等) 但 exec
在上下文中是指其中任何一个.
Note that there is a whole family of exec
calls (execl
, execle
, execve
and so on) but exec
in context here means any of them.
下图说明了典型的 fork/exec
操作,其中 bash
shell 用于通过 ls
命令列出目录:
The following diagram illustrates the typical fork/exec
operation where the bash
shell is used to list a directory with the ls
command:
+--------+
| pid=7 |
| ppid=4 |
| bash |
+--------+
|
| calls fork
V
+--------+ +--------+
| pid=7 | forks | pid=22 |
| ppid=4 | ----------> | ppid=7 |
| bash | | bash |
+--------+ +--------+
| |
| waits for pid 22 | calls exec to run ls
| V
| +--------+
| | pid=22 |
| | ppid=7 |
| | ls |
V +--------+
+--------+ |
| pid=7 | | exits
| ppid=4 | <---------------+
| bash |
+--------+
|
| continues
V
这篇关于fork 和 exec 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!