Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does OS execute compiled binary files?

This questions came to my head when I was studying processes scheduling.

How does OS execute and control the execution of binary and compiled files? I thought maybe OS copies a part of the binary to some memory location, jumps there, comes back after executing that block and executes the next one. But then it wouldn't have any control on it (e.g. the program can do a jump anywhere and don't come back).

In JVM case it makes perfect sense, the VM is interpreting each instruction. But in the binary files case the instructions are real CPU executable instructions so I don't think that an OS acts like VM.

like image 420
Gediminas Avatar asked Oct 17 '12 09:10

Gediminas


1 Answers

It does exactly that. The operating system, in some order,

  • creates an entry in the process table
  • creates a virtual memory space for the process
  • loads the program code in the process memory
  • points the process instruction pointer to the process entry point
  • creates an entry in the scheduler and sets the process thread ready for execution.

Concurrency is not handled by the program being split into blocks. Switching between tasks is done via interrupts: before a process is given CPU, a timer is set up. When the timer finishes, the CPU registers an interrupt, pushes the instruction pointer to the stack and jumps to the interrupt handler defined by the operating system. This handler stores the CPU state in memory, swaps a virtual memory table and restores some other thread that is ready for execution. The same swap occurs if the thread must pause for some other reason (waiting for user / disk / network...) or yields.
http://en.wikipedia.org/wiki/Multitasking#Preemptive_multitasking.2Ftime-sharing

Note that relying on the process yielding the CPU is possible but unreliable (the process might not yield, preventing other processes from running)
http://en.wikipedia.org/wiki/Multitasking#Cooperative_multitasking.2Ftime-sharing

Security is handled by switching the CPU into protected mode where the application code cannot run some instructions (so jumping around randomly is mostly harmless). See the link provided by @SkPhilipp

Note that modern JVM does not interpret each instruction (that would be slow). Instead it compiles into native code and runs the code or (in case of just-in-time compilation) interprets at first, but compiles the "hot spots" (the code that gets run often enough).

like image 75
John Dvorak Avatar answered Oct 15 '22 21:10

John Dvorak