On Tue, May 13, 2008 at 6:38 PM, Mulyadi Santosa <mulyadi.santosa@xxxxxxxxx> wrote: > Hi Eduardo... > > > On Tue, May 13, 2008 at 9:01 AM, Eduardo Júnior <ihtraum18@xxxxxxxxx> wrote: > > > > Hello, > > > > > > I asked this once in irc, but I think that I was not clear > > I wanna know how does memory allocation for brand new processes work? > > I hope it can give you the answer,although not point per point. > > In essence, when you do fork() or clone(), Copy on Write mechanism is > used. That means, it starts with nothing more than just the process > metadata such as task_struct, mm_struct, opened file and so on. > > When it tries to allocate something, page fault kicks in. It could > lead simply to reading page cache, or reading directly to disk (hard > page fault). Most likely, if it's something like shared library, it's > already cached in RAM thus page cache is hit. > > Thing is a bit different when deals with exec() family. COW no longer > works here because you're forking based on whole new binary. But > still, demand paging works. It starts with very minimal allocated > pages (process metadata, some elf info but not all). I remind you that > process is not an identical copy of the whole ELF image. Things like > ELF header....AFAIK..is not copied to RAM. Section can be mapped into > overlapped region...and so on > > I hope it gives another point of view... > > regards, > > Mulyadi. Just to contribute a bit: in fs/binfmt_elf.c: load_elf_binary() is the function for loading ELF. And within this function, u can see all the glory of all that has been mentioned before. Basically, just to give the impt gists of this LONG function, ELF image has file size and image size specified within it. The image (meaning memory) size is virtually allocated, then the physical data is copied into virtual mem (amount is equal to file size specified), and so normally it is always smaller than virtual size as in the following remark in binfmt_elf.c (p_memsz vs p_filesz): /* * Check to see if the section's size will overflow the * allowed task size. Note that p_filesz must always be * <= p_memsz so it is only necessary to check p_memsz. */ And there are many sections to be copied - as specified the ELF header - and this is why the function is so LONG. Virtual allocation does not guarantee that pagetable entries have been constructed, and so upon page fault it will be constructed and physical mem allocated. Another complication is the common libraries need not be multiply loaded in physical memory, but in virtual memory it can take on different different starting addresses, as required by the ELF. One more thing: all ELF is compiled to be loaded at a particular FIXED starting virtual mem, but the libraries are not, as they have relocation table embedded within to allow it to be relocated anywhere in memory. THanks. -- Regards, Peter Teoh -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ