fork()、vfork()、exec()和clone()的区别 [英] The difference between fork(), vfork(), exec() and clone()

查看:37
本文介绍了fork()、vfork()、exec()和clone()的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Google 上找到这四个之间的区别,我预计会有大量关于这方面的信息,但是这四个调用之间确实没有任何可靠的比较.

I was looking to find the difference between these four on Google and I expected there to be a huge amount of information on this, but there really wasn't any solid comparison between the four calls.

我开始尝试编译一种基本的概览,看看这些系统调用之间的差异,这就是我得到的.所有这些信息是否正确/我是否遗漏了任何重要的信息?

I set about trying to compile a kind of basic at-a-glance look at the differences between these system calls and here's what I got. Is all this information correct/am I missing anything important ?

Fork :fork 调用基本上复制了当前进程,几乎在所有方面都相同(并非所有内容都被复制,例如,某些实现中的资源限制,但其想法是创建尽可能接近副本).

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),并将旧进程(父进程)的 PI​​D 作为其父进程 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.

Vfork :vfork 和 fork 的基本区别在于,当使用 vfork() 创建新进程时,父进程会暂时挂起,子进程可能会借用父进程的地址空间.这种奇怪的状态一直持续到子进程退出或调用 execve(),此时父进程过程继续.

Vfork : The basic difference between vfork and fork is that when a new process is created with vfork(), the parent process is temporarily suspended, and the child process might borrow the parent's address space. This strange state of affairs continues until the child process either exits, or calls execve(), at which point the parent process continues.

这意味着 vfork() 的子进程必须小心避免意外修改父进程的变量.特别是子进程一定不能从包含vfork()调用的函数中返回,也不能调用exit()(如果需要退出,应该使用_exit();其实对于子进程也是如此一个普通的 fork()).

This means that the child process of a vfork() must be careful to avoid unexpectedly modifying variables of the parent process. In particular, the child process must not return from the function containing the vfork() call, and it must not call exit() (if it needs to exit, it should use _exit(); actually, this is also true for the child of a normal fork()).

Exec : exec 调用是一种基本上用新程序替换整个当前进程的方法.它将程序加载到当前进程空间并从入口点运行它.exec() 用函数指向的可执行文件替换当前进程.除非出现 exec() 错误,否则控制永远不会返回到原始程序.

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. exec() replaces the current process with a the executable pointed by the function. Control never returns to the original program unless there is an exec() error.

Clone : Clone,作为 fork,创建一个新进程.与 fork 不同,这些调用允许子进程与调用进程共享部分执行上下文,例如内存空间、文件描述符表和信号处理程序表.

Clone : Clone, as fork, creates a new process. Unlike fork, these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers.

当子进程用clone创建时,它执行函数application fn(arg).(这与 fork 不同,fork 在子进程中从原始 fork 调用的点继续执行.) fn 参数是指向子进程在执行开始时调用的函数的指针.arg 参数被传递给 fn 函数.

When the child process is created with clone, it executes the function application fn(arg). (This differs from fork, where execution continues in the child from the point of the original fork call.) The fn argument is a pointer to a function that is called by the child process at the beginning of its execution. The arg argument is passed to the fn function.

当 fn(arg) 函数应用程序返回时,子进程终止.fn 返回的整数是子进程的退出代码.子进程也可以通过调用 exit(2) 或在收到致命信号后显式终止.

When the fn(arg) function application returns, the child process terminates. The integer returned by fn is the exit code for the child process. The child process may also terminate explicitly by calling exit(2) or after receiving a fatal signal.

信息获取形式:

感谢您花时间阅读本文!:)

Thanks for taking the time to read this ! :)

推荐答案

  • vfork() 是一个过时的优化.在良好的内存管理之前,fork() 制作了父级内存的完整副本,因此非常昂贵.因为在很多情况下,fork() 后面跟着 exec(),它会丢弃当前的内存映射并创建一个新的内存映射,这是一个不必要的开销.现在,fork() 不会复制内存;它只是设置为写入时复制",所以 fork()+exec()vfork()+ 一样有效>exec().

    • vfork() is an obsolete optimization. Before good memory management, fork() made a full copy of the parent's memory, so it was pretty expensive. since in many cases a fork() was followed by exec(), which discards the current memory map and creates a new one, it was a needless expense. Nowadays, fork() doesn't copy the memory; it's simply set as "copy on write", so fork()+exec() is just as efficient as vfork()+exec().

      clone()fork() 使用的系统调用.使用一些参数,它创建一个新进程,使用其他参数,它创建一个线程.它们之间的区别只是共享或不共享哪些数据结构(内存空间、处理器状态、堆栈、PID、打开的文件等).

      clone() is the syscall used by fork(). with some parameters, it creates a new process, with others, it creates a thread. the difference between them is just which data structures (memory space, processor state, stack, PID, open files, etc) are shared or not.

      这篇关于fork()、vfork()、exec()和clone()的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆