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

查看:304
本文介绍了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),并将旧进程(父进程)的PID作为其父进程PID(PPID).因为这两个进程现在正在运行完全相同的代码,所以它们可以通过fork的返回码来分辨出哪个-子代为0,父代为子代的PID.当然,这一切都假设派生调用有效-否则,将不会创建任何子代且父代会获得错误代码.

The new process (child) gets a different process ID (PID) and has the 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 :克隆(作为派生)创建一个新进程.与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.

使用克隆创建子进程时,它将执行功能应用程序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.

信息已获取:

  • Differences between fork and exec
  • http://www.allinterview.com/showanswers/59616.html
  • http://www.unixguide.net/unix/programming/1.1.2.shtml
  • http://linux.about.com/library/cmd/blcmdl2_clone.htm

感谢您抽出宝贵的时间阅读本文! :)

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天全站免登陆