views:

27

answers:

1

When a new process is created the Address space is created using fork() i.e new page table entries are created for the new process which are exactly same as the parent process. After fork() the exec() is called. What happens during the exec() system call?

I read in the book "Operating system concepts " that when a new program is executed, the process is given a new empty VAS. Does that mean that the page table entries created during fork() would get deleted/modifeid ? What is the meaning of empty VAS?

How does the memory mapping of binary to VAS is performed? How does the loader knows that what addresses of the VAS should be mapped to the corresponding binary file?

I am really confused here.

A: 

when you call exec the kernel will load the binary and set up a whole new set of page tables (replacing the old ones).

The loader gets the address to load the binary at from the binary itself (basically it does read() to get the headers and stuff that's not code, then mmap() to actually load the code/data stuff in the binary)

so it looks at the binary and figures out how it should be loaded, the does mmap(), passing in an address to do the map at for each part of the binary that needs to be in a different place (ie code and data sections are probably two different calls to mmap() also the .bss section would be mapped from /dev/zero)

Note that depending on the OS and the binary being loaded some of this stuff may be handled by the kernel directly or by a userspace loader (on UNIXish systems ld would be the loader, it handles shared object loading)

Spudd86
The loader does get information for mapping the binary to VAS from the binary itself, by reading the headers of the ELF format binary.Now, suppose there are two processes simultaneously using a binary say abc.exe, then if the VAS is mapped according to the binary it means that the virtual address space of both the processes would be similar. Is it so??
Ashish
sometimes... there's also position independent code that can be loaded at any address (shared libraries are always like this). The other part of the loaders job is to process 'relocations', that is change certain values so that they match the actual address the binary is loaded at. However executables are frequently not PIC so they are always loaded at the same virtual address. (but the address space might still be different if for some reason the dynamic linker loaded say libc at different spot (eg address randomization))
Spudd86