How is a program loaded into memory and then executed?

Asked

Viewed 1,826 times

15

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

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

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

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

  • Your flow is a little crooked. But regardless of this, the instructions are executed one after the other, who has the job of "knowing" which address the next instruction is the processor and not the OS.

  • Crooked as so? , I know the instructions are executed sequentially. What I’d like to know is how does the processor know to execute instructions from the "x" memory position? Someone should ask him to do it, right? , I imagine so because after the execution command the stream passes through the S.O kernel before going to processor.

  • He knows this because he has a recorder that is always pointing to the memory position that is the next instruction.

2 answers

12


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

The exact way the operating system loads an executable despende d and which operating system we are talking about. The executable format itself also depends on.

Virtual memory

In modern Oses there is a virtual memory system. When a command loads an executable it usually maps the file to memory. So somehow it doesn’t matter much what’s in the mass storage system or the RAM. Of course it needs to be in RAM to run, but when the content will go into RAM is determined by the need and algorithms implemented in the operating system. Loading is done in pages with size 4KB (the most common).

Memória virtual

Virtual addresses are provided by the operating system. Actual addresses are provided by the processor. Every modern high-capacity processor has a virtual address translation subsystem for physicists that does not impose cost on normal processing.

All this can be best seen in another question here.

Transference

The copy is made by another operating system service that interacts with the file system and the 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 one that makes the control of the input and output devices and the memory (DMA).

When the charge is made it is possible to go delivering the instructions to the processor.

Relocation

The load usually implies hitting addresses other than global symbols. The code works with relative addresses. He doesn’t really know where he’ll actually be in his memory. Knowing the relative addresses you can get the absolute of all symbols and the OS is responsible for relocating the addresses.

Payload

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

There are static data. The most common example is text (strings) present code. But in fact virtually any literal will be in any way either next to the instructions or in the static area.

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

Before starting execution 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 thread new will have its own stack.

Completion

Anyway, simply put. 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 suitable location. If you want something more detailed, you need to see what and in what OS.

Addendum

The term Assembler is often misused. In fact there was not even to use Assembler, nor Assembly, is machine code even.

  • Thanks for the explanation, the links also helped a lot. I’m reading "Computer Organization and Design: The Hardware/Software Interface (David A. Patterson)". It has helped me understand these concepts, you recommend some other reading for those who want to delve into the subject ?.

4

Well, basically the executable is loaded into memory by the Operating System. Actually, your question has to do with memory management and each operating system has a way to manage it.

In general terms, it is separated for the program, a memory region that will be used by it and the form of use will be determined by the compiler (or 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 directly accesses routines, processes and registers through processing cycles (controlled directly by the processor)

Yes, the OS provides multiple "cells" of memory to 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 the main memory. I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.