Briefly describe what happened in Hello World

Source: Internet
Author: User

I have been reading a book recently. Here I will record some important parts.

#include <stdio.h>int main(){     printf("hello world");     return 0;  }

In a simple sentence, Hello world, what has happened from compilation to execution? Below is a brief description.

After such a hello. c file is written, it is only a common text file consisting of ASCII characters.

First, you need to use a compiler to compile it into an executable program.

Gcc-O hello. c

In this compilation process, we have actually performed four steps, namely preprocessing, assembly, compilation, and linking.

Pre-compilation phase: the original file is modified by processing all the commands starting with # In hello. C to generate a hello. I file, such as # include, # define

Assembly phase: translate the pre-processed hello. I file into the assembly version of the text file hello. s

Compilation phase: translate the Assembly text file into machine language commands. package these target commands into a format that can be relocated to the target program, and save the result in hello. o file, the hello. O is a binary file.

Link phase: Find the target binary file printf Based on the functions called in hello. O merge to our hello. in the O program, the hello file is generated. This is an executable target file that can be loaded into the memory and executed by the system.

================================================= ==========

What happened to the generated hello during execution? Here we need to know the composition of the system hardware.

Bus: A group of Electronic pipelines that run through the entire system. It is responsible for transmitting information between various hardware. It usually transmits a fixed length of bytes. Most systems use 4 bytes (32 bits) or 8 bytes (64-bit)

I/O device: the connection channel between the system and the outside world. Generally, I/O devices have keyboards and mouse that are used as user input, as user output monitors, and disks that store data and programs, io devices are connected to the IO bus through controllers or adapters.

Primary storage: a temporary storage device, that is, memory. It is used to store data processed by programs and programs when the processor executes the program. Logically, it can be regarded as a linear byte array.

CPU: Short for CPU, which is used to explain the engine for executing commands stored in the primary storage. The core of a processor is a storage device (generally a register) with a size of one word. It is called a program counter. The processor reads the instruction from the memory pointed to by the program counter and interprets the bit in the instruction, and execute this command

The CPU may perform the following operations as required.

Load: copy a byte or word from the primary storage to the Register to overwrite the previous content.

Storage: copy a word or byte from a register to a location in the primary storage to overwrite the previous content.

Operation: copy the two registers to Alu (arithmetic/logical unit). ALU performs arithmetic operations on the two words and stores the results in one register.

Jump: extract a word from the command itself and copy the word to the program counter.

================================================= ==========

After learning about the above basic hardware, let's take a look at what happens when a hello command is executed in shell.

First, enter./Hello on the terminal.

The USB controller receives user input from the keyboard and transmits the input data to the bus through the I/O bus. Then, the bus interface stores the characters in the registers one by one, and then puts the characters in the main memory from the registers.

Then we press enter and we can see hello World.

When you press enter, the shell program knows that we have completed the command input and then executed a series of commands to load the executable hello

These commands copy the target file from the disk to the primary storage, and then the processor starts to execute machine language commands in the hello main.

The machine language Command copies the hello World string from the primary storage to the register, then copies it from the register to the display device, and finally outputs the result.

The information here is transmitted through the bus interface and I/O bus.

At this point, the hello. c execution is complete.

================================================= ==========

The above example shows that the system spends a lot of time moving information from one place to another, for example, transferring information from I/O input to a register, then it is stored in the primary memory, copied from the primary memory to the register, and then output to the I \ O device display.

These replications are overhead, slowing down the real work of the program. Therefore, one of the main objectives of the system is to make the replication as fast as possible.

According to the machine principle, storage devices in large spaces run slowly. For example, reading data from a disk is 10 million times less efficient than reading data from the primary storage, reading from the primary memory is 100 times less efficient than reading from the register.

To address the differences between registers and primary storage, the system designer uses a smaller and faster storage device and high-speed cache memory, which stores information that may be needed by the processor in the near future.

High-speed cache can be divided into three levels: L1, L2, and L3

The capacity of the L1 high-speed cache can reach tens of thousands of bytes, And the access speed is almost as fast as the Register

The capacity of the L2 high-speed cache is 100,000 to several millions, and the access speed is 5 times slower than that of the L1 high-speed cache.

By storing frequently accessed data in the high-speed cache, most memory operations can be completed in the high-speed cache quickly, and the Register does not need to take data from the primary storage using the bus, copy data directly from the cache.

================================================= ==========

The running process in the hello example shows that our program does not directly access the keyboard, display, primary storage, and disk. But through the services provided by the operating system. We can regard the operating system as a layer of software between applications and hardware.

All applications must operate on the hardware through the operating system.

 

Briefly describe what happened in Hello World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.