Utkarsh Srivastav
Utkarsh Srivastav

Reputation: 3227

Program elements in main memory

What are the program elements that should be present in RAM when a process like a c program executable a.out is running ? Is program code also there? And if yes what is the need of it?

Upvotes: 1

Views: 185

Answers (3)

Jeegar Patel
Jeegar Patel

Reputation: 27210

Is program code also there?

yes your program code will be also in RAM

actualy any c program has main 3 segment in memory

* Data Segment
* Code Segment
* Stack and Heap areas

your code goes in code segment.
enter image description here

see This article

Upvotes: 0

In principle the machine code is inside the process' address space, and very often in RAM. However, there are situations where the machine code is not yet in RAM, and the kernel has to load it from disk. This happens transparently, because of virtual memory. Concretely, the execve(2) syscall sets up memory mapping for the various segments in the ELF executable binary, much like mmap(2) does (it is the system call, with munmap and  mprotect to change the memory map).

Look at the /proc/1234/maps of a process 1234 to understand more, or simply run

  cat /proc/self/maps

to get the memory map of the process running that cat.

Upvotes: 0

Igor Serebryany
Igor Serebryany

Reputation: 3331

yes, the code a.out is present in memory when a.out is running. the instruction pointer (or register) points to the current operation being executed in memory, and most operations also advance the instruction pointer to the next operation. although, if by code you mean the original C code, this is not always the case -- only the assembled executable bytecode has to be in memory. there are flags to the compiler/linker to include the C code in the assembled output executable to make debugging easier.

if we're talking about the virtual memory space of the process, this will also include some memory-mapped io registers, some kernel-space functions, and any libraries the program requires (at least libc for your general-type operations).

Upvotes: 1

Related Questions