Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is all program code loaded into the text\code section\segment of memory

I have started to look at c programming and whilst I am not a complete beginner (I have knowledge of java and web development) there are a lot of things I do not understand.

My question is about when a program is first loaded into memory. I am having trouble understanding what actually happens here.

Is all of the program code loaded into memory when the program is launched or is only what is needed loaded in?

After this does this code\set of instructions get swapped in and out of the physical disk as the process gets CPU time or does loaded code stay in memory whilst the program is running?

If two processes can share the same set of instructions does this mean each process gets a separate code section in its virtual memory space?

I am sorry if my questions are basic or poorly worded but I only started looking at this last week and after a weekend of reading I have far more questions than answers!

like image 527
berimbolo Avatar asked May 20 '13 11:05

berimbolo


People also ask

Does text segment contain program code?

The text segment consists of program code. It is read-only and is initialized from the program executable file. Initialized data: strings initialized from the program executable. Uninitialized data: global variables.

How are programs loaded into memory?

For a program to execute as a process, it has to be loaded into memory via a program loader. The executable file that contains the program consists of several sections that identify different regions of the program and where they should be loaded into memory.

What goes into the text segment?

The (somewhat oddly named) text section (segment) usually contains the compiled binary code, as you suspect. An executable often contains other types of data besides the code, like initialized variable values, resources, debug information, relocation data, and so on, which are placed in sections with other names.

What is code segment in memory?

In computing, a code segment, also known as a text segment or simply as text, is a portion of an object file or the corresponding section of the program's virtual address space that contains executable instructions.


1 Answers

Is all of the program code loaded into memory when the program is launched or is only what is needed loaded in?

Most modern OS's will load "on demand", so the starting point of the application (main) will be loaded by the OS, then the OS just kicks off there. When the application jumps to a piece of code that isn't in memory yet, it loads that bit.

After this does this code\set of instructions get swapped in and out of the physical disk as the process gets CPU time or does loaded code stay in memory whilst the program is running?

If the OS decides that some memory is needed it may well throw out some of the code, and reload it when it needs it later [if it's ever needed again - if it was some part of the initialization, it may never get hit again].

If two processes can share the same set of instructions does this mean each process gets a separate code section in its virtual memory space?

It is certainly possible to share the code between multiple copies of the same application. Again, whether a particular OS does this or not depends on the OS. Linux certainly shares code copies from the same application between two (unrelated) processes [obviously, a forked process shares code by definition]. I believe Windows also does.

Shared libraries (".so" and ".dll" files for Linux/Unix and Windows respectively) are also used to share code between processes - the same shared library is used for many different applications.

The Data space is of course separate for each application and shared libraries will also get their own data section per process sharing the library.

like image 149
Mats Petersson Avatar answered Oct 06 '22 00:10

Mats Petersson