C语言 fork 和 thread 和有什么不一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2483041/
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
What is the difference between fork and thread?
提问by Pavunkumar
Can anyone explain the difference between a fork and a thread?
谁能解释叉子和线程之间的区别?
回答by Dacav
A fork gives you a brand new process, which is a copy of the current process, with the same code segments. As the memory image changes (typically this is due to different behavior of the two processes) you get a separation of the memory images (Copy On Write), however the executable code remains the same. Tasks do not share memory unless they use some Inter Process Communication (IPC)primitive.
fork 给你一个全新的进程,它是当前进程的副本,具有相同的代码段。随着内存映像的变化(通常这是由于两个进程的不同行为),您会分离内存映像(写时复制),但可执行代码保持不变。任务不共享内存,除非它们使用某些进程间通信 (IPC)原语。
One process can have multiple threads, each executing in parallel within the same context of the process. Memory and other resources are shared among threads, therefore shared data must be accessed through some primitive and synchronization objects (like mutexes, condition variablesand semaphores) that allow you to avoid data corruption.
一个进程可以有多个线程,每个线程在进程的同一上下文中并行执行。内存和其他资源在线程之间共享,因此必须通过一些原始和同步对象(如互斥体、条件变量和信号量)访问共享数据,以避免数据损坏。
回答by Napster_X
Fork
叉子
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 its own memory. The parent process creates a separate address space for the child. Both parent and child process possess the same code segment, but execute independently from each other.
Fork 只不过是一个新进程,它看起来与旧进程或父进程完全一样,但它仍然是一个不同的进程,具有不同的进程 ID 并拥有自己的内存。父进程为子进程创建一个单独的地址空间。父进程和子进程拥有相同的代码段,但彼此独立执行。
The simplest example of forking is when you run a command on shell in Unix/Linux. Each time a user issues a command, the shell forks a child process and the task is done.
最简单的分叉示例是在 Unix/Linux 中的 shell 上运行命令。每次用户发出命令时,shell 都会派生一个子进程并完成任务。
When a fork system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process, but in certain cases, this is not needed. Like in ‘exec' system calls, there is no need to copy the parent process pages, as execv replaces the address space of the parent process itself.
当发出 fork 系统调用时,会创建与父进程对应的所有页面的副本,并由操作系统为子进程加载到单独的内存位置,但在某些情况下,这不是必需的。就像在 'exec' 系统调用中一样,不需要复制父进程页,因为 execv 替换了父进程本身的地址空间。
Few things to note about forking are:
关于分叉需要注意的几点是:
- The child process will be having its own unique process ID.
- The child process shall have its own copy of the parent's file descriptor.
- File locks set by parent process shall not be inherited by child process.
- Any semaphores that are open in the parent process shall also be open in the child process.
- Child process shall have its own copy of the parent's message queue descriptors.
- Child will have its own address space and memory.
- 子进程将拥有自己唯一的进程 ID。
- 子进程应该有它自己的父文件描述符的副本。
- 父进程设置的文件锁不能被子进程继承。
- 在父进程中打开的任何信号量也应在子进程中打开。
- 子进程应该有它自己的父消息队列描述符的副本。
- 孩子将拥有自己的地址空间和内存。
Threads
线程
Threads are Light Weight Processes (LWPs). Traditionally, a thread is just a CPU (and some other minimal state) state with the process containing the rest (data, stack, I/O, signals). Threads require less overhead than “forking” or spawning a new process because the system does not initialize a new system virtual memory space and environment for the process. While most effective on a multiprocessor system where the process flow can be scheduled to run on another processor thus gaining speed through parallel or distributed processing, gains are also found on uniprocessor systems which exploit latency in I/O and other system functions which may halt process execution.
线程是轻量级进程 (LWP)。传统上,线程只是一个 CPU(和其他一些最小状态)状态,进程包含其余部分(数据、堆栈、I/O、信号)。线程需要的开销比“分叉”或产生新进程少,因为系统不会为进程初始化新的系统虚拟内存空间和环境。虽然在多处理器系统上最有效,其中进程流可以被安排在另一个处理器上运行,从而通过并行或分布式处理获得速度,但在利用 I/O 延迟和其他可能停止进程的系统功能的单处理器系统上也发现了收益执行。
Threads in the same process share:
同一进程中的线程共享:
- process instructions
- most data
- open files (descriptors)
- signals and signal handlers
- current working directory
- user and group id
- 流程说明
- 大多数数据
- 打开文件(描述符)
- 信号和信号处理程序
- 当前工作目录
- 用户和组 ID
More details can be found here.
可以在此处找到更多详细信息。
回答by Sam Post
Dacav's answer is excellent, I just wanted to add that not all threading models give you true multi-processing.
Dacav 的回答非常好,我只想补充一点,并非所有线程模型都能为您提供真正的多处理。
For example, Ruby's default threading implementation doesn't use true OS / kernel threads. Instead it mimics having multiple threads by switching between the Thread objects within a single kernel thread / process.
例如,Ruby 的默认线程实现不使用真正的操作系统/内核线程。相反,它通过在单个内核线程/进程内的 Thread 对象之间切换来模拟具有多个线程。
This is important on multiprocessor / multi-core systems, because these types of lightweight threads can only run on a single core - you don't get much in the way of performance boost from having multiple threads.
这在多处理器/多核系统上很重要,因为这些类型的轻量级线程只能在单核上运行——多线程对性能提升的影响不大。
The other place this makes a difference is when one thread blocks (waiting on I/O or calling a driver's IOCTL), all Threads block.
另一个不同的地方是当一个线程阻塞(等待 I/O 或调用驱动程序的 IOCTL)时,所有线程都会阻塞。
This isn't very common nowadays - most threading implementations use kernel threads which don't suffer from these issues - but its worth mentioining for completeness.
这在当今并不常见 - 大多数线程实现使用不受这些问题影响的内核线程 - 但为了完整性值得一提。
By contrast, fork gives you another process which is runnable simultaneously on another physical CPU while the original process is executing. Some people find IPC more suitable for their app, others prefer threading.
相比之下,fork 为您提供了另一个进程,该进程可以在原始进程执行时同时在另一个物理 CPU 上运行。有些人发现 IPC 更适合他们的应用程序,其他人则更喜欢线程。
Good luck and have fun! Multi-threading is both challenging and rewarding.
祝好运并玩得开心点!多线程既具有挑战性又有益。
回答by sergio
Threads are functions run in parallel, fork is a new process with parents inheritance. Threads are good to execute a task in parallel, while forks are independent process, that also are running simultaneously. Threads have race conditions and there controls semaphores and locks or mutexes, pipes can both be used in fork and thread.
线程是并行运行的函数,fork 是一个具有父级继承的新进程。线程很适合并行执行任务,而 fork 是独立的进程,它们也同时运行。线程具有竞争条件,并控制信号量和锁或互斥体,管道可以在 fork 和 thread 中使用。

