进程VS线程:两个进程可以共享相同的共享内存吗?两个线程可以吗? [英] Process VS thread : can two processes share the same shared memory ? can two threads ?

查看:39
本文介绍了进程VS线程:两个进程可以共享相同的共享内存吗?两个线程可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在思考了共享内存的整个概念之后,提出了一个问题:

After thinking about the the whole concept of shared memory , a question came up:

两个进程可以共享同一个共享内存段吗?两个线程可以共享同一个共享内存吗?

can two processes share the same shared memory segment ? can two threads share the same shared memory ?

仔细考虑之后,我几乎肯定两个进程可以共享同一个共享内存段,其中第一个是父亲,第二个是儿子,这是用 fork 创建的() ,但是两个线程呢?

After thinking about it a little more clearly , I'm almost positive that two processes can share the same shared memory segment , where the first is the father and the second is the son , that was created with a fork() , but what about two threads ?

谢谢

推荐答案

两个进程可以共享同一个共享内存段吗?

can two processes share the same shared memory segment?

是和不是.通常在现代操作系统中,当另一个进程从第一个开始forked 时,它们共享在所有页面上设置 copy-on-write 的相同内存空间.对任何读写内存页面进行的任何更新都会为该页面制作一个副本,因此将有两个副本,并且该内存页面将不再在父进程和子进程之间共享.这意味着只有只读页面或尚未写入的页面将被共享.

Yes and no. Typically with modern operating systems, when another process is forked from the first, they share the same memory space with a copy-on-write set on all pages. Any updates made to any of the read-write memory pages causes a copy to be made for the page so there will be two copies and the memory page will no longer be shared between the parent and child process. This means that only read-only pages or pages that have not been written to will be shared.

如果一个进程没有从另一个进程分叉,那么它们通常不共享任何内存.一个例外是,如果您正在运行同一程序的两个实例,那么它们可能会共享 代码,甚至可能是静态的数据段,但不会共享其他页面.另一个是一些操作系统如何允许应用程序共享由多个应用程序加载的动态库的代码页.

If a process has not been forked from another then they typically do not share any memory. One exception is if you are running two instances of the same program then they may share code and maybe even static data segments but no other pages will be shared. Another is how some operating systems allow applications to share the code pages for dynamic libraries that are loaded by multiple applications.

还有特定的内存映射调用来共享同一个内存段.该调用指定映射是只读的还是读写的.如何做到这一点取决于操作系统.

There are also specific memory-map calls to share the same memory segment. The call designates whether the map is read-only or read-write. How to do this is very OS dependent.

两个线程可以共享同一个共享内存吗?

can two threads share the same shared memory?

当然.通常,多线程进程内部的所有内存都是共享"的.除了一些相对较小的每个线程的堆栈空间外,由所有线程执行.这通常是线程的定义,因为它们都在相同的内存空间中运行.

Certainly. Typically all of the memory inside of a multi-threaded process is "shared" by all of the threads except for some relatively small stack spaces which are per-thread. That is usually the definition of threads in that they all are running within the same memory space.

线程的复杂性还在于具有 高速内存中的缓存内存段与处理器/内核相关联.此缓存内存共享,内存页面的更新会根据同步操作刷新到中央存储中.

Threads also have the added complexity of having cached memory segments in high speed memory tied to the processor/core. This cached memory is not shared and updates to memory pages are flushed into central storage depending on synchronization operations.

这篇关于进程VS线程:两个进程可以共享相同的共享内存吗?两个线程可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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