In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how the hello program works". In daily operation, I believe many people have doubts about how the hello program works. The editor consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how the hello program works"! Next, please follow the small series to learn together!
Before we start, let's look at one of the most common programs:
#include int main(){ printf("hello,world\n"); return 0;}
This article starts with the simplest hello program above and follows its life cycle.
Program save format
The hello program above is actually a sequence of bits consisting of the values 0 and 1, grouped into groups of 8 bits called bytes. The characters in the hello.c file we enter are represented by each byte (most computer systems represent characters in ASCII, which converts bytes to integer values).
Summary: Information = bits + context
How does the program work?
Hello programs were born in C, which has the advantage of being readable by humans, but in order to run on a system, they have to be translated into a series of low-level machine language instructions.
On Unix systems, the conversion from source files to target files depends on the compiler, which records the compilation process as follows:
hello.c needs to read the contents of the system header file through the preprocessor and insert it into the program text to get a new C program, usually with.i as the extension;
The compiler then translates the text file hello.i into the assembly language text file hello.s.
Next, the assembler comes on stage, translating the assembly language into machine language instructions and saving them to the hello.o file, which is now a binary file
The final linking phase incorporates the c standard library functions called in our program into our hello.o program. The result is an executable file that can be loaded into memory and executed by the system.
the hardware composition
To really understand how the program works, you must first have an understanding of the hardware components of the system:
Bus: The electrical conduit that runs through the entire system. It can be understood that all data devices and data flow between systems must be connected to the bus.
I/O devices: communication channels between the system and the outside world, such as keyboards, mice, disks, monitors, etc.;
All I/O settings are connected to the I/O bus through a controller or adapter;
Main memory: that is, we often say memory, this is a temporary storage device, in the processor execution program, used to store programs and processed data;
Processor: Also known as CPU, it is the engine that interprets instructions stored in main memory; its core is a register of one word size (bytes of fixed length, depending on the system), called program counter (PC). During program execution, PC refers to a machine-language instruction in main memory.
From system power-on until system power-off, the processor is constantly executing instructions directed by PC, updating PC so that it points to the next instruction;
Here are a few possible actions that the CPU can perform when requested by an instruction:
The processor appears to be a simple implementation of its instruction set architecture, but modern processors employ very sophisticated mechanisms to speed up programs. Therefore, when we understand it, we should separate the instruction set architecture of the processor from the microarchitecture of the processor: the instruction set architecture describes the effect of each machine code instruction; the microarchitecture describes the implementation of the processor;
Load: Copy a word from main memory to a register, overwriting the contents of the original register;
Storage: copying a word from a register to a location in main memory to overwrite the original value at that location;
Operation: copy the contents of two registers to ALU (arithmetic/logic unit),ALU does arithmetic operation on these two words, and stores the result in a register to overwrite the original contents;
Jump: Take a word from the instruction itself and copy it to the PC to overwrite the original value in the PC.
run the program
When we are executing./ After hello, what actually happens is:
At first, the shell executes its instructions, waiting for us to type a command, and when we type./ After hello, the shell program reads the character into a register and stores it in memory;
When you hit Enter, the shell knows that we have finished typing commands, and then the shell executes a series of instructions to load the executable file, copying the code and data of the current file to main memory. Note: With direct memory (DMA) technology, data can be transferred directly from disk to main memory without the processor.
Once loaded into memory, the processor begins executing the program's main machine instructions, which copy bytes from the "hello,world\n" string from main memory to the register file. It is copied from the register file to the display device and finally displayed on the screen.
cache
From the above example, we can conclude that the hello program has gone through the process of starting on disk, being copied to main memory when loaded, copying from main memory to processor when the processor is running, and finally copying from processor to display.
here, from our programm's point of view, these copies are overhead, so that question come, how to reduce overhead and improve processor efficiency???
From a mechanical point of view, the larger the storage device, the slower it runs; the processor costs 10 million times more to read disk than to read memory, and the register file reads almost 100 times faster than the memory block, so speeding up the processor runs faster than speeding up the main memory.
To address the difference between processor and main memory, systems are designed to use smaller, faster storage devices called caches to store information that the processor may need in the near future. In fact, this and we usually develop procedures are the same, using multi-level cache, storage hot data, improve system processing capacity. The idea here is to take advantage of the tendency of programs to access data and code in local areas, so the cache stores data that is likely to be accessed frequently, so that most operations can be done in the cache.
Take a look at the memory hierarchy structure below, I believe you will be clear at a glance:
As shown in the figure, the upper level of memory is the lower level of cache.
Operating system management hardware
We write programs that do not directly access hardware such as keyboards, monitors, disks, etc., but rely on services provided by the operating system, so we can think of the operating system as a layer of software between applications and hardware.
The operating system has two main functions:
Prevent misuse of hardware;
Provide simple and consistent mechanisms for shielding applications from complex and often disparate underlying hardware devices;
Operating systems usually abstract several concepts: processes, virtual memory, files;
process
A process is an abstraction of a running application by an operating system, and a system can run multiple processes simultaneously.
Single-core processors can execute only one program at a time, whereas current multicore processors can execute multiple programs simultaneously. Whether single-core or multicore, a CPU appears to be executing multiple processes concurrently, which is achieved by switching processors between processes, known as context switches;
Switching between processes is managed by the operating system kernel, which is the main memory resident part of the operating system. When an application needs some operation from the operating system, such as reading or writing a file, it executes a system call that passes control to the kernel. The kernel then performs the requested operation and returns to the application. Note that the kernel is not an independent process; it is a collection of code and data structures used by the system to manage all processes.
thread
A process is actually made up of multiple execution units called threads, each of which runs in the context of the process and shares the same code and global data.
Advantages: Easier to share data than between processes; generally more efficient than processes;
virtual memory
This is an abstract concept, which provides an illusion for each process, that is, each process is using main memory separately, and the memory seen by each process is consistent, called virtual address space, as shown in the following figure, the address is increasing from small to upward:
file
A file is a sequence of bytes, and all I/O devices, even networks, can be viewed as files.
concurrent
Multi-core processors integrate multiple CPUs into one integrated circuit chip. The multicore processor is organized as follows:
Hyperthreading: A technique called simultaneous multithreading that allows a CPU to execute multiple concurrent streams simultaneously. The Intel Core i7 processor allows each core to execute two threads.
Abstraction in computer systems
In processing, the ISA provides an abstraction from the actual processor hardware, and using this abstraction, machine code behaves as if it were running on a processor executing only one instruction at a time. No matter how sophisticated the underlying structure is, even if multiple instructions can be executed concurrently, the burden is always consistent with that simple and orderly model. As long as the model is the same, different processor implementations can execute the same machine code while offering different overhead and performance. This abstraction is so important that it can be found throughout computer science, such as the lives of java classes and C function prototypes, and the layering of computer networks.
Looking at the above picture, it can be summarized as:
Files are abstractions of I/O settings;
Virtual memory is an abstraction of main memory and disk;
A process is an abstraction of processors, main memory, and I/O devices;
At this point, the study of this chapter is over, mainly on the composition of the computer system and the operation of the program has a large framework of cognition, follow-up to continue in-depth study.
scaling issues
Message = bit + context, what is context? What are some examples from work?
Every program has a lot of external variables. Only a simple function like Add has no external variables. Once your program has external variables, it is incomplete and cannot run independently. To make them work, you have to write values to all the external variables one by one. The collection of these values is called context. For example: C++ lambda expression inside,[written here is the context](int a, int b){... }
What is the difference between RISC instruction set and CISC instruction set, and what are their typical CPUs?
CSIC(Complex Instruction Set Computer) CPU with complex instruction set; CISC system design concept is to use the minimum number of instructions to complete the task (such as calculation multiplication only needs a MUL instruction), so CISC CPU itself design complex, complex process, but the advantage is that the compiler is well designed. CISC appeared earlier, Intel has been using CISC design;
RSIC(Reduced Instruction Set Computer) CPU with reduced instruction set; RISC design concept is to let software to complete specific tasks, CPU itself only provides basic function instruction set, that is, the number of instructions in the instruction set is relatively small. Compared with CISC design concept, CPU design and process are simpler, but compiler design is more complicated.
Typical CPU: A typical CISC has about 300 CPU instructions. The ARM CPU (a typical RISC CPU) has about 30 commonly used instructions. In general, CISC CPU power consumption is higher, generally used in PCs and laptops. Relatively speaking, RISC CPU power consumption is lower, generally used in the embedded field.
What is the difference between a stack-based CPU and a register-based CPU?
This problem can be thought of as a stack-based CPU, which runs programs using stacks, and code must use these instructions to move variables (i.e. push and pop);
This concludes my study of how the hello program works and hopefully solves your doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.