One article teaches you the execution process of hello program

One article teaches you the execution process of hello program


The following is the first chapter of "In-depth understanding of computer systems":

There are roughly three processes:

1. Read command:
   Initially, the shell program executes its instructions, waiting for us to lose a command. When we enter the character string "./hello" on the keyboard, the shell program reads the characters one by one in the human register , and then stores it in memory , as shown below. (The order is keyboard-register-main memory)

img


Second, copy the data

  When we hit the Enter key on the keyboard, the shell knows that we have ended the command input. Then the shell executes a series of instructions to load the executable hello file , copying the code and data in the hello target file from the disk to the main memory. The data includes the string "hello, world \ n" that will be output eventually. (Sequential: disk-main storage).

Note: Using direct memory access (DMA) technology, data can reach the main memory directly from the disk without going through the processor (as shown below)

img


3. Execute and display the
   processor to execute the machine language instructions in the main program of the hello program. These instructions copy the bytes in the "hello, world \ n" string from main memory to the register file, and then from the register file to the display device, and finally display it on the screen.

img

The above picture comes from what is a "power" programmer?


Our instructions go through the process of reading from disk to main memory, then from main memory to processor execution
and data is read from disk to main memory, then from main memory to register file, and then from register File to the monitor.


After reading, do you feel a little bit, but I think there is something wrong, I am like this, so I summarized the following difficult points:


1, Shell is how the characters one by one to read people register , then store it into memory , these are not the cpu to do it?

2. How did the shell execute a series of instructions to load the executable hello file?

Answer: The first two questions can be answered together. First, the shell loads and runs the hello program. It does not directly access the keyboard, display, disk, or main memory, but depends on the services provided by the operating system .
  What service? The answer is the process . The process is an abstraction of a running program by the operating system. The shell we run is also a process. The process is the CPU. The result is to read the characters one by one in the register and store it in the memory.
  Is the shell process executing the hello file? The answer is no. Each time the user enters the name of an executable object file into the shell and runs a program, the shell transfers control to the operating system through a system call. After the operating system saves the context of the shell process, a new process is created, which we can call the " hello process ". The operating system passes control to the "hello process". After the processor executes the "hello process", it Control is returned to the shell process, and the shell process continues to wait for the next command line input.

3. How does the shell display "hello, world \ n" on the screen?

Answer: After executing the "hello process", the shell will feed back the running results to the user, which is displayed on the screen.


Summary: When the shell interprets the command, it is not to execute it in person, but to derive the child process and let the child process complete the work.


Anyone who writes badly or wants to discuss is welcome to leave a message!
Published 14 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/Milan_1in/article/details/105487667