除了malloc/free之外,程序还需要OS提供其他功能吗? [英] Other than malloc/free does a program need the OS to provide anything else?

查看:89
本文介绍了除了malloc/free之外,程序还需要OS提供其他功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在开发的OS设计内核(实际上我将其称为核心",但本质上是相同的).如果我无法启动并运行多任务,内存管理和其他基本功能,则操作系统本身的细节无关紧要,因此我需要首先进行处理.我有一些关于设计malloc例程的问题.

I'm working on designing the kernel (which I'm going to actually call the "core" just to be different, but its basically the same) for an OS I'm working on. The specifics of the OS itself are irrelevant if I can't get multi-tasking, memory management, and other basic things up and running, so I need to work on that first. I've some questinos about designing a malloc routine.

我认为malloc()将成为内核本身的一部分(我倾向于此)或程序的一部分,但是我将不得不编写自己的C实现.标准库的任何一种方式,所以我要编写一个malloc.在这方面,我的问题实际上很简单,C(或C ++)如何管理其堆?

I figure that malloc() is either going to be a part of the kernel itself (I'm leaning towards this) or a part of the program, but I'm going to have to write my own implementation of the C standard library either way, so I get to write a malloc. My question is actually rather simple in this regard, how does C (or C++) manage its heap?

我在理论课中一直被教导的是,堆是一块不断扩展的内存,从指定的地址开始,并且在很多方面表现得像堆栈.通过这种方式,我知道在全局范围内声明的变量才是开头,并且在它们各自的范围内声明它们时,更多的变量会被压入"堆中,而超出范围的变量只会保留在内存空间中,但是该空间被标记为可用空间,因此堆可以根据需要扩展更多的空间.

What I've always been taught in theorey classes is that the heap is an ever expanding piece of memory, starting at a specified address, and in a lot of senses behaving like a stack. In this way, I know that variables declared in global scope are at the beginning, and more variables are "pushed" onto the heap as they are declared in their respective scopes, and variables that go out of scope are simply left in memory space, but that space is marked as free so the heap can expand more if it needs to.

我需要知道的是,C实际上如何以这种方式处理动态扩展的堆?编译后的C程序是对malloc例程进行自己的调用并处理其自己的堆,还是需要为其提供自动扩展的空间?另外,C程序如何知道堆从哪里开始?

What I need to know is, how on earth does C actually handle a dynamically expanding heap in this manner? Does a compiled C program make its own calls to a malloc routine and handle its own heap, or do I need to provide it with an automatically expanding space? Also, how does the C program know where the heap begins?

哦,我知道相同的概念也适用于其他语言,但是我希望任何示例都可以使用C/C ++,因为我对这种语言最满意.我也不想担心诸如堆栈之类的其他事情,因为我认为我能够自己处理类似的事情.

Oh, and I know that the same concepts apply to other languages, but I would like any examples to be in C/C++ because I'm most comfortable with that language. I also would like to not worry about other things such as the stack, as I think I'm able to handle things like this on my own.

所以我想我真正的问题是,除了malloc/free(它自己处理获取和释放页面等)之外,程序是否还需要操作系统提供其他功能?

So I suppose my real question is, other than malloc/free (which handles getting and freeing pages for itself, etc) does a program need the OS to provide anything else?

谢谢!

EDIT 与malloc例程本身的实际工作方式相比,我对C如何在堆中使用malloc更加感兴趣.如果有帮助,我可以在x86上执行此操作,但是C是交叉编译器,因此无关紧要. ^ _ ^

EDIT I'm more interested in how C uses malloc in relation with the heap than in the actual workings of the malloc routine itself. If it helps, I'm doing this on x86, but C is cross compiler so it shouldn't matter. ^_^

进一步我了解我可能对术语感到困惑.我被告知,堆"是程序存储诸如全局/局部变量之类的东西的地方.我习惯于在汇编编程中处理堆栈",而我只是意识到我可能是故意的意思.我的一些研究表明,堆"更常用于指程序为其分配的总内存,或操作系统提供的内存页面的总数(和顺序).

EDIT FURTHER: I understand that I may be getting terms confused. I was taught that the "heap" was where the program stored things like global/local variables. I'm used to dealing with a "stack" in assembly programming, and I just realized that I probably mean that instead. A little research on my part shows that "heap" is more commonly used to refer to the total memory that a program has allocated for itself, or, the total number (and order) of pages of memory the OS has provided.

因此,考虑到这一点,我该如何应对不断扩展的 stack ? (看来我的C理论课确实有轻微...有缺陷.)

So, with that in mind, how do I deal with an ever expanding stack? (it does appear that my C theory class was mildly... flawed.)

推荐答案

malloc通常在用户空间的C运行时中实现,它依赖于特定的OS系统调用来映射到虚拟内存的页面中. mallocfree的工作是管理固定大小(通常为4 KB,但有时更大)的那些内存页面,并将它们切成小块,供应用程序使用.

malloc is generally implemented in the C runtime in userspace, relying on specific OS system calls to map in pages of virtual memory. The job of malloc and free is to manage those pages of memory, which are fixed in size (typically 4 KB, but sometimes bigger), and to slice and dice them into pieces that applications can use.

例如,参见 GNU libc 实现.

要实现更简单的操作,请从最后查看 MIT操作系统类年.具体来说,请参见最终实验室讲义 ,然后查看lib/malloc.c.该代码使用在该类中开发的操作系统JOS.它的工作方式是读取页表(操作系统提供只读),查找未映射的虚拟地址范围.然后,它使用sys_page_allocsys_page_unmap系统调用将页面映射和取消映射到当前进程.

For a much simpler implementation, check out the MIT operating systems class from last year. Specifically, see the final lab handout, and take a look at lib/malloc.c. This code uses the operating system JOS developed in the class. The way it works is that it reads through the page tables (provided read-only by the OS), looking for unmapped virtual address ranges. It then uses the sys_page_alloc and sys_page_unmap system calls to map and unmap pages into the current process.

这篇关于除了malloc/free之外,程序还需要OS提供其他功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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