# How a program is copied to RAM from harddisk

I know that for executing a program, it should be copied to RAM. But the problem is whole of it may not be copied always.

Since the size of the RAM is limited, there is mechanism called virtual memory. If the addressed thing is not in memory, a page fault occurs and the data is copied to the RAM. My question is who keeps track of which data is in the RAM and not in the RAM?

If the virtual page is currently unmapped then the operating system receives an interrupt. At this point it looks into the memory map for the process. This is a list of ranges of virtual memory, the permissions that should be applied to that range, and the file (if any) on disk that stores the data from that range when it is not in RAM. Typically for the main executable there will be a text segment, data segment and sometimes a read-only data segment (for constants), as well as a bss (zero initialized data) segment, a stack, and the heap managed by malloc. There may be (usually are) additional text and data segments for each shared object (shared library) that the program needs to load.
You can use mmap() to tell a Posix operating system to create a new region in the memory map, the permissions for that region, and which file to use to back that region. On Linux, for an existing process you can get a listing of its currently mapped regions using the pmap command.