当堆栈溢出时,操作系统如何防止自身崩溃?

发布于 2024-12-06 06:17:30 字数 49 浏览 1 评论 0原文

当我的程序之一意外泄漏内存或堆栈溢出时,操作系统使用什么方法来防止崩溃或不稳定行为?

What methods do operating systems use to prevent crashes or erratic behavior when one of my programs accidentally leaks memory or stack overflow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2024-12-13 06:17:30

简而言之:内存管理。

通常,每个进程都会分配有限的(但通常是可调整的)堆栈空间,因此单个进程不会使用足够的堆栈空间来导致整个系统出现问题。

如果一个进程试图访问为其分配的内存之外的内存,那么(最坏的情况下)会导致进程本身崩溃;这会释放分配给该进程的资源,而不会占用其他进程。

Briefly: Memory management.

Typically each process is allocated a limited (but usually adjustable) amount of stack space, so a single process can't use up enough to cause problems for the system as a whole.

And if a process attempts to access memory outside what's been allocated for it, that will (at worst) crash the process itself; this frees up the resources allocated for that process without stepping on other processes.

诗化ㄋ丶相逢 2024-12-13 06:17:30

操作系统通常不能防止程序中的内存泄漏;但是一旦你的应用程序结束,它的所有内存都会被回收。如果您的应用程序永远不会结束,那么操作系统最终会在内存不足时陷入困境。

关于堆栈溢出,它们可以检测到您已经超出了堆栈大小。一种可能性是将堆栈后的几页标记为受保护的内存,如果您尝试访问它,则会出现段错误,并且您的程序将被终止。

OSes don´t generally protect from memory leaks in your program; but once your application ends all its memory is reclaimed. If your application never ended, then the OS would eventually get into trouble when it runs out of memory.

Regarding stack overflows, they can detect that you have gone through your stack size. A posibility is to flag a few pages after the stack as protected memory, if you try to access it then you will get a segfault and your program will be terminated.

迟月 2024-12-13 06:17:30

非常好的问题,谢谢您的提问。我可以立即想到三个问题。并且,对于每个问题,都有两种情况。

堆栈溢出:如果您的程序不是用汇编语言编写的,则操作系统可以检测堆栈溢出,因为所有堆栈操作都是软件操作。运行时系统管理软件堆栈并知道何时发生溢出。

如果您不厌其烦地用汇编语言编写程序,并且错误地弹出了硬件堆栈,那么操作系统无法拯救您。不好的事情可能会发生。

越界内存访问:当您的 C++ 程序启动时,操作系统会代表您将内存边界设置到 CPU 中。如果您的程序尝试访问这些边界之外的内存,CPU 会引发硬件中断。操作系统在处理中断时可以告诉您程序出现了异常行为。例如,当您尝试取消引用 NULL 指针时,就会发生这种情况。

不过,您的汇编语言程序可以尝试从任何内存中读取或写入。如果您的程序是礼貌的并且是由操作系统以通常的方式启动的,那么操作系统可以捕获该错误。但是,如果您的程序是邪恶的并且以某种方式在操作系统的权限之外启动,那么它可能会造成一些真正的损害。

内存泄漏:抱歉,这里没有人可以帮助您。

Very good question, thanks for asking. There are three issues that I can think of off the bat. And, for each issue, there are two cases.

Stack overflows: If your program is written in anything but assembly language, the OS can detect stack overflow because all stack operations are software operations. The run-time system manages the software stack and knows when overflow happens.

If you have taken the trouble to write your program in assembly language and you pop the hardware stack in error, well, the OS can't save you. Bad things can happen.

Out-of-bounds memory accesses: When your C++ program starts, the OS sets memory bounds on your behalf into the CPU. If your program tries to access memory outside those bounds, the CPU raises a hardware interrupt. The OS, as it handles the interrupt, can tell you that your program has misbehaved. This is what happens when you try to dereference a NULL pointer, for example.

Your assembly-language program, though, can try to read or write from/into whatever memory it feels like. If your program is polite and was started by the OS in the usual way, then the OS can catch that error. But if your program is evil and somehow started outside the purview of the OS, it can do some real damage.

Memory Leaks: Sorry, nobody can help you here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文