操作系统内部究竟发生了什么导致分段错误 [英] What exactly happens inside the OS to cause the segmentation fault

查看:21
本文介绍了操作系统内部究竟发生了什么导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于虚拟地址和分页的信息.让我先告诉大家我的理解.当一个进程想要执行某事时,它会尝试将数据从硬盘加载到内存中.为此,它使用虚拟地址.所以我们的 MMU 验证虚拟地址查找 TLB 以找到相应的物理页面,如果它没有找到它会查找 Inverted Page Table,如果它没有找到那里的条目,最后它会查找页表它会产生页面错误,并且所有页面交换都已完成,并且所有表都将被更新.当我阅读时,所有进程都有不同的页表和相同的虚拟地址.因此,如果我尝试访问定义为 int a[100] 的数组元素 a[1000],我确信会出现分段错误,因为该指令可能正在尝试访问不属于它的内存.但是操作系统如何通过仅使用虚拟地址和物理页面的概念来知道 a[1000] 不属于正在运行的进程.我在这里遗漏了什么还是我的整个理解都错了?

I have read a lot about the virtual address and paging. Let me first tell you people what i understood. When a process wants to execute something it tries to load the data from the hard disk to memory. To do this it uses a virtual address. So our MMU validates the virtual address looks into the TLB to find the corresponding physical page, if it doesn't find there it looks into Inverted Page Table and at the end it looks into Page table if it doesn't find an entry over there it generates a page fault and all the swapping of page is done and all the tables will be updated. And as I read all the processes have different page tables and same virtual address. so if I try access an array element a[1000] which was defined as int a[100] I am sure that am gonna get a segmentation fault cause that instruction might be trying to access a memory that doesn't belong to it. but how OS comes to know that a[1000] doesn't belong to the running process by just using the concept of virtual address and physical pages. Am I missing something here or my entire understanding is wrong?

我知道如果进程试图访问只读或支持真实内存段,我们可以说内存访问是非法的.

I know we can say a memory access is illegal if a process is trying to access a read only or sup true memory segment.

最后,沸腾的问题是操作系统如何决定将哪些内存分配给哪个进程以及它如何决定这种内存访问是非法的.

at the end the boiling question is how OS decides which memory is allocated to which process and how it decides that this access of memory is illegal.

什么是 Linux 上的分段错误?

这个链接没有多大帮助.

this link didn't help much .

非常感谢各位可爱的人的投入:)

thanks a lot in advance for all you lovely people's inputs :)

推荐答案

在x86上,linux使用了分段和分页的组合,所以程序生成的地址首先查找对应的段基址和限位寄存器值.这给出了虚拟地址,然后使用页表进行转换.当您尝试访问尚未分配的内存时,访问的页面超出了限制寄存器允许的范围,从而产生了分段错误.

On x86, linux uses a combination of segmentation and paging, so the address generated by program first looks up for the corresponding segments base and limit registers values. This gives the virtual address which is then translated using the page table. When you try to access a memory which has not been allocated, the accessed page is beyond what the limit register allows, hence generating a segmentation fault.

这篇关于操作系统内部究竟发生了什么导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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