简体   繁体   中英

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. If there are no translations found then a segfault is generated. Else the translated address is returned. (to keep it simple here I'm not mentioning the role of TLB and page faults).

My question is, how it this table generated? How does it know 0x123 should be mapped to physical memory when my program starts running? is it because a linker has already added the required addresses to the binary when linking?

Depending on your operating system, compilers and linkers will be written to output certain executable formats. For Windows machines, it will ouput .exe files. For Linux machines, it will ouput elf files which is an open source convention (Executable and Linkable Format).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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