fork 和 thread 和有什么不一样? [英] What is the difference between fork and thread?

查看:28
本文介绍了fork 和 thread 和有什么不一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 fork 和 thread 的区别?

解决方案

一个 fork 给你一个全新的进程,它是当前进程的副本,具有相同的代码段.随着内存映像的变化(通常这是由于两个进程的不同行为),您会分离内存映像(写时复制),但可执行代码保持不变.任务不会共享内存,除非它们使用一些进程间通信 (IPC) 原语.>

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

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

解决方案

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.

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.

这篇关于fork 和 thread 和有什么不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆