Linux core (1)
Www.ibmtc.pku.edu.cn/crs/kernel/kernel.htm
(This article allows for academic purposes.)
the second part
Linux mystery
Linux and many software issued under the protection of GNU General Public License Terms, often being mistakenly considered "free" software, but the "free" of "Free" in the GNU GPL clause is actually "freedom", that is, said, The user has the right to free distribution software, modify the source code, but require the user to follow this GPL clause and should give other users with the same rights.
The Rapid Growth of Linux and the "free" right of users enjoy the new version of the new version of the program. Nowadays, Chinese users can also freely get Linux and the source code of many applications or libraries, but how can I use Linux to maximize? Obviously, impact the world Linux's heat waves, not only because of its low cost and high performance, mainly in its true open structure and the standards followed, as well as the beliefs pursued by Linux enthusiasts and developers. Due to many reasons, Linux has been promoted in China than for several years than foreign countries. Fortunately, more software enthusiasts have begun to learn Linux since last year. However, only understanding and mastering Linux is far enough, to truly use Linux and play its most potential, you need to deeply understand the Linux system and its kernel structure.
This part discusses the contents of the Linux kernel, discusses the contents of memory management, process, inter-process communication, etc. Introduce the corresponding system tool while introducing the kernel components. In addition, content related to boot, login, and security is also introduced. The contents of each chapter are relatively independent. Through this part of learning, readers can have a deep understanding of Linux, which can make a solid foundation for system management, application development and software development.
Read this part, first ask the reader to master the basic Linux commands and have a certain amount of operation experience, and readers need a certain PC hardware and software knowledge, especially C language and data structures. Beginners can refer to the first part of this book. In addition, the Linux kernel is constantly evolving, and the content described in this section is based on 2.0.xx version. When this book is written, the popular Linux release version is up to 2.0.36, while the highest stability is 2.2.3.
Chapter nine
Linux system overview
Before you understand the Linux mystery, this chapter first introduces the reader's simple introduction to the components of Linux and as the operating system, Linux provides users with the user.
9.1 Concepts and components of the operating system
From a programmer's perspective, the operating system provides an extension or virtual computing platform with computer hardware equivalents. It abstracts many hardware details, and the program can perform data processing in a unified manner, and the programmer can avoid many hardware details. From another perspective, ordinary users see the operating system as a resource manager. With its help, users can organize their own data in a certain way to complete their work and share resources with others. .
In fact, the operating system is generally composed of kernels and some system programs, and there are some applications to help users complete specific tasks. The kernel is the soul of the operating system. It is responsible for managing files, memory, responsible for launching and running procedures, responsible for receiving and transmitting packets from the network. All in all, the operating system is actually an abstract resource operation to interface between specific hardware operation details. For multi-user operating systems such as Linux, it also needs to avoid direct access to hardware and prevent mutual interference between users.
The system program and all other programs run on the kernel, the interface between the program and the kernel is defined by a set of "abstract instructions" provided by the operating system, which is called "system call". All procedures running on the kernel can be divided into two categories: system programs and user programs, but they are running under "User Mode". The boundaries between system programs and user programs are blurred. System programs generally refer to programs that are indispensable to run systems, such as the shell in Linux; and the user program provides users with specific functions, such as word handles or gaming programs. Actual operating system often contains some tool programs (such as compilers) and some online documents. 9.2 An important part of the Linux kernel
The Linux kernel consists of the following parts: memory management, process management, device drivers, file systems, and network management. The following is a brief introduction to the main components of the core.
9.2.1 Memory Management
For any computer, its memory and other resources are limited. In order to make limited physical memory meet the big demand for the memory, Linux uses memory management methods called "virtual memory". Linux divides memory to "memory pages", during the system run, the application's demand for memory is greater than physical memory, Linux can swap the temporary memory page to the hard disk, so that the idle memory page can Meet the memory needs of the application, but the application will not notice the occurrence of memory exchange.
The details of memory management are described in Chapter 10.
9.2.2 process
The process is actually a run entity for a particular application. In the Linux system, you can run multiple processes at the same time, Linux implements "multitasking" by running these processes in a short time interval. This short time interval is called "time slice", which makes the process turning the process of "scheduling", and the schedule is called the scheduler. Through multi-task mechanism, each process can think that only yourself exclusive computer, thereby simplifying the programming. Each process has its own separate address space and can only be accessed by this process, so that the operating system avoids the mutual interference between the processes and the "bad" procedure to harm the system.
In order to complete a particular task, it is sometimes necessary to integrate two programs, such as one program output text, and another program sorted the text. To this end, the operating system also provides communication mechanism between processes to help complete such tasks. There are signal, pipeline, shared memory, semaphore, and socket, etc. in Linux.
The details of the process and inter-process communication are described in Chapter 11.
9.2.3 Device Driver
The device driver is the main part of the Linux kernel. Similar to the other parts of the operating system, the device driver runs in a high privilege-level processor environment, so that the hardware can be operated directly, but because of this, any device driver error can cause the operating system to crash. The device driver actually controls the interaction between the operating system and the hardware device. The device driver provides a set of operating systems to complete the interaction between the operation and the operating system, and the specific operation details associated with the hardware are done by the device driver. In general, the control chip of the device driver and the device is related to, for example, if the computer hard disk is a SCSI hard drive, you need to use the SCSI driver instead of the IDE driver.
Details about hardware and device drivers are described in Chapter 12.
9.2.4 file system
Unlike the operating systems such as DOS, a separate file system in the Linux operating system is not identified by the drive letter or drive name (such as A: or C:, etc.). In contrast, like UNIX operating systems, the Linux operating system combines a separate file system into a hierarchical tree structure and represents this file system by a separate entity. Linux puts the new file system through a one called "Hame" or "Mount" to a directory, so that different file systems combine into a whole. An important feature of the Linux operating system is that it supports many different types of file systems. The most common file system in Linux is EXT2, which is also a file system of Linux. But Linux can also support different types of file systems such as FAT, VFAT, FAT32, Minix, which can be easily exchanged with other operating systems. Since Linux supports many different file systems, and organizes them into a unified virtual file system, users and processes do not need to know the file system type of files, but only need to use files in the EXT2 file system. they. In fact, Linux uses the virtual file system to separate the specific implementation details of file system operations and different file systems.
The details of the document system are described in Chapter 13.
9.2.5 Network
Linux and the network are almost synonymous. Linux is actually the product of Internet and WWW. Linux developers use network and web for information exchange, while Linux itself is used in network support of various organizations. As we all know, the TCP / IP protocol is the standard protocol of the Internet, and is also a factual industrial standard. Linux's network implementation supports BSD sockets to support all TCP / IP protocols. The network part of the Linux kernel consists of BSD socket, network protocol layer, and network device driver.
The basic concepts of the network are described in Chapter 14.
9.2.6 Other
In addition to the above primary components, the kernel also includes some general tasks and mechanisms that allow each of the Linux kernels to be effectively combined, and they are the necessary guarantees for the above main part.
In terms of structure, the operating system has a minimalian structure and a single block structure, Windows NT and Minix are a typical micro-kernel operating system, while Linux is an operating system for a single block. Micro-kernel structures are convenient to add new components in the kernel, and the single structure is not easy to do. To this end, Linux supports a module that can be dynamically loaded and unloaded. With modules, it is convenient to add new components or unloading the kernel components that are no longer needed in the kernel.
Chapter 15 tells the concept of the above kernel mechanism and module.
Figure 9-1 illustrates an important part of the above Linux kernel and its relationship.
9.3 Main Services of Linux systems
The main services described in this section are also the main services provided by the UNIX system, and these services will be described in detail later.
9.3.1 init
INIT is started as the first process of each UNIX system and is also the last job of the kernel during the boot process. After init starts, it will continue the boot process, including check and load file system, and launch daemon.
And init related concepts are "single user mode" and "multi-user mode". In "single user mode", only root can log in to the system. These two concepts are often attached to the "run level", and the two patterns correspond to the two run levels of INIT, but the run level of the init has 6, and the other run levels can be used by the application.
When the system is turned off, INIT is responsible for "killing" all the processes, unloading all file systems and terminates the execution of the processor.
The details of the guidance and shutdown and init will be described in Chapter 16.
9.3.2 Terminal Login
Sign in and log in and and when logging in (not running x window) by serial lines (not running X Window). INIT launches a separate GetTy instance for each terminal. Getty reads the username and launch the Login program. If the username and password are correct, login will start the shell program. If the user logs out, or if the login is terminated due to incorrect username and password, INIT will launch a new Getty. The user's login is completely completed by the system program, the kernel does not participate in the login process. Figure 9-1 Important components of the Linux kernel
Details about login and logout will be described in this section.
9.3.3 Syslog
Core and system programs often generate some errors, warnings, or other types of messages. These messages are usually very important for system management, so it is necessary to record these messages for future viewing. This work is done by the syslog program. Syslog can make a message in accordance with the principles of the message and the importance of the message to facilitate viewing.
9.3.4 Period Command Execution: CRON and AT
System administrators or users often make some periodic tasks to complete in order to reduce daily workload, for example, the files in the / tmp in the / tmp every week.
CRON is a daemon that can complete similar work. Each user has a crontab file, and the user can list the commands to be executed and the time to execute, and CRON completes the appropriate work according to the specified time and command.
AT and cron are similar, but the commands executed by the AT can only be executed without repeated execution.
9.3.5 Graphical User Interface
Linux does not directly combine the user interface into the operating system kernel. In contrast, the user interface is implemented by the user-level program. The user interface includes a character user interface and a graphical user interface. Linux mainly uses the X WINDOW system (referred to as x) as its graphical environment, but X is just a window system, which only manages the settlement, destruction, and graphics output in the window, and the actual window management program requires separate implementation . Different window management programs mean different graphical user interface style, although there is a lot of flexibility, it makes it difficult for the learning of the user interface. In Linux, common X-based user interface styles are XWMF, XWMF2, MotiF, and Afterstep.
9.3.6 Network
As mentioned earlier, Linux supports a complete TCP / IP protocol, many basic operating system services, such as file systems, printing, backups, etc., can be implemented through network, which makes the network centering management.
9.3.7 Network Login
Network login and usual login have some differences. Since the number of physical lines is limited, the number of logins through the serial line is limited, but each network login can be represented by a separate virtual network connection, so the number of network logins is almost unrestricted. For this reason, it is impossible to log in to each possible network to run separate Getty as a terminal login. At the same time, the way network login is also diverse, for example, using Telnet login, you can also use rlogin login.
To this end, for all network login methods, the system runs a daemon (inetd), this daemon monitors all network logins. When a user tries to log in, the daemon will launch a new own instance to process login, and the original process continues to listen. The new process is similar to getty.
9.3.8 Network File System
The network service can be shared through the network service, which is usually used by the network file system developed by Sun Microsystem, namely NFS. With the help of the network file system, the program can handle files on other computers like processing a local file, so that information is shared very simple because it does not need to make additional modifications to the program. 9.3.9 Other
Email is a common information exchange method between computers. The system provides many programs to send and receive emails.
The printer is a relatively special device because there is only one user using the printer at the same time. The system coordinates multiple users to the printer by the print queue. When the printer is printing, other print jobs queued in the print queue. After the last print job is completed, the next print job is automatically started. The management software of the print queue can remove the printed output of the program to the disk, so that the program that outputs the printed can continue to operate without waiting for the printing.
9.4 Standard layout of the directory tree
For most Linux release, the directory tree layout of the file system follows the FSSTND standard. Follow this standard to contribute or transplant software, and it is also conducive to system management and maintenance. Although the FSSTND standard is not enforced, this standard will bring a lot of benefits to our daily work. This section briefly describes the FSSTND-based Linux directory tree layout, and see the fsstnd documentation for details.
The complete directory tree can be divided into small parts, which can be stored separately on their own disks or partitions. Thus, relatively stable portions and frequently varied portions can be individually stored in different partitions, thereby facilitating backup or system management. The main parts of the catalog tree include root, / usr, / var, / home (Figure 9-2). Such layouts are convenient to some parts of the file system between the Linux computer.
Figure 9-2 Different parts of the Linux directory tree
Different parts of the directory tree are given by Table 9-1 in the Linux system.
Table 9-1 Different parts of the directory tree in the role played in the Linux system
name
Character
root
The root file system contains content related to a specific computer, so the root file system is typically saved in the local hard disk of the computer, but can also be saved in the RAMDisk or network drive. The contents of the root file system include: Boot system's must-have file, file system's mount information, and system repair tools and backup tools.
/ usr
/ USR file system contains command programs, libraries, manuals, and other documents that do not need to be modified in the usual operation. Since the file in / usr is not related to a specific computer, it is not modified in the usual use, so these files can be shared through the network. In this way, after the root is installed, all computers that share this file system can use new software.
/ var
/ VAR file system contains frequent changes, such as printers, emails, news, etc., log files, formatted man page, and temporary files, etc. According to traditional habits, / var files are usually included in / usr, but the / usr file system cannot be hung as a read-only file system.
/ home
/ HOME includes the user's home directory, the user's data is saved in its home directory, and / Home can be divided into different file systems, such as / home / students, and / home / teachers, etc. if necessary. Place / Home to facilitate backup of user data in a separate file system.
/ proc
The / PROC file system does not save on disk, in contrast, the operating system creates this file system in memory. It contains some and system-related information, such as CPU, DMA channel, and interrupt usage information.
In the above table, the different parts of the directory tree is also called a file system, but they don't have to be saved in a separate file system.
9.4.1 root file system
The root file system contains some key files, and its content is also small. If the root file system is destroyed, the operating system cannot be guided correctly. The files and directories included in the root file system are shown in Table 9-2. Table 9-2 Files and directories in the root file system
/ vmlinuz
file. The standard boot map of the system is usually in the form of compression.
/ bin
Contains commands necessary for boot process, or use them by ordinary users.
/ sbin
And / bin, although the commands can be used by ordinary users, but because these commands belong to the system-level command, there is no special demand without using the commands.
/ ETC
Contains profiles related to a particular computer.
/ root
Root user's home directory.
/ lib
The program in the ROOT file system is saved in this directory.
/ lib / modules
Contains the loadable kernel module.
/ dev
Contains device files.
/ TMP
Contains temporary files. The program running after boot should save the file in / var / tmp because the available space is large.
/ boot
Contains the file to boot the loader to use. The kernel image is usually saved in this directory. Because multiple kernel images take up a lot of disk space, you can place this directory in a separate file system.
/ MNT
Temporary file system flushing point.
/ usr, / var,
/ home, / proc
Hamed points for other file systems.
9.4.2 / USR file system
The / usr file system contains all program files and online documents, so its content is usually large. The file and directory included in the / usr file system are shown in Table 9-3.
Table 9-3 / USR file system
/ usr / x11r6
Contains all files for the X window system.
/ usr / x386
Similar to / usr / x11r6, but contains X11 Release 5.
/ usr / bin
Most user commands. Other commands are included in / bin and / usr / local / bin.
/ usr / sbin
System management commands that are not needed in the root file system.
/ usr / man, / usr / info,
/ usr / doc
The man page, GNU INFO documentation, and other miscellaneous documents, respectively.
/ usr / include
C language header file.
/ usr / lib
The unchanged data file used by the program and subsystems.
/ usr / local
Local floors and other files of other files.
9.4.3 / var file system
/ var usually contains files that frequently change during system operation. The directory included in the / VAR file system is shown in Table 9-4.
Table 9-4 / VAR file system
/ VAR / CATMAN
Format the cache of the handbook.
/ VAR / LIB
Contains files that are often changed during system runtime.
/ VAR / LOCAL
Install the variable data of the program in / usr / local.
/ VAR / LOCK
Contains lock files.
/ VAR / LOG
The log file generated by the program is included.
/ VAR / RUN
This directory contains information related to the system before the next boot.
/ var / spot
A spool directory for queuing tasks.
/ VAR / TMP
Contains large temporary files, or temporary files for longer save time.
9.4.4 / proc file system
The / PROC file system does not save on disk, in contrast, the operating system creates this file system in memory. The files and directories contained in the / proc file system are shown in Table 9-5.
Table 9-5 / PROC file system files and directories
/ proc / 1
The process information of the process number 1 is included in this directory. Each process has a directory that is named in its own process number in / proc directory.
/ proc / cpuinfo
Information about CPU name, model, performance, and type.
/ proc / devices
The device driver list in the current kernel.
/ Proc / DMA
The currently used DMA channel.
/ proc / filesystems
File system supported by kernels.
/ proc / interrupts
Currently used interrupt information.
/ Proc / Ioports
The currently used I / O port.
/ proc / kcore system physical memory image.