I am studying on OS memory management, and If only to ensure which i got the fundamental mechanism of allocation virtual memory paging straight.
Let us say a procedure calls malloc(), what goes on behind the curtain? my answer: The runtime library finds an properly sized block of memory in the virtual memory address space. (This is when allocation calculations for example first-fit, best-fit that cope with fragmentation come up)
Now let us the process accesses that memory, how's so now? my answer: The memory address, as seen through the process, is actually virtual. The OS inspections in the event that address is presently planned to some physical memory address and when so works the access. Whether it is not planned - a webpage fault is elevated.
Shall We Be Held getting this straight? i.e. the compilerruntime library are responsible for allocating virtual memory blocks, and also the OS manages a mapping between processes' virtual address and physical addresses (and also the paging formula that entails)?
About right. The memory must appear in the virtual memory from the process for any page fault to really allocate an actual page though. You cannot just start poking around anywhere and expect the kernel to place physical memory in which you occur to access.
There's a lot more into it than this. Educate yourself on mmap(), anonymous and never, shared and. And brk() too. malloc() develops brk() and mmap().
You've almost first got it. The main one factor you skipped is how the procedure asks the machine for additional virtual memory to begin with. As Thomas stated, you cannot just write in which you want. There is no reason an OS could not be made to allow that, but it is a lot more efficient whether it has an idea where you are likely to be writing and also the space where you're doing so is contiguous.
On Unixy systems, userland processes possess a region known as the data segment, that is what it really seems like: it's in which the data goes. Whenever a process needs memory for data, it calls
brk(), which asks the machine to increase the information segment to some specified pointer value. (For instance, in case your existing data segment was empty and also you desired to extend it to 2M, you'd call
Observe that while common,
brk() isn't a standard actually it had been yanked from POSIX.1 about ten years ago because C identifies
malloc() and there is pointless to mandate the interface for data segment allocation.