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.
但是不需要将它们一起使用.例如,如果程序同时包含父代码和子代码,则无需exec
就可以将程序本身fork
本身是完全可以接受的(您需要小心操作,每个实现都可能有限制).守护进程使用了很多(现在仍然),这些守护进程仅在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
,它使用了它们称为写时复制的功能.这是一个技巧,可以延迟对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屋!