如何在操作系统中加载程序 [英] How is a program loaded in OS

查看:99
本文介绍了如何在操作系统中加载程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关逻辑和物理寻址的内容.我很困惑,当一个二进制文件运行时,它是先通过生成逻辑地址的CPU还是直接复制到物理内存中?

I am reading about logical and physical addressing. I am confused that when a binary file is run, does it pass first through the CPU where the logical address are generated or is it directly copied to the physical memory ?

推荐答案

二进制文件运行时,是先通过CPU生成逻辑地址还是直接拷贝到物理内存?

when a binary file is run, does it pass first through the CPU where the logical address are generated or is it directly copied to the physical memory ?

通常某处的一些代码将可执行文件的头文件加载到内存中,然后使用头文件中的信息来找出文件的各个部分(部分 - 例如 .text.data 等)应该在虚拟内存中,每个虚拟页面的虚拟权限应该是什么(如果允许写入,如果允许执行).

Typically some code somewhere loads the executable file's headers into memory, and then uses information from the headers to figure out where various pieces of the file (sections - e.g. .text, .data, etc) should be in virtual memory and what each virtual page's virtual permissions should be (if writes are allowed, if execution is allowed).

在此之后,虚拟地址空间的区域被设置.通常这是通过将文件的相关部分映射到虚拟地址空间的内存来完成的,而不是实际将它们加载到物理内存中.在这种情况下,每个页面的实际权限并不反映页面的虚拟权限(例如,读/写"页面最初可能不存在",当软件尝试从页面读取时,您会遇到页面错误,并且页面错误处理程序可能会从磁盘获取页面并将页面更改为存在,只读";然后当软件尝试写入页面时,您可能会遇到第二个页面错误并且页面错误处理程序可能会执行复制写",这样使用相同物理页面的任何其他内容都不会受到影响,然后使新副本读/写",使其与原始虚拟权限匹配).

After this, areas of the virtual address space are set up. Often this is done by memory mapping the relevant part of the file into the virtual address space, without actually loading them into physical memory. In this case each page's actual permissions don't reflect the page's virtual permissions (e.g. a "read/write" page might be "not present" initially, and when software tries to read from the page you'll get a page fault and the page fault handler might fetch the page from disk and change the page to "present, read only"; and then later when software tries to write to the page you might get a second page fault and the page fault handler might do a "copy on write" so that anything else using the same physical page isn't effected and then make the new copy "read/write" so that it matches the original virtual permissions).

在发生这种情况时;操作系统可以(取决于可用物理 RAM 的数量以及存储设备是否有更重要的数据要传输)从磁盘预取文件数据(例如到 VFS 缓存中),并且可以机会性地"更新进程的页表以避免已预取页面的页面错误开销.

While this is happening; the OS could (depending on amount of free physical RAM and whether storage devices have more important data to transfer) be prefetching the file data from disk (e.g. into VFS cache), and could be "opportunistically" updating the process' page tables to avoid the overhead of page faults for pages that have been prefetched.

然而;如果操作系统知道文件位于不可靠和/或可移动介质上,它可能会认为使用内存映射文件是一个坏主意,并且可能会在执行之前将所需的可执行文件部分加载到内存中;并且操作系统可能具有导致文件在执行之前被加载到 RAM 的其他功能(例如,如果操作系统在允许执行文件之前检查可执行文件的数字签名是否正确,则可能需要加载整个文件存入内存以允许检查数字签名,在这种情况下,当设置虚拟地址空间时,整个文件可能仍在内存中).

However; if the OS knows that the file was on unreliable and/or removable media it may decide that using memory mapped files is a bad idea and may actually load the needed executable's sections into memory before executing it; and an OS could have other features that cause the file to be loaded into RAM before it's executed (e.g. if the OS checks that an executable file's digital signature is correct before allowing the file to be executed, then the entire file probably needs to be loaded into memory to allow the digital signature can be checked, and in that case the entire file is likely to still be in memory when virtual address space is set up).

这篇关于如何在操作系统中加载程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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