12

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!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
berimbolo
  • 3,319
  • 8
  • 43
  • 78

1 Answers1

8

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Mats thank you for that answer it covered everything I needed to know, unfortunately (as is usually the case) it has left me with more related questions though! If the instructions for a method were loaded and this method could be called multiple times in the life of a program would it stay in memory? And my other question is about the code to generate graphical display? I would assume this code would have to be loaded permenantly for the lifetime of the program or am I incorrect in thinking this? – berimbolo May 20 '13 at 11:57
  • As long as there isn't so much shortage of memory that the OS "needs" the memory for something else, it stays in memory. I don't see why "code to generate graphical display" is any different from any other code - it's code, just like code that does math, searches in a table or counts the number of ones in some integer. [Obviously, the code for a DRIVER that actually draws on the screen is a kernel module or system driver, and these are typically not "swappable" - and these are normally not "demand loaded" either, but loaded up all at once, and not unloaded in a typical case]. – Mats Petersson May 20 '13 at 12:05
  • I should add that typically, the OS will keep track of "how long since this memory was used" in some way. So it is more likely that some code (or data) that has been sitting around without being used will be "kicked out" than something that has recently been run. – Mats Petersson May 20 '13 at 12:07
  • ok thanks again for taking the time to explain this, I think I am now happy that I understand enough to continue reading! Cheers. – berimbolo May 20 '13 at 12:15