为什么 Unix 有 fork() 而没有 CreateProcess()?
我不明白为什么 Unix 有 fork()
< /a> 用于创建新进程。在 Win32 API 中,我们有 < code>CreateProcess() 创建一个新进程并将可执行文件加载到其地址空间中,然后从入口点开始执行。然而,Unix 提供了用于创建新进程的 fork,如果我想运行另一个进程,我不明白为什么要复制我的进程。
那么让我问这两个问题:
- 如果
fork()
然后exec()
效率更高,为什么没有一个函数forkexec(const char * newProc)
因为几乎在所有情况下我们都会在fork()
之后调用exec()
吗? - 如果它没有效率更高,为什么还要存在
fork()
呢?
I do not get why Unix has fork()
for creating a new process. In Win32 API we have CreateProcess()
which creates a new process and loads an executable into its address space, then starts executing from the entry point. However Unix offers fork for creating a new process, and I don't get why would I duplicate my process if I'd like to run another process.
So let me ask these two questions:
- If
fork()
and thenexec()
is more efficient, why isn't there a functionforkexec(const char *newProc)
since we will callexec()
afterfork()
almost in every case? - If it is not more efficient, why does
fork()
exist at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
fork()
调用就足够了。也更加灵活;它允许您调整子进程中的 I/O 重定向,而不是使创建进程的系统调用复杂化。对于 SUID 或 SGID 程序,它允许子进程在执行其他进程之前失去其提升的权限。如果您想要以复杂的方式创建流程,请查找
posix_spawn()
函数。不同之处在于 posix_spawnp() 会在 PATH 上搜索可执行文件。
还有一整套其他函数用于处理 posix_spawn_file_actions_t 和 posix_spawnattr_t 类型(请点击引用的手册页底部的“另请参阅”链接)。
这有点像 Windows 上的
CreateProcess()
。不过,在大多数情况下,使用fork()
后紧跟着exec()
会更简单。很多时候,您执行的代码不是由您编写的,因此您无法修改子进程开始时发生的情况。想想一个外壳;如果您从 shell 运行的唯一程序是您编写的程序,那么生活将变得非常贫困。
通常,您执行的代码会从许多不同的地方调用。特别是,考虑一个 shell 和一个程序,它们有时在管道中执行,有时在没有管道的情况下执行。被调用的程序无法知道它应该做什么 I/O 重定向和修复;调用程序知道。
如果调用程序以提升的权限(SUID 或 SGID 权限)运行,则在运行另一个程序之前想要关闭这些权限是正常的。依靠其他程序来知道要做什么是......愚蠢的。
The
fork()
call is sufficient. It is also more flexible; it allows you to things like adjust the I/O redirection in the child process, rather than complicating the system call to create the process. With SUID or SGID programs, it allows the child to lose its elevated privileges before executing the other process.If you want a complex way to create a process, lookup the
posix_spawn()
function.The difference is the
posix_spawnp()
does a search on PATH for the executable.There is a whole set of other functions for handling
posix_spawn_file_actions_t
andposix_spawnattr_t
types (follow the 'See Also' links at the bottom of the referenced man page).This is quite a bit more like
CreateProcess()
on Windows. For the most part, though, usingfork()
followed shortly byexec()
is simpler.Very often, the code you execute is not written by you, so you can't modify what happens in the beginning of the child's process. Think of a shell; if the only programs you run from the shell are those you've written, life is going to be very impoverished.
Quite often, the code you execute will be called from many different places. In particular, think of a shell and a program that will sometimes be executed in a pipeline and sometimes executed without pipes. The called program cannot tell what I/O redirections and fixups it should do; the calling program knows.
If the calling program is running with elevated privileges (SUID or SGID privileges), it is normal to want to turn those 'off' before running another program. Relying on the other program to know what to do is ... foolish.
类 UNIX 操作系统(至少是较新的 Linux 和 BSD 内核)通常具有非常高效
fork
实现——它“如此便宜”以至于有“线程”实现某些语言基于它。最后,
forkexec
函数是 ~n 行应用程序代码(对于 n 的某个小值)。我确实希望 Windows 有这样一个有用的
ForkProcess
:(快乐编码
。cnicutar 提到,写入时复制 (COW) 是使用的一种策略。
UNIX-like operating systems (at least newer Linux and BSD kernels) generally have a very efficient
fork
implementation -- it is "so cheap" that there are "threaded" implementations based upon it in some languages.In the end the
forkexec
function is ~n -- for some small value of n -- lines of application code.I sure wish windows had such a useful
ForkProcess
:(Happy coding.
A cnicutar mentioned, Copy-On-Write (COW) is one strategy used.
有一个函数相当于forkexec - system
http://www.tutorialspoint.com/c_standard_library/ c_function_system.htm
There is a function that is equivalent to forkexec - system
http://www.tutorialspoint.com/c_standard_library/c_function_system.htm