Loading programs into memory

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Matt

Loading programs into memory

Post by Matt »

Im kinda wondering, when your program loads another program into the memory to run, how does the kernel know where to put the program in the memory (how do you make sure your not writing over another programs code if you are multitasking)?
[glow=red,2,300]Thanks :D[/glow]
frank

Re:Loading programs into memory

Post by frank »

I think it works like this:


first , after your kernel memory is loaded the (empty) memory at an address. Then the first task gets 100bytes of free memory (for example), the second task the next, the folowing task the third and so on...
Schol-R-LEA

Re:Loading programs into memory

Post by Schol-R-LEA »

Matt wrote: Im kinda wondering, when your program loads another program into the memory to run, how does the kernel know where to put the program in the memory (how do you make sure your not writing over another programs code if you are multitasking)?
[glow=red,2,300]Thanks :D[/glow]
Memory management is a largish subject in it's own right, and can get quite compilcated when combined with virtual memory and such (though VM also simplifies it, in other ways). However, what it boils down to is that the system has to to keep track of what memory is used, and, more importantly, unused, and only allocated used memory when the task needs some.

The basic tool of software memory management is the free list, which is a map (usually a linked list) of all the memory areas that are not allocated to any tasks. Whenever a memory request is made (regardless of whether it is a new task or one that is already running), it checks the free list for any available memory that meets the request, and takes it off the free list.

Each task record has it's own list, of the memory it has allocated by the task (actually, it could be a single 'allocated list' for all the tasks, but this approach is easier IMAO). When a task frees memory it allocated (either by a free() or delete, or because the task ended or was closed by the system), it calls the memory manager, which puts the freed memory back into the free list.

This is a very simple model, but it covers most of the basics. The question of how allocation algorithm (how to decide which block of memory to allocate for a given request) is one of the great religious wars of OS design; how to prevent tasks from hogging all of memory is another one.

Virtual memory changes this a bit - each process has what it believes is the full system memory at it's disposal, while the memory management hardware tracks where each process's virtual addressing maps to the physical memory (or to the swap space). Setting up VM is rather difficult, however. Refer to Intel's documentation, or any of the many pages explaning VM for the x86 system, for more details.

Garbage collection simplifies the model from the perspective of the user programs (since there is no need to explicitly allocate or free memory), but adds considerable complexity to memory manager (interestingly enough, a VM system with sufficiently fine-grained paging can eliminate some of the complexity by implementing the equivalent of a conservative-comapaction algorithm for memory reclamation - but that's getting well ahead of ourselves). See Jones' book Garbage Collection and the various GC-related web pages for more details.
Matt

Re:Loading programs into memory

Post by Matt »

Ok, that makes it much more clear :D
Post Reply