What is a computer system

A computer system is made up of hardware and software that work together to run programs. Different systems may have different implementations, but the core concepts are the same and universal.

The different systems are Microsoft Windows, Apple Mac OS X, Linux, etc.

All computer systems have similar software and hardware components that perform similar functions.

What do you want

First, let me ask you a question. What kind of programmer do you want to be?

This is a great open source project I recently found and its path is github.com/keithnull/T…

That is

I have also downloaded all the Chinese/English books involved in it. (Von Neumann)

I always wanted to be the first kind of engineer, and even if I never did, I wanted to get closer and closer to it.

Get back to business

Yeah, I want to be a power programmer

A simple procedure

This time really get down to business, the following is a very simple C program (regardless of my name is Java Builder or whatever, Java builder can not learn C? Java is the rice bowl, but C is the father.

#include <stdio.h>

int main(a){
  pritnf("hello, world\n");
  return 0;
}
Copy the code

This is a Hello,world program output in C, and although it’s a very simple program, every part of the system has to work together to run.

The program life cycle is when the programmer creates the program, runs it in the system, prints a simple message, and then terminates.

The programmer first creates the code in text, also known as the source file or program, and saves it as a hello.c file, which is essentially a bit of zeros and ones. The eight bits form a group called a byte. Each byte in turn represents a text character, which is usually composed of ASCII characters, such as the ASCII code for the Hello. c program

The hello.c program is stored in a file in byte order, with each byte corresponding to an integer value, that is, 8 bits for an integer. If the first character is 35, where does this 35 come from? In fact, there is a comparison table of ASCII codes (because there are many ASCII codes, you can check the website ascii.911cha.com/?year=%23, here only a few for reference).

Each line ends with an invisible \n, whose ASCII code value is 10.

Note; Files such as hello.c that consist only of ASCII characters are called text files. All other files are called binaries.

The hello.c representation illustrates the basic idea that all information in a system – files on disk, programs in memory, data stored in memory, and data transmitted over the network – is represented by a string of bits. The only way to distinguish between different data objects is the context in which we read them. For example, in a different context, the same sequence of bytes might represent an integer, a floating point number, a string, or a machine instruction.

Why C

Here we come. Why do we need to learn C? Learning Java with C language? It’s time to talk about the development of C

The C language originated at Bell LABS. The American National Standards Institute ANSI issued the ANSI C standard in 1981, and later C was standardized. These standards define the C language and a series of function libraries, the so-called C language standard library, so what are the characteristics of C language?

  • The C language is closely associated with the Unix operating system. C has been developed as the programming language for UNIX systems since the beginning. Most of the UNIX kernel (operating system and core parts) and tools and dynamic libraries are written in C. UNIX became the most popular operating system of the 1970s and 1980s, while C became the most popular programming language
  • C is a very small, simple language. And the simplicity of C language makes it more portable.
  • The C language is designed for practical purposes.

We have mentioned the advantages of C, but not all programmers can master and use C. Pointers in C often give many programmers headaches. C also lacks good support for abstractions, such as classes and objects, but C++ and Java have solved these problems.

Programs are translated into different forms by other programs

C programs are a high-level language because they can read and understand people’s minds. However, in order to run the hello.c program on the system, individual C statements must be converted by other programs into a series of low-level machine language instructions. These instructions are packaged as executable object programs and stored in binary disk files. An object program is also called an executable object file.

On UNIX systems, conversion from source files to object files is performed by the compiler.

gcc -o hello hello.c
Copy the code

The GCC compiler driver reads Hello.c from the source file and translates it into an executable file called Hello. This translation process can be illustrated in the figure below

This is the complete Execution of a Hello World program, which involves several core components: a preprocessor, a compiler, an assembler, and a connector, which we’ll break down one by one.

  • Preprocessing PhaseThe preprocessor will start with#Character to modify the source C program.#include <stdio.h>The command tells the preprocessor to read the system header filestdio.hAnd insert it into the program as text. And then you get another C programhello.i, this program is usually based on.iTo the end.
  • And then theCompilation Phase, the compiler will put the text filehello.iTranslate into texthello.sIt includes a paragraphAssembly language Program. This function contains the definition of main as follows
main:
	subq		$8, %rsp
	movl		$.LCO, %edi
	call		puts
	movl		&0, %eax
	addq		$8, %rsp
	ret
Copy the code

2-7 in the above definition describes a low-level language instruction. Assembly language is very useful because it can provide its own set of standard output languages for different high-level languages.

  • After the compilation is completeAssembly PhaseThis step,The compiler asHello. S is translated into machine instructions, which are packaged intoRelocatable Binaries (Relocatable Object Program)Put it in the hello.c file. It contains 17 bytes of instruction encoding the main function, and if we open Hello. c in a text editor we’ll see a bunch of garbled characters.
  • The last one isLinking Phase, our Hello program will callprintfFunction, which is part of the C standard library provided by the C compiler. The printf function is located in a function calledprintf.oThis is a separate pre-compiled object file that must be linked to our hello.o,Connector (ld)The merge operation is handled. As a result, the Hello file, which is an executable object file (or executable file), is ready to be loaded into memory and executed by the system.

You need to understand what the compilation system does

For a simple Hello program like the one above, we could rely on the compilation system to provide a correct and valid machine code. However, for the programmers we talked about above, there are a few characteristics of compilers that you need to know

  • Optimizing Program PerformanceModern compilers are an efficient tool for generating good code. As a programmer, you don’t need to understand what’s going on inside the compiler in order to write high-quality code. However, in order to write efficient C programs, we need to understand some basic machine code and how the compiler converts different C statements into machine code.
  • Understanding link-time errorsIn our experience, some very complex errors are mostly caused by the linking phase, especially if you want to build large software projects.
  • Avoiding security holesIn recent years,Buffer overflow vulnerabilities were ignored.Is the main culprit of the network and Internet services, so we need to avoid this problem

The processor reads and interprets instructions in memory

Our hello.c source program has now been interpreted as an executable Hello target program, which is stored on disk. If we want to run this program on a UNIX operating system, we need to type it into a shell application

cxuan $ ./hello
hello, world
cxuan $ 
Copy the code

To explain what a shell is, a shell is essentially a command interpreter that outputs a character, waits for the user to type a command, and then executes the command. If the first word on the command line is not a built-in command, the shell assumes that this is an executable, and it loads and runs the executable.

System hardware composition

To understand what happens when the Hello program runs, we need to first have an understanding of the system’s hardware. Here is a model of an Intel system product to explain

  • Bus (Buses)Running throughout the system are collections of electrical pipes called buses that transfer bytes of information back and forth between components. Usually buses are designed to transmit chunks of fixed length, i.eWord (word). The number of bytes in a word (word length) is a basic system parameter that varies from system to system. Most words today are 4 bytes (32 bits) or 8 bytes (64 bits).

  • I/O Devices: Input/Output Devices are the connections between the system and the outside world. In the figure above, there are four types of I/O devices: a keyboard and mouse for user input, a display for user output, and a disk drive for storing data and programs for long periods of time. In the beginning, executable programs are saved on disk.

    Each I/O device connected to the I/O bus is called a controller or Adapter. The main difference between controllers and adapters is encapsulation. A controller is a chipset on the I/O device itself or on the system’s main printed board circuit (usually called the motherboard). The adapter is a card that plugs into a slot on the motherboard. Regardless of the form of organization, their ultimate purpose is to exchange information with each other.

  • Main Memory. Main Memory is a temporary storage device, not permanent storage. Disks are permanent storage devices. Main memory holds both the program and the data processed by the processor to execute the flow. In terms of physical composition, main memory is a set of dynamic random access memory (DRAM). Logically, memory is a linear byte array with its unique address number, starting at 0. Generally, each machine instruction that makes up a program is made up of a different number of bytes, and the size of the data item corresponding to the C program variable varies according to the type. For example, on Linux x86-64 machines, short data takes 2 bytes, int and float 4 bytes, and long and double 8 bytes.

  • A Processor, central processing unit (CPU), or simple Processor is an engine that interprets (and executes) instructions stored in main memory. The processor’s core is a one-word storage device (or register) called a program counter (PC). At any given moment, the PC points to a machine-language instruction in main memory (that is, the address containing the instruction).

    From the time the system is powered on until the system is powered off, the processor continuously executes the instruction pointed to by the program counter, and then updates the program counter to point to the next instruction. The processor operates according to an instruction model defined by its instruction set architecture. In this model, instructions are executed in strict order, and executing an instruction involves executing a series of steps. The processor reads instructions from memory pointed to by the program counter, interprets the bits in the instruction, performs some simple operations that the instruction instructs, and then updates the program counter to point to the next instruction. Instructions may be sequential or dissequential (for example, JMP instructions are not read sequentially)

    Here are a few steps where the CPU might perform a simple operation

  • Load: To copy a byte or word from main memory into memory, overwriting the previous contents of a register

  • Store: The copying of a byte or word in a register to a location in main storage, overwriting the previous contents of that location

  • Operate: copy the contents of the two registers to the Arithmetic logic unit (ALU). To perform arithmetic operations on two words and store the result in a register, overwriting the previous contents of the register.

An arithmetic logic unit (ALU) is a combined digital electronic circuit that performs arithmetic and bitwise operations on numeric binary numbers.

  • Jump, jump): Extracts a word from the instruction and copies the word toProgram counter (PC)Overrides the original value

Analyze the execution of the Hello program

Now let’s take a more formal look at what happens when we run the example program. We’ll describe it from a macro point of view, without getting into all the technical details

At first, the shell program executes its instructions and waits for the user to type a command. When we type the./hello characters on the keyboard, the shell program reads the characters one by one into registers and puts them into memory, as shown in the figure below

When we hit enter on the keyboard, the shell program knows we have finished typing a command. The shell then loads the executable Hello file with a series of instructions that copy the code and data from the object file from disk to main memory.

Using DMA(Direct Memory Access) technology, you can directly copy data from disk to Memory, as shown below

Once the code and data from Hello in the object file are loaded into main memory, the processor starts executing 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 a register file, from the register to the display device, and finally to the screen. As shown below.

Caching is key

Above we have described the execution of a Hello program. The system spends a lot of time moving information from one place to another. The machine instructions for the Hello program are initially stored on disk. When programs are loaded, they are copied to main memory. When the CPU starts running, instructions are copied from memory to the CPU. Similarly, the string data Hello,world \n is originally on disk, copied into memory, and then output to the display device. From the programmer’s point of view, much of this copying is overhead, which slows down the efficiency of the program. Therefore, for system design, one of the most important jobs is to make the program run faster and faster.

Due to the laws of physics, larger storage devices are slower than smaller ones. As the processing of registers and memory becomes more and more efficient, system designers address this difference by using smaller and faster storage devices called cache memory (cache memory for short), which serve as temporary staging areas for information that may be needed in the near future. As shown in the figure below

We have indicated the location of the cache. The L1 cache in the cache can be in the tens of thousands of bytes and can be accessed almost as fast as a register file. The larger L2 cache is linked to the CPU through a special bus. Although L2 cache is five times slower than L1 cache, it is still five to ten times faster than memory. L1 and L2 are implemented using a hardware technology called static random access memory (SRAM). The latest, more powerful systems even have three levels of caches: L1, L2, and L3. The system can obtain a large memory and access speed is also faster because of the principle of caching locality.

Locality principle: In CS, reference locality, also known as locality principle, is the mechanism by which the CPU tends to repeatedly access the same set of memory within a short period of time.

By storing frequently accessed data in the cache, most operations on memory can be done directly in the cache.

Storage device hierarchy

Above we mentioned L1, L2, L3 caches and memory, which are all used for storage purposes. Below we have plotted the hierarchy between them for you

The main idea of memory is that the upper level of memory acts as a cache for the lower level of memory. Thus, register files are the cache for L1, L1 is the cache for L2, L2 is the cache for L3, and L3 is the cache for main memory, which in turn is the cache for disk. Here is a brief introduction to the memory device hierarchy, which will be explained later.

How does the operating system manage hardware

Back in our hello program, when the shell loads and runs the hello program, and the Hello program outputs its own message, neither the shell nor the Hello program has direct access to the keyboard, monitor, disk, or main memory. Instead, They rely on the operating System to do this. An operating system is a kind of software. We can think of the operating system as a layer of software between applications and hardware. All operations that want to operate directly on hardware go through the operating system.

An operating system has two basic functions:

  • The operating system prevents hardware from being abused by runaway programs
  • Provide applications with a simple and consistent mechanism to control low-level hardware devices.

So how does an operating system operate on hardware? These functions are achieved through processes, virtual memory, and files.

A file is an abstract representation of an I/O device, virtual memory is an abstract representation of main memory and disk I/O devices, and a process is an abstract representation of a processor, main memory, and I/O device. Let’s take a look at them one by one

process

Process is the core concept in operating system. Process is an abstraction of running program. Everything else in the operating system revolves around processes. They support (pseudo) concurrent operations even with only one CPU. They abstract a single CPU into multiple virtual machine cpus. We can abstract a process as a process model.

In the process model, a process is an instance of an executing program, including the current values of program counters, registers, and variables. Conceptually, each process has its own virtual CPU, but in practice the CPU switches back and forth between processes.

As shown in the figure above, this is a multi-processing program with four programs, and the program counter changes in different ways as the process changes.

In the figure above, the four programs are abstracted as four processes with their own control flow (that is, each program counter), and each program runs independently. Of course, there is really only one physical program counter, and when each program is run, its logical program counter is loaded into the physical program counter. When the program finishes running, its physical program counter is the real program counter and is then put back into the logical counter of the process.

As you can see from the figure below, after a long enough period of observation, all processes are running, but only one process is actually running at any given moment.

Thus, when we say that a CPU can only really run one process at a time, even if there are 2 cores (or cpus), each core can only run one thread at a time.

Since the CPU switches quickly back and forth between processes, the amount of time each process runs in the CPU is uncertain. And when the same process runs in the CPU again, its running time inside the CPU is often not fixed.

As shown in the figure below, the transition from one process to another is managed by the operating system kernel. The kernel is the resident part of the operating system code. When an application needs something from the operating system, such as reading or writing a file, it executes a special system call instruction.

Note: The kernel is not a separate process. Rather, it is a collection of code and data structures used by the system to manage all processes.

We’ll talk more about these processes later

thread

In traditional operating systems, each process has an address space and a thread of control. In fact, this is the definition of most processes. However, in many cases, it is common to have multiple threads of control running in the same address space, acting as separate processes. To be more precise, this is really a discussion of the process model versus the thread model, and the answer to this question is probably a three-step answer

  • The ability of multiple threads to share the same address space and all available data is something that processes don’t have
  • Threads are more important than processesA lightweightBecause threads are lighter, they are easier to create than processes and easier to undo. On many systems, creating a thread is 10-100 times faster than creating a process.
  • A third reason may be a performance argument. If multiple threads are CPU-intensive, there is no performance gain, but if there is a lot of computation and a lot of I/O processing, having multiple threads on top of each other in these activities can speed up the execution of the application

A process has a thread of execution, often abbreviated as thread. The thread has a program counter to keep track of which instruction to execute next; Threads also have registers that hold variables that the thread is currently using. Threads also have a stack, which keeps track of the program’s execution path. Although threads must be executed in a process, processes and threads are completely different concepts, and they can be handled separately. Processes are used to hold resources together, while threads are scheduled execution entities on the CPU.

Threads add an element to the process model that allows greater independence from each other without interference within the same process. Running multiple threads in parallel in a process is similar to running multiple processes on a computer. In multiple threads, each thread shares the same address space and other resources. In multiple processes, processes share physical memory, disks, printers, and other resources. Threads are called Lightweight processes because they contain some process properties. The term multithreading is also used to describe multiple threads in the same process.

In the figure below we can see three traditional processes, each with its own address space and a single thread of control. Each thread runs in a different address space

In the figure below, we can see that there is a process with three threads. Each thread runs in the same address space.

Virtual memory

The basic idea of virtual memory is that each program has its own address space, which is divided into blocks called pages. Each page is a contiguous range of addresses. These pages are mapped to physical memory, but not all pages must be in memory to run the program. When a program references a portion of the address space in physical memory, the hardware immediately performs the necessary mapping. When a program references a portion of the address space that is not in physical memory, it is the responsibility of the operating system to load the missing portion into physical memory and re-execute the failed instruction.

In a sense, the virtual address is an overview of the base address register and the indexed register. The 8088 has separate base address registers (but not indexing registers) for text and data.

With virtual memory, you can map the entire address space into physical memory in tiny units, rather than just relocating the Text and data areas. We will explore how virtual memory is implemented.

Virtual memory is well suited for use in multiprogramming systems, where fragments of many programs are kept in memory at the same time and the CPU can be handed over to another process while one program waits for part of it to be read into memory.

file

Files are logical units of information created by a process. A disk can contain thousands or even millions of files, and each file is independent of the others. It is an abstraction mechanism that provides a way to store information and read it later.

Network communication

Modern systems do not exist on their own, so they are often connected to other systems through networks. Viewed as a separate system, the network can be viewed as an I/O device, as shown in the figure below

When the system copies a string of bytes from main memory to a network adapter, the data flows across the network to another machine, not to the local disk drive. Similarly, a system can read data sent by other systems and copy the data into its own main memory.

With the advent of the Internet, the case for copying data from one host to another has become one of the most important uses. For example, applications like E-mail, instant messaging, FTP, and Telnet are all based on the ability to replicate information over the network.

The full text.

Article Reference:

En.wikipedia.org/wiki/Locali…

En.wikipedia.org/wiki/Arithm…

Understanding Computer Systems in Depth. Third edition

Github.com/keithnull/T…

ascii.911cha.com/?year=%23