Linux Kernel Core Chinese Manual (2) - Software Foundation

xiaoxiao2021-03-06  42

Software Basic program is a computer instruction combination for performing a specific task. Programs can be written in assembly language, a very low-level computer language, or high-level languages ​​that are not related to machines, such as C language. The operating system is a special program that allows users to run applications, such as electronic meter and text processing, and more. This chapter describes the basic programming principles and introduces the purpose and functionality of the operating system. 2.1 Computer Languages ​​2.1.1. Assembly language CPU reads and executes instructions from memory cannot be understood for humans. They are machine code, accurately tell the computer what to do. For example, the hexadecimal number 0x89E5 is an instruction of Intel 80486, copy the contents of the register ESP to the register EBP. One of the initial software tools in the early computer is the assembler. It reads the source file that humans can read and put it into machine code. The assembly language explicitly handles the operation of the register and the data, and this operation is special for a particular microprocessor. Intel X86 microprocessor assembly language and assembly language of Alpha Ax microprocessors are completely different. The following Alpha AXP assembly code demonstrates how the program can perform: LDR R16, (R15); first line LDR R17, 4 (R15); second lines of BEQ R16, R17, 100; third line STR R17, (R15 ); The fourth line 100:; The first line of the first statement (first line) is loaded into the register 16 in the address specified by the register 15. The second instruction will be loaded into the register 17 in the following memory. The third line comparison register 16 and register 17, if equally, branches to label 100, otherwise, continue to execute the fourth line, save the contents of the register 17 into memory. If the data in the memory is the same, the data is not required. Compilation-level procedures require tips and very lengthy, easy to make mistakes. A small part of the core of the Linux system is written in assembly language, and the use of assembly languages ​​is just to improve efficiency and related to specific microprocessors. 2.1.2 The C Programming Language and Compiler (C Language and Compiler) Writing large programs using assembly language is very difficult, consuming time, easy error, and generated programs cannot be transplanted, can only be bound to a specific processor family. Better choice is to use and have unrelated languages, such as C. C Allows you to describe the program and data to be processed with a logical algorithm. Special programs called Compiler (Compiler) are read into the C program and convert it to assembly language, which generates machine-related code. Good compiler generated assembly instructions can be close to program efficiency written by good assembly programmers. Most Linux cores are written in C language. The following C fragment: if (x! = Y) x = Y; executes the same operation as the assembly code in the previous example. If the contents of the variable X are different, the contents of the variable Y are copied to the variable x. C Code Routine Combine, each routine executing a task. The routine can return any value or data type supported by C. Large programs such as Linux cores consist of many C language modules, each module has their own routines and data structures. These C source code modules constitute a logical function such as a process code for a file system. C Support multiple types of variables. A variable is a specific location in memory and can be referenced by symbolic names. In the above C fragment, X and Y reference the location in memory. The programmer does not need to care about the specific location of the variable in memory, which is the connection program (below) must be processed.

Some variables contain different data such as integers, floating point numbers, and others including pointers. The pointer is a variable that contains addresses in memory in memory. Assume a variable x, located in the memory address 0x80010000, you may have a pointer PX, pointing X. PX may be located at address 0x80010030. The value of PX is the address of the variable x, 0x80010000. C allows you to set the associated variables. For example: struct {INT i; char b;} my_struct; is a data structure called my_struct, including two elements: an integer (32-bit) I and a character (8-bit data) B. 2.1.3 Linkers Connects to connect several target modules and library files together into a separate full program. The target module is a compilation program or a compiler's machine code output, which includes machine code, data, and connection information for the connection program. For example: a target module may include all database functions of the program, while another target module includes a function of processing a command line parameter. The connection program determines the reference relationship between the target module, which determines the routine referenced by one module and the actual location in another module. The Linux core is an independent large program that is connected by multiple target modules. 2.2 What is an operating system (What is an operating system?) No software, the computer is just a bunch of heat-free electronic components. If the hardware is the heart of the computer, the software is its soul. The operating system is a set of system programs that allow users to run applications. The operating system will abstore the hardware of the system, presented before the user and the application, is a virtual machine. It is the characteristics of the software to create a computer system. Most PCs can run a plurality of operating systems, while each operating system is very different from the appearance and feel. Linux consists of a part of different functions, and these parts have the overall combination of the LINUX operating system. The most obvious part of Linux is KERNEL itself, but if there is no shell or libraries, it is useless. In order to understand what is operating system, look at what happened when you enter the easiest command: $ ls mail c images Perl DOCS TCL $ is the message of the login shell output (this is BASH): Shell is waiting for you (user) input command. Enter the character of the LS to initiate the keyboard driver identification input, the keyboard driver passes the identified character to the shell destination. Shell looks for the executive image of the same name. It finds / bin / ls, then calls the core service to load the LS executter into the virtual memory and start execution. The LS executor calls the search file by executing the system of the core. The file system may use the cached file system information or read file information from the disk through the disk device driver, or it may be the detailed information of the remote file accessed by the system through the network device driver. Details of the remote file accessed by this system ( The file system can be remotely installed through the NFS network file system). Regardless of how the file information is obtained, the LS outputs the information, and is displayed on the screen via the display driver. The above process looks quite complicated, but it explains the result of the common collaboration between the various functional modules of the operating system, only this can be provided to you (users) a complete system view. 2.2.1 Memory Management If you have unlimited resources, such as memory, many things that the operating system must do may be redundant. One basic skill in all operating systems is to make a small amount of physical memory works as a quite amount of memory.

The big memory that looks like this surface is called virtual memory, which is when the software is running, let it believe it has a lot of memory. The system divides memory into easy-to-handed pages, switched to the hard disk when the system is running. The application software does not know because the operating system also uses another technology: multi-process. 2.2.2 Processes process can be seen as an executable program, each process is a separate entity that is running a specific program. If you look at your Linux system, you will find that there are many processes running. For example: Entering PS on my system Shows the following process: $ ps pid Tty Stat Time Command 158 pre 1 0:00 -bash 174 pre 1 0:00 sh / usr / x11r6 / bin / startx 175 pre 1 0:00 Xinit / usr / x11r6 / lib / x11 / xinit / xinitrc - 178 pre 1 n 0:00 Bowman 182 pre 1 n 0:01 RXVT -GEOMETRY 120X35-FG White -bg Black 184 pre 1 <0:00 xclock -bg Grey -Geometry -1500-1500 -Padding 0 185 pre 1 <0:00 xload -bg grey -geometry -0-0 -Label XLoad 187 PP6 1 9:26 / bin / bash 202 pre 1 n 0:00 rxvt -geometry 120x35 -fg white -bg black 203 PPC 2 0:00 / bin / bash 1796 pre 1 n 0:00 rxvt -geometry 120x35 -fg white -bg Black 1797 V06 1 0:00 / bin / bash 3056 pp6 3 <0: 02 Emacs Intro / Introduction.Tex 3270 PP6 3 0:00 Ps $ If my system has multiple CPUs, each process may be running on different CPUs. Unfortunately, there is only one, so the operating system uses techniques, running each process in a short period of time. This time period is called a time slice. This skill is called multi-process or scheduling, which deceives every process as if they are the only process. The process is protected from each other, so if a process crashes or cannot work, it will not affect other processes. The operating system is implemented by a separate address space for each process, and the process can only access its own address space. 2.2.3 Device Drivers The device driver consists of the main part of the Linux core. Like other parts of the operating system, they work in a high priority environment. If an error occurs, it may cause serious problems. The device driver controls the interaction between the operating system and its controlled hardware devices. For example: The file system write data blocks to the IDE disk is used using a universal block device interface. Driver control details, and handles and devices. The device driver is related to the specific controller chip it driven, so if your system has an NCR810 SCSI controller, then you need the NCR810 driver. 2.2.4 The FileSystems is the same as Unix. In Linux, the system is not accessible with the device indicator (such as the drive number or drive name), but is connected to a tree structure.

Linux When installing a new file system, install it to the specified installation directory, such as / MNT / CDROM, incorporates it to this single file system tree. An important feature of Linux is that it supports a variety of different file systems. This makes it very flexible and can coexist with other operating systems. Linux's most commonly used file system is ext2, most Linux release supports. The file system will be stored in the system's hard drive and the directory can be provided to the user, allowing users to consider the type of file system or the properties of the underlying physical device. Linux transparently supports multiple file systems (such as MS-DOS and EXT2), collecting all installed files and file systems into a virtual file system. Therefore, users and processes usually do not need to know the type of file system in which the file used is, it is. The block device driver masks the difference between the physical block device type (such as IDE and SCSI). For file systems, physical equipment is a collection of linear data blocks. The block size of different devices may be different. If the floppy drive is typically 512 bytes, and the IDE device is typically 1024 bytes, and the same, for the system of the system, these differences have been masked. EXT2 file system does not care what device it uses, it looks the same. 2.3 KERNET DATA STRUCTURES The operating system must record many of the information about the current state of the system. If something happens in the system, these data structures must be changed accordingly to reflect the current actual situation. For example: When the user logs in to the system, you need to create a new process. The core must create the data structure indicating this new process accordingly, and associate with the data structure of other processes in the system. Most of such data structures are in physical memory and can only be accessed by cores and its subsystems. The data structure includes data and pointers (other data structures or address of routines). At first glance, the data structure used by Linux core may be very confusing. In fact, each data structure has its own purpose, although some data structures are used in multiple subsystems, but in fact they feel more simple than the first time I see. It is understood that the key to Linux core is to understand its data structure and core processing of a large number of functions used in these data structures. This book describes Linux core based on data structure. On the algorithm of each core subsystem, the way to process and their use of the core data structure. 2.3.1 Linked Lists Linux uses a software engineering technology to connect its data structure. In most cases it uses the chain table data structure. If each data structure describes a single instance of an object or an event, such as a process or a network device, the core must be able to find all instances. In the linked list, the root pointer includes the address of the first data structure or unit, and each of the lists in the list contains a pointer to the next element in the list. The next pointer to the last element may make 0 or NULL, indicating that this is the end of the list. In the two-way linked table structure, each element includes not only the pointer of the next element in the list, but also a pointer to the previous element in the list. Using a two-way linked list is easier to add or delete an element in the middle of the list, but this requires more memory access. This is a typical operating system: the memory number or the number of cycles of the CPU. 2.3.2 Hash Tables Link table is a common data structure, but the efficiency of the link table may not be high. If you want to find the specified element, you may have to find a full table to find it. Linux uses another technique: Hashing to solve this limit. Hash Table is an array or a metrics of a pointer. An array or vector table is an object that is sequentially stored in memory. The bookshelf can be said to be an array of books.

转载请注明原文地址:https://www.9cbs.com/read-51961.html

New Post(0)