简体   繁体   English

操作系统如何为每个进程生成页表?

[英]How are page tables generated by the operating system, per process?

I know each process has it own virtual address space.我知道每个进程都有自己的虚拟地址空间。 A table is generated per process to map the virtual address to a physical address which resides in physical memory (called pages).每个进程都会生成一个表,将虚拟地址映射到驻留在物理内存中的物理地址(称为页面)。

When trying to access an address say 0x123 , the address is looked up using the table.当尝试访问地址时,例如0x123 ,将使用该表查找该地址。 If there are no translations found then a segfault is generated.如果没有找到翻译,然后一个segfault产生。 Else the translated address is returned.否则返回转换后的地址。 (to keep it simple here I'm not mentioning the role of TLB and page faults). (为了简单起见,我没有提到 TLB 和页面错误的作用)。

My question is, how it this table generated?我的问题是,这张表是怎么生成的? How does it know 0x123 should be mapped to physical memory when my program starts running?当我的程序开始运行时,它如何知道0x123应该映射到物理内存? is it because a linker has already added the required addresses to the binary when linking?是不是因为linkerlinker已经将所需的地址添加到二进制文件中?

Depending on your operating system, compilers and linkers will be written to output certain executable formats.根据您的操作系统,将编写编译器和链接器以输出某些可执行格式。 For Windows machines, it will ouput .exe files.对于 Windows 机器,它将输出 .exe 文件。 For Linux machines, it will ouput elf files which is an open source convention (Executable and Linkable Format).对于 Linux 机器,它将输出 elf 文件,这是一个开源约定(可执行和可链接格式)。

The elf file will contain several segments (code, data, etc) which need to reside at certain positions in the virtual address space to reference each other correctly. elf 文件将包含多个段(代码、数据等),它们需要驻留在虚拟地址空间中的某些位置才能正确地相互引用。

Each process is basically given a whole virtual address space and it is the job of the OS to switch the paging tables and to create them so that the virtual adresses will translate to someplace in RAM that won't bother other processes or the kernel.每个进程基本上都有一个完整的虚拟地址空间,操作系统的工作是切换分页表并创建它们,以便虚拟地址将转换到 RAM 中的某个位置,而不会打扰其他进程或内核。 It gets more complex with multiple processors.使用多个处理器会变得更加复杂。

The OS will map the necessary space depending on the size of the segments referenced in the executable file.操作系统将根据可执行文件中引用的段的大小映射必要的空间。 The OS decides where the process will really end up in physical memory since this will be transparent to the process which will only use virtual adresses.操作系统决定进程在物理内存中的真正结束位置,因为这对仅使用虚拟地址的进程是透明的。

The process can ask for more memory to the OS as well if it does dynamic memory allocations.如果进程进行动态内存分配,它也可以向操作系统请求更多内存。 In this case, it will be written specifically for this OS using its libraries.在这种情况下,它将使用其库专门为此操作系统编写。 There will either be dynamic linking at runtime or static linking.将在运行时进行动态链接或静态链接。 Static linking will add all the necessary code to the executable so it will be freestanding.静态链接会将所有必要的代码添加到可执行文件中,因此它将是独立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM