Linux 分叉与线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16354460/
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 22:50:47  来源:igfitidea点击:

Forking vs Threading

linuxmultithreadingunixprogramming-languagesfork

提问by Rushabh RajeshKumar Padalia

I have used threading before in my applications and know its concepts well, but recently in my operating system lecture I came across fork(). Which is something similar to threading.

我以前在我的应用程序中使用过线程并且非常了解它的概念,但最近在我的操作系统讲座中我遇到了 fork()。这类似于线程。

I google searched difference between them and I came to know that:

我用谷歌搜索了它们之间的区别,我才知道:

  1. Fork is nothing but a new process that looks exactly like the old or the parent process but still it is a different process with different process ID and having it's own memory.
  2. Threads are light-weight process which have less overhead
  1. Fork 只不过是一个新进程,它看起来与旧进程或父进程完全一样,但它仍然是一个不同的进程,具有不同的进程 ID 并拥有自己的内存。
  2. 线程是具有较少开销的轻量级进程

But, there are still some questions in my mind.

但是,我心里还是有一些疑问。

  1. When should you prefer fork() over threading and vice-verse?
  2. If I want to call an external application as a child, then should I use fork() or threads to do it?
  3. While doing google search I found people saying it is bad thing to call a fork() inside a thread. why do people want to call a fork() inside a thread when they do similar things?
  4. Is it True that fork() cannot take advantage of multiprocessor system because parent and child process don't run simultaneously?
  1. 什么时候你应该更喜欢 fork() 而不是线程,反之亦然?
  2. 如果我想在小时候调用外部应用程序,那么我应该使用 fork() 还是线程来执行它?
  3. 在进行谷歌搜索时,我发现有人说在线程内调用 fork() 是件坏事。为什么人们在做类似的事情时想要在线程内调用 fork() ?
  4. 因为父进程和子进程不同时运行,fork() 不能利用多处理器系统,这是真的吗?

采纳答案by Niels Keurentjes

The main difference between forking and threading approaches is one of operating system architecture. Back in the days when Unix was designed, forking was an easy, simple system that answered the mainframe and server type requirements best, as such it was popularized on the Unix systems. When Microsoft re-architected the NT kernel from scratch, it focused more on the threading model. As such there is today still a notable difference with Unix systems being efficient with forking, and Windows more efficient with threads. You can most notably see this in Apache which uses the prefork strategy on Unix, and thread pooling on Windows.

分叉和线程方法之间的主要区别在于操作系统架构之一。在 Unix 被设计的时代,fork 是一个简单、简单的系统,最能满足大型机和服务器类型的要求,因此它在 Unix 系统上得到了普及。当微软从头开始重新构建 NT 内核时,它更多地关注线程模型。因此,今天仍然存在显着差异,Unix 系统的分叉效率更高,而 Windows 的线程效率更高。您可以在 Apache 中最明显地看到这一点,它在 Unix 上使用 prefork 策略,在 Windows 上使用线程池。

Specifically to your questions:

具体到您的问题:

When should you prefer fork() over threading and vice-verse?

什么时候你应该更喜欢 fork() 而不是线程,反之亦然?

On a Unix system where you're doing a far more complex task than just instantiating a worker, or you want the implicit security sandboxing of separate processes.

在 Unix 系统上,您要执行的任务远比实例化工作程序复杂得多,或者您希望对单独的进程进行隐式安全沙箱处理。

If I want to call an external application as a child, then should I use fork() or threads to do it?

如果我想在小时候调用外部应用程序,那么我应该使用 fork() 还是线程来执行它?

If the child will do an identical task to the parent, with identical code, use fork. For smaller subtasks use threads. For separate external processes use neither, just call them with the proper API calls.

如果孩子将使用相同的代码执行与父母相同的任务,请使用 fork。对于较小的子任务,使用线程。对于单独的外部进程,两者都不使用,只需使用适当的 API 调用来调用它们。

While doing google search I found people saying it is bad thing to call a fork() inside a thread. why do people want to call a fork() inside a thread when they do similar things?

在进行谷歌搜索时,我发现有人说在线程内调用 fork() 是件坏事。为什么人们在做类似的事情时想要在线程内调用 fork() ?

Not entirely sure but I think it's computationally rather expensive to duplicate a process and a lot of subthreads.

不完全确定,但我认为复制一个进程和许多子线程在计算上相当昂贵。

Is it True that fork() cannot take advantage of multiprocessor system because parent and child process don't run simultaneously?

因为父进程和子进程不同时运行,fork() 不能利用多处理器系统,这是真的吗?

This is false, fork creates a new process which then takes advantage of all features available to processes in the OS task scheduler.

这是错误的,fork 创建一个新进程,然后利用 OS 任务调度程序中进程可用的所有功能。

回答by Brian Agnew

fork()spawns a new copy of the process, as you've noted. What isn't mentioned above is the exec()call which often follows. This replaces the existing process with a new process (a new executable) and as such, fork()/exec()is the standard means of spawning a new process from an old one.

fork()正如您所指出的,会生成该过程的新副本。上面没有提到的是exec()经常跟随的电话。这将用新进程(新的可执行文件)替换现有进程,因此,fork()/exec()是从旧进程生成新进程的标准方法。

e.g. that's how your shell will invoke a process from the command line. You specify your process (ls, say) and the shell forks and then execs ls.

例如,这就是您的 shell 从命令行调用进程的方式。您指定您的进程(ls例如)和shell fork,然后指定 execs ls

Note that this operates at a very different level from threading. Threading runs multiple lines of execution intra-process. Forking is a means of creating newprocesses.

请注意,这与线程在非常不同的级别上运行。线程在进程内运行多行执行。分叉是创建进程的一种手段。

回答by Selvaperumal

A forked process is called a heavy-weight process, whereas a threaded process is called light-weight process.

分叉的进程称为重量级进程,而线程化的进程称为轻量级进程。

The following are the difference between them:

以下是它们之间的区别:

  1. A forked process is considered a child process whereas a threaded process is called a sibling.
  2. Forked process shares no resource like code, data, stack etc with the parent process whereas a threaded process can share code but has its own stack.
  3. Process switching requires the help of OS but thread switching it is not required
  4. Creating multiple processes is a resource intensive task whereas creating multiple thread is less resource intensive task
  5. Each process can run independently whereas one thread can read/write another threads data. Thread and process lectureenter image description here
  1. 分叉的进程被认为是子进程,而线程化的进程被称为兄弟进程。
  2. 分叉进程不与父进程共享代码、数据、堆栈等资源,而线程进程可以共享代码但有自己的堆栈。
  3. 进程切换需要操作系统的帮助,但不需要线程切换
  4. 创建多进程是资源密集型任务,而创建多线程是资源密集型任务
  5. 每个进程可以独立运行,而一个线程可以读/写另一个线程数据。线程和过程讲座在此处输入图片说明