fork和thread之间有什么区别?

时间:2023-01-20 17:36:40

Can anyone explain the difference between a fork and a thread?

有人能解释一下叉子和线的区别吗?

5 个解决方案

#1


74  

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 variables and semaphores) that allow you to avoid data corruption.

一个进程可以有多个线程,每个线程在进程的同一上下文中并行执行。内存和其他资源在线程之间共享,因此必须通过一些原始和同步对象(如互斥体、条件变量和信号量)访问共享数据,以避免数据损坏。

#2


65  

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 it’s own memory. Parent process creates a separate address space for 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 not 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 it’s own unique process ID.
  • 子进程将拥有自己的唯一进程ID。
  • The child process shall have it’s own copy of 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 it’s own copy of message queue descriptors of the parents.
  • 子进程应该有它自己的父进程消息队列描述符的副本。
  • Child will have it’s own address space and memory.
  • 子程序将拥有自己的地址空间和内存。

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 remains (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.

线程是轻量级进程(LWPs)。传统上,线程只是一个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.

更多细节可以在这里找到。

#3


27  

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的默认线程实现不使用真正的OS /内核线程。相反,它通过在单个内核线程/进程内的线程对象之间切换来模拟具有多个线程的情况。

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.

祝你好运,玩得开心!多线程处理既具有挑战性又有价值。

#4


5  

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中使用。

#5


4  

  1. Threads share the address space of the process that created it; processes have their own address space.
  2. 线程共享创建进程的地址空间;进程有自己的地址空间。
  3. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  4. 线程可以直接访问进程的数据段;进程有自己的父进程的数据段副本。
  5. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  6. 线程可以直接与进程的其他线程通信;进程必须使用进程间通信与同级进程通信。
  7. Threads have almost no overhead; processes have considerable overhead.
  8. 线程几乎没有开销;过程有相当大的开销。
  9. New threads are easily created; new processes require duplication of the parent process.
  10. 很容易创建新线程;新的进程需要父进程的重复。
  11. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
  12. 线程可以对同一进程的线程执行相当大的控制;进程只能对子进程进行控制。
  13. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes
  14. 对主线程的更改(取消、优先级更改等)可能会影响进程中其他线程的行为;对父进程的更改不会影响子进程

#1


74  

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 variables and semaphores) that allow you to avoid data corruption.

一个进程可以有多个线程,每个线程在进程的同一上下文中并行执行。内存和其他资源在线程之间共享,因此必须通过一些原始和同步对象(如互斥体、条件变量和信号量)访问共享数据,以避免数据损坏。

#2


65  

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 it’s own memory. Parent process creates a separate address space for 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 not 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 it’s own unique process ID.
  • 子进程将拥有自己的唯一进程ID。
  • The child process shall have it’s own copy of 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 it’s own copy of message queue descriptors of the parents.
  • 子进程应该有它自己的父进程消息队列描述符的副本。
  • Child will have it’s own address space and memory.
  • 子程序将拥有自己的地址空间和内存。

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 remains (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.

线程是轻量级进程(LWPs)。传统上,线程只是一个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.

更多细节可以在这里找到。

#3


27  

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的默认线程实现不使用真正的OS /内核线程。相反,它通过在单个内核线程/进程内的线程对象之间切换来模拟具有多个线程的情况。

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.

祝你好运,玩得开心!多线程处理既具有挑战性又有价值。

#4


5  

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中使用。

#5


4  

  1. Threads share the address space of the process that created it; processes have their own address space.
  2. 线程共享创建进程的地址空间;进程有自己的地址空间。
  3. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  4. 线程可以直接访问进程的数据段;进程有自己的父进程的数据段副本。
  5. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  6. 线程可以直接与进程的其他线程通信;进程必须使用进程间通信与同级进程通信。
  7. Threads have almost no overhead; processes have considerable overhead.
  8. 线程几乎没有开销;过程有相当大的开销。
  9. New threads are easily created; new processes require duplication of the parent process.
  10. 很容易创建新线程;新的进程需要父进程的重复。
  11. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
  12. 线程可以对同一进程的线程执行相当大的控制;进程只能对子进程进行控制。
  13. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes
  14. 对主线程的更改(取消、优先级更改等)可能会影响进程中其他线程的行为;对父进程的更改不会影响子进程