用于创建进程和线程的 Linux 系统调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9477753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:52:54  来源:igfitidea点击:

Linux system call for creating process and thread

linuxmultithreadingprocesssystem-calls

提问by atoMerz

I read in a paperthat the underlying system call to create processes and threads is actually the same, and thus the cost of creating processes over threads is not that great.

我在一篇论文中看到创建进程和线程的底层系统调用实际上是相同的,因此通过线程创建进程的成本并没有那么大。

  • First, I wanna know what is the system call that creates processes/threads (possibly a sample code or a link?)
  • Second, is the author correct to assume that creating processes instead of threads is inexpensive?
  • 首先,我想知道创建进程/线程的系统调用是什么(可能是示例代码或链接?)
  • 其次,作者假设创建进程而不是线程成本低廉是否正确?

EDIT:
Quoting article:

编辑:
引用文章:

Replacing pthreads with processes is surprisingly inexpensive, especially on Linux where both pthreads and processes are invoked using the same underlying system call.

用进程替换 pthread 的成本低得惊人,尤其是在 Linux 上,pthread 和进程都使用相同的底层系统调用来调用。

采纳答案by Damon

Processes are usually created with fork, threads (lightweight processes) are usually created with clonenowadays. However, anecdotically, there exist 1:N thread models, too, which don't do either.

进程通常用 来创建fork,线程(轻量级进程)现在通常用 来创建clone。然而,有趣的是,也存在 1:N 线程模型,它们也没有。

Both forkand clonemap to the same kernel function do_forkinternally. This function can create a lightweight process that shares the address space with the old one, or a separate process (and many other options), depending on what flags you feed to it. The clonesyscall is more or less a direct forwarding of that kernel function (and used by the higher level threading libraries) whereas forkwraps do_forkinto the functionality of the 50 year old traditional Unix function.

双方forkclone映射到相同的内核函数do_fork内部。这个函数可以创建一个与旧进程共享地址空间的轻量级进程,或者一个单独的进程(和许多其他选项),这取决于你提供给它的标志。该clone系统调用或多或少是核函数(和上级线程库使用),而直接转发fork包裹do_fork到50岁传统的Unix函数的功能。

The important difference is that forkguarantees that a complete, separate copy of the address space is made. This, as Basil points out correctly, is done with copy-on-write nowadays and therefore is not nearly as expensive as one would think.
When you create a thread, it just reuses the original address space and the same memory.

重要的区别是fork保证了地址空间的完整、单独的副本。正如 Basil 正确指出的那样,现在是通过写时复制完成的,因此并不像人们想象的那么昂贵。
创建线程时,它只是重用原始地址空间和相同的内存。

However, one should not assume that creating processes is generally "lightweight" on unix-like systems because of copy-on-write. It is somewhat less heavy than for example under Windows, but it's nowhere near free.
One reason is that although the actual pages are not copied, the new process still needs a copy of the page table. This can be several kilobytes to megabytes of memory for processes that use larger amounts of memory. Another reason is that although copy-on-write is invisible and a clever optimization, it is not free, and it cannot do magic. When data is modified by either process, which inevitably happens, the affected pages fault.

但是,由于写时复制,不应假设在类 Unix 系统上创建进程通常是“轻量级”的。它比例如在 Windows 下轻一些,但它远非免费。
一个原因是虽然没有复制实际的页面,但新进程仍然需要一份页表的副本。对于使用大量内存的进程,这可能是几千字节到几兆字节的内存。另一个原因是,虽然写时复制是不可见的并且是一种巧妙的优化,但它不是免费的,也不能变魔术。当数据被任一进程修改时,这不可避免地发生,受影响的页面错误。

Redis is a good example where you can see that forkis everything but lightweight (it uses forkto do background saves).

Redis 是一个很好的例子,你可以看到它fork除了轻量级之外的一切(它用于fork进行后台保存)。

回答by Basile Starynkevitch

The underlying system call to create threads is clone(2)(it is Linux specific). BTW, the list of Linux system calls is on syscalls(2), and you could use the strace(1)command to understand the syscalls done by some process or command. Processes are usually created with fork(2)(or vfork(2), which is not much useful these days). However, you could (and some C standard libraries might do that) create them with some particular form of clone. I guess that the kernel is sharing some code to implement clone, forketc... (since some functionalities, e.g. management of the virtual address space, are common).

创建线程的底层系统调用是clone(2)(它是 Linux 特定的)。顺便说一句,Linux 系统调用列表在syscalls(2) 上,您可以使用strace(1)命令来了解某个进程或命令完成的系统调用。进程通常是用fork(2)(或vfork(2),现在用处不大)创建的。但是,您可以(并且某些 C 标准库可能会这样做)使用某种特定形式的clone. 我猜内核正在共享一些代码来实现clonefork等等......(因为一些功能,例如虚拟地址空间的管理,是常见的)。

Indeed, process creation (and also thread creation) is generally quite fast on most Unix systems (because they use copy-on-writemachinery for the virtual memory), typically a small fraction of a millisecond. But you could have pathological cases (e.g. thrashing) which makes that much longer.

实际上,在大多数 Unix 系统上,进程创建(以及线程创建)通常非常快(因为它们对虚拟内存使用写时复制机制),通常只有一小部分毫秒。但是你可能有病理情况(例如颠簸),这会使时间更长。

Since most C standard libraryimplementations are free softwareon Linux, you could study the source code of the one on your system (often GNU glibc, but sometimes musl-libcor something else).

由于大多数C 标准库实现是Linux上的免费软件,您可以研究系统上的源代码(通常是GNU glibc,但有时是musl-libc或其他东西)。

回答by Yavar

If you are using C here are some of the stuff that might help you:

如果您使用 C,这里有一些可能对您有所帮助的东西:

Fork - For creating process: http://en.wikipedia.org/wiki/Fork_(operating_system)

Fork - 用于创建过程:http: //en.wikipedia.org/wiki/Fork_(operating_system)

Pthreads Library for threads: http://en.wikipedia.org/wiki/POSIX_Threads

线程的 Pthreads 库:http: //en.wikipedia.org/wiki/POSIX_Threads

If you are into C++ you can check out Boost Threads Library for threading.

如果您使用 C++,您可以查看 Boost Threads Library 进行线程处理。

A classic example where multi process technology was considered more suitable than multi threaded is Google Chrome Browser.

多进程技术被认为比多线程更适合的一个经典例子是谷歌 Chrome 浏览器。