操作系统中的共享内存段 [英] Shared Memory Segment in Operating System

查看:527
本文介绍了操作系统中的共享内存段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共享内存属于哪里?这意味着它由堆栈和堆之类的每个单独进程拥有.因此,其他程序无法 能够访问某些其他程序的堆栈.还是它是一个公共的内存段,可用于任何数量的进程.这 下图以图解方式显示了我的问题.

Where is shared memory belongs to ? Which means it is owned by each individual process like stack and heap. So, other program cannot able to access the stack of some other program. Or it is a common segment of memory which is used by any number of process. The below figure shows my question diagramatically.

图1:

    -----------------       -----------------       -----------------    
    |    stack      |       |    stack      |       |    stack      |
    |               |       |               |       |               |
    |  Shared m/y   |   --->|  Shared m/y   |<---   |  Shared m/y   |
    |               |   |   |               |   |   |               |
    |     heap      |   |   |    Heap       |   |   |    Heap       |
    |               |   |   |               |   |   |               |
    |  Data segment |   |   | Data segment  |   |   | Data segment  |
    |               |   |   |               |   |   |               |
    |      text     |___|   |      text     |   |___|   text        | 
    -----------------       -----------------       -----------------
        Process 1                Process 2               Process 3

(OR)

图2:

                -----------------------------------------
                |                                        |
                |                                        |
                |          Shared Memory                 |<--
        ------->|                                        |  |
        |       |                                        |  |
        |       -----------------------------------------   |
        |                           ^                       |
        |                           |                       |  
    -----------------       -----------------       ----------------- 
    |    stack      |       |    stack      |       |    stack      |
    |               |       |               |       |               |
    |     heap      |       |    Heap       |       |    Heap       |
    |               |       |               |       |               |
    |  Data segment |       | Data segment  |       | Data segment  |
    |               |       |               |       |               |
    |      text     |       |      text     |       |   text        | 
    -----------------       -----------------       -----------------
       Process 1               Process 2                Process 3

在图1中,每个进程在一个进程的地址空间内都有一部分共享内存.在进程2和3的共享内存上,进程1和进程3对其进行访问.在图2中,共享内存 是所有进程都可以访问的内存段.因此,在以上两种情况下,进程用于共享内存 部分.

In Figure 1, Each process have a segment of shared memory within the address space of a process. On that shared memory of process 2 is accessed by process 1 and process 3. In Figure 2, the shared memory is a segment of memory which is accessed by all the processes. So, in above two scenario, which is used by process for shared memory segment.

推荐答案

我怀疑您的部分困惑来自术语.在64位之前的Intel中,数据是按进行组织的.链接器中还使用了术语来描述程序中数据的组装方式(32位Intel中的数据可能映射到硬件段).但是,对于您的问题,您应该删除术语 segment .

I suspect part of your confusion is from terminology. In pre-64-bit Intel, data was organized in segments. The term segment is also used in linkers to describe how data is assembled in a program (that in 32-bit Intel may be mapped to hardware segments). However, for you question, you should eliminate the term segment.

在Intel 64位系统和大多数非Intel系统中,通常的工作方式是将物理内存划分为某些固定大小(例如4K,1K)的PAGE FRAMES. CPU的内存管理单元在相同大小的PAGES上运行.操作系统为由页面组成的每个进程设置线性逻辑地址空间.操作系统使用PAGE TABLE将逻辑进程地址空间的页面映射到物理页面框架.

The general way things work in Intel 64-bit and most non-Intel systems is that the physical memory is divided into PAGE FRAMES of some fixed size (e.g. 4K, 1K). The memory management unit of the CPU operates on PAGES of the same size. The operating system sets up a linear logical address space for each process consisting of pages. The operating system maps the pages of the logical process address space to physical page frames using a PAGE TABLE.

每个进程运行时,都会看到其自己的逻辑地址空间,该地址空间的范围为0到任意值.每个进程的地址空间中的页面都映射到物理页面框架,并且内存管理单元使用页面表自动将使用页面的逻辑地址转换为使用页面框架的物理内存地址.

When each process runs it sees its own logical address space with addresses in the range 0 to whatever. The pages within each process's address space are mapped to physical page frames and the Memory Management Unit does the translation from logical addresses using pages to physical memory addresses using page frames automatically using the page table(s).

此系统可防止每个进程与其他进程混淆.通常,如果进程X访问逻辑地址Q,而进程Y访问逻辑地址Q,则它们将访问不同的物理内存位置,因为它们的页表将具有不同的映射.

This system keeps each process from messing with other processes. Generally, if Process X accesses logical address Q and Process Y accesses logical address Q, they will be accessing different physical memory locations because their page tables will have different mappings.

我知道的每个系统都使用逻辑内存转换,能够将多个进程将逻辑页面映射到同一物理页面框架:共享内存.流程可以使用这种机制来快速交换数据(以管理与该数据的同步为代价).

Every system I am aware of that uses logical memory translation has the ability to for multiple processes to map a logical page(s) to the same physical page frame(s): shared memory. Processes can use this mechanism to quickly exchange data (at the cost of having manage the synchronization to that data).

在这种类型的共享中,物理页面框架不必映射到相同的逻辑地址(通常不是).

In this type of sharing, the physical page frame does not have to be mapped to the same logical address (and usually is not).

进程X可以将页面框架P映射到页面A,同时 流程Y可以将页面框架P映射到页面B

Process X can map page frame P to page A while Process Y can map page frame P to page B

还有另一种形式的共享内存,通常以完全不同的方式实现.处理器或操作系统(在某些处理器上)为系统地址空间定义了一系列逻辑地址.所有进程的地址范围都是相同的,并且该范围内所有进程的逻辑地址到物理页面帧的映射都相同.

There is another form of shared memory that usually is implemented quite differently. The processor or operating system (on some processors) defines a range of logical addresses for a System Address Space. This range of addresses is the same for all processes and all processes have the same mapping of logical addresses to physical page frames in this range.

系统地址受保护,因此只能在内核模式下访问,因此进程之间不会发生冲突.

The System address is protected so that it can only be accessed in Kernel mode so processes cannot muck with each other.

请考虑页面而不是细分.

Think in terms of pages rather than segments here.

这篇关于操作系统中的共享内存段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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