How is a program loaded into memory and then executed?

10

Imagine that I wrote a "hello world" in C language. I compiled and generated an executable, then executed it.

The result of the compilation is a binary, which in fact is also a set of instructions that will be executed by the processor, correct?

After the execution command as the binaries are transferred to RAM?

After the binaries are in memory the operating system makes an assembler call asking the processor to execute the binary instructions, if yes is the operating system that provides the instructions memory address?

    
asked by anonymous 02.06.2017 / 00:48

2 answers

8

I think you'll want to know how the computer works with the code .

The exact way the operating system loads an executable is from the operating system. The executable format itself also depends.

Virtual memory

In modern OS there is a virtual memory system. When a command sends an executable, it normally maps the file to memory. So in a way it does not matter much what is in the mass storage system or in RAM. Of course it needs to be in RAM to run, but when the contents will go to the RAM is determined by the need and algorithms implemented in the operating system. The load is made on pages with 4KB (the most common) size.

Thevirtualaddressesareprovidedbytheoperatingsystem.Theactualaddressesareprovidedbytheprocessor.Everymodernhigh-capacityprocessorhasavirtualaddresstranslationsubsystemforphysicsthatdoesnotimposecostonnormalprocessing.

Allthiscanbestbeseenin another question here .

Transfer

The copy is made by another operating system service that interacts with the file system and driver which understands how to command the disk system or other form of "permanent" storage. Obviously it depends on the collaboration of the main processor or some secondary that controls the input and output devices and memory ( DMA ).

When the load is done it is possible to go handing the instructions to the processor.

Relocation

The load usually implies a hit of several addresses of global symbols. The code works with relative addresses. He does not quite know where he will actually be in the memory. Knowing relative addresses one can get the absolute of all symbols and the OS is responsible for relocating the addresses.

Uploading the executable

Of course, the executable does not just have instructions. There is a lot of information that helps the operating system decide how to work with it, which configures how it will need to work, among other things.

There is static data. The most common example is the text ( strings ) code present. But in fact virtually any literal will be in some form or by the instructions or in the static area.

There may also be data that is not accessed directly but is required by the application. Actually you can put anything in the executable:)

Before starting to run, a memory area is reserved for the stack and the heap begins to be formed, if necessary, almost always is. But this has more to do with the process as a whole, because each new thread will have its own stack .

Conclusion

Finally, this is simplicity. You can write a book about it. New specific questions can be asked.

If you want information about the electronic operation of the transfer, here is not the most appropriate place. If you want something more detailed, you would need to see what and where.

Addendum

The term assembler is often misused. It was not even there to use Assembler, nor Assembly . It's machine code itself.

    
02.06.2017 / 02:54
1

Well, basically the executable is loaded into memory by the Operating System. In fact, your question has to do with memory management and every operating system has a way of managing it.

In general terms, a region of memory that is used by the program is separated into the program, and the form of use is determined by the compiler (or by the way the program was written). This allocation can be totally static or dynamic (if the program uses dynamic memory allocation). When the program is in memory (Von Neumann architecture), it basically accesses the routines, processes and registers directly through processing cycles (directly controlled by the processor).

Yes, the OS provides several "cells" of memory for the program (which will be addressed using memory management techniques). That is, it is the OS that does the memory management, controlling the allocations, protecting, cleaning and controlling the access of programs to main memory. I hope I have helped.

    
02.06.2017 / 01:30