第 1 章 大局观

At first glance, a modern operating system such as Linux is very complicated, with a dizzying number of pieces simultaneously running and communicating. For example, a web server can talk to a database server, which could in turn use a shared library that many other programs use. But how does it all work?

乍一看,像Linux这样的现代操作系统非常复杂,同时运行和通信的部件数量令人眼花缭乱。

例如,一个Web服务器可以与数据库服务器通信,而后者又可以使用许多其他程序使用的共享库。

但是这一切是如何工作的呢?

The most effective way to understand how an operating system works is through abstraction—a fancy way of saying that you can ignore most of the details. For example, when you ride in a car, you normally don’t need to think about details such as the mounting bolts that hold the motor inside the car or the people who build and maintain the road upon which the car drives. If you’re a passenger in a car, all you really need to know is what the car does (transports you somewhere else) and a few basics about how to use it (how to operate the door and seat belt). But if you’re driving a car, you need to know more. You need to learn how to operate the controls (such as the steering wheel and accelerator pedal) and what to do when something goes wrong.

"了解操作系统工作原理最有效的方法是通过抽象" 这是一种非常常见的说法,即您可以忽略大部分细节。

例如,当您乘坐汽车时,通常不需要考虑固定发动机在汽车内部的螺栓或建造和维护汽车行驶的道路的人员等细节。

如果您是汽车的乘客,您真正需要知道的只是汽车的功能(将您运送到其他地方)以及如何使用它的一些基本知识(如如何操作车门和安全带)。

But if you’re driving a car, you need to know more. You need to learn how to operate the controls (such as the steering wheel and accelerator pedal) and what to do when something goes wrong.

但是如果您驾驶汽车,您需要了解更多。

您需要学习如何操作各种控制装置(如方向盘和油门踏板)以及在发生故障时应该怎么做。

For example, let’s say that the car ride is rough. Now you can break up the abstraction of “a car that rolls on a road” into three parts: a car, a road, and the way that you’re driving. This helps isolate the problem: If the road is bumpy, you don’t blame the car or the way that you’re driving it. Instead, you may want to find out why the road has deteriorated or, if the road is new, why the construction workers did a lousy job.

例如,假设汽车行驶起来很颠簸。现在,您可以将“一辆在道路上行驶的汽车”的抽象分为三个部分:汽车、道路和您的驾驶方式。

这有助于隔离问题:如果道路崎岖不平,您不会责怪汽车或您的驾驶方式。

相反,您可能想要找出道路恶化的原因,或者如果道路是新的,为什么施工工人做得不好。

Software developers use abstraction as a tool when building an operating system and its applications. There are many terms for an abstracted subdivision in computer software, including subsystem, module, and package—but we’ll use the term component in This chapter because it’s simple. When building a software component, developers typically don’t think much about the internal structure of other components, but they do care about what other components they can use and how to use them.

软件开发人员在构建操作系统及其应用程序时使用抽象作为一种工具。

计算机软件中有许多术语用于表示抽象的细分,包括子系统、模块和包,但在本章中我们将使用组件这个简单的术语。

在构建软件组件时,开发人员通常不会过多考虑其他组件的内部结构,但他们确实关心可以使用哪些其他组件以及如何使用它们。

This chapter provides a high-level overview of the components that make up a Linux system. Although each one has a tremendous number of technical details in its internal makeup, we’re going to ignore these details and concentrate on what the components do in relation to the whole system.

本章概述了构成Linux系统的组件。

尽管每个组件在其内部构造中有大量的技术细节,但我们将忽略这些细节,集中关注组件在整个系统中的作用。

1.1 Levels and Layers of Abstraction in a Linux System(Linux 系统中的抽象层级)

Using abstraction to split computing systems into components makes things easier to understand, but it doesn’t work without organization. We arrange components into layers or levels. A layer or level is a classification (or grouping) of a component according to where that component sits between the user and the hardware. Web browsers, games, and such sit at the top layer; at the bottom layer we have the memory in the computer hardware—the 0s and 1s. The operating system occupies most of the layers in between

使用抽象方法将计算系统拆分为多个组件,可以让事情更容易理解,但如果没有组织,就无法发挥作用。

我们将组件划分为不同的层或级。

层或级是根据组件在用户和硬件之间的位置对组件进行的分类(或分组)。

网络浏览器、游戏等位于顶层;底层是计算机硬件中的内存--0 和 1。

操作系统占据了中间的大部分层

A Linux system has three main levels. Figure 1-1 shows these levels and some of the components inside each level. The hardware is at the base. Hardware includes the memory as well as one or more central processing units (CPUs) to perform computation and to read from and write to memory. Devices such as disks and network interfaces are also part of the hardware.

Linux 系统有三个主要级别。

图 1-1 显示了这些层次以及每个层次中的一些组件。

硬件是基础。

硬件包括内存以及一个或多个用于执行计算和读写内存的中央处理器(CPU)。

磁盘和网络接口等设备也是硬件的一部分。

The next level up is the kernel, which is the core of the operating system. The kernel is software residing in memory that tells the CPU what to do. The kernel manages the hardware and acts primarily as an interface between the hardware and any running program

下一级是内核,它是操作系统的核心。

内核是驻留在内存中的软件,它告诉中央处理器该做什么。

内核管理硬件,主要充当硬件与任何运行程序之间的接口

Processes—the running programs that the kernel manages—collectively make up the system’s upper level, called user space. (A more specific term for process is user process, regardless of whether a user directly interacts with the process. For example, all web servers run as user processes.)

进程--内核管理的运行程序--共同构成了系统的上层,称为用户空间。

(进程的一个更具体的术语是用户进程,与用户是否直接与进程交互无关。

例如,所有网络服务器都作为用户进程运行)。

Figure 1-1. General Linux system organization

There is a critical difference between the ways that the kernel and user processes run: The kernel runs in kernel mode, and the user processes run in user mode. Code running in kernel mode has unrestricted access to the processor and main memory. This is a powerful but dangerous privilege that allows a kernel process to easily crash the entire system. The area that only the kernel can access is called kernel space

内核和用户进程运行的方式之间存在着关键的区别:内核在内核模式下运行,而用户进程在用户模式下运行。

在内核模式下运行的代码可以无限制地访问处理器和主内存。

这是一种强大但危险的特权,允许内核进程轻易地使整个系统崩溃。

只有内核可以访问的区域称为内核空间。

User mode, in comparison, restricts access to a (usually quite small) subset of memory and safe CPU operations. User space refers to the parts of main memory that the user processes can access. If a process makes a mistake and crashes, the consequences are limited and can be cleaned up by the kernel. This means that if your web browser crashes, it probably won’t take down the scientific computation that you’ve been running in the background for days

相比之下,用户模式限制了对内存和安全CPU操作的访问(通常是一个相当小的子集)。

用户空间指的是用户进程可以访问的主内存的部分。

如果一个进程出现错误并崩溃,后果是有限的,并且可以由内核清理。

这意味着如果你的网络浏览器崩溃了,它可能不会影响你在后台运行了几天的科学计算。

In theory, a user process gone haywire can’t cause serious damage to the rest of the system. In reality, it depends on what you consider “serious damage,” as well as the particular privileges of the process, because some processes are allowed to do more than others. For example, can a user process completely wreck the data on a disk? With the correct permissions, yes—and you may consider this to be fairly dangerous. There are safeguards to prevent this, however, and most processes simply aren’t allowed to wreak havoc in this manner.

从理论上讲,一个失控的用户进程不能对系统的其余部分造成严重的破坏。

但实际上,这取决于你对“严重破坏”的定义,以及进程的特权,因为有些进程被允许做更多的事情。

例如,一个用户进程能否完全破坏磁盘上的数据?在正确的权限下,是的,你可能认为这相当危险。

然而,有防护措施来防止这种情况发生,大多数进程并不被允许以这种方式制造混乱。

1.2 Hardware: Understanding Main Memory(硬件:了解主存储器)

Of all of the hardware on a computer system, main memory is perhaps the most important. In its most raw form, main memory is just a big storage area for a bunch of 0s and 1s. Each 0 or 1 is called a bit. This is where the running kernel and processes reside—they’re just big collections of bits. All input and output from peripheral devices flows through main memory, also as a bunch of bits. A CPU is just an operator on memory; it reads its instructions and data from the memory and writes data back out to the memory

在计算机系统的所有硬件中,主存储器可能是最重要的部分。

以最原始的形式来看,主存储器只是一个用于存储一串0和1的大容器。

每个0或1被称为一个比特。

运行的内核和进程都驻留在这里,它们只是一堆比特的集合。

所有来自外围设备的输入和输出也都以一串比特的形式通过主存储器流动。

CPU只是内存上的操作员;它从内存中读取指令和数据,并将数据写回内存。

You’ll often hear the term state in reference to memory, processes, the kernel, and other parts of a computer system. Strictly speaking, a state is a particular arrangement of bits. For example, if you have four bits in your memory, 0110, 0001, and 1011 represent three different states.

在涉及到内存、进程、内核和计算机系统的其他部分时,你经常会听到“状态”这个术语。

严格来说,状态是比特的一种特定排列。例如,如果你的内存中有四个比特,0110、0001和1011代表了三种不同的状态。

When you consider that a single process can easily consist of millions of bits in memory, it’s often easier to use abstract terms when talking about states. Instead of describing a state using bits, you describe what something has done or is doing at the moment. For example, you might say “the process is waiting for input” or “the process is performing Stage 2 of its startup.”

考虑到一个单独的进程在内存中很容易包含数百万个比特,当谈论状态时,使用抽象的术语往往更容易。

与其使用比特来描述状态,不如描述某个事物在某一时刻所做的或正在做的事情。

例如,你可以说“该进程正在等待输入”或“该进程正在执行其启动的第二阶段”。

NOTE Because it’s common to refer to the state in abstract terms rather than to the actual bits, the term image refers to a particular physical arrangement of bits.

注意,由于通常用抽象的术语而不是实际的位来引用状态,所以术语“镜像”指的是位的特定物理排列。

1.3 The Kernel(内核)

Why are we talking about main memory and states? Nearly everything that the kernel does revolves around main memory. One of the kernel’s tasks is to split memory into many subdivisions, and it must maintain certain state information about those subdivisions at all times. Each process gets its own share of memory, and the kernel must ensure that each process keeps to its share.

为什么我们要谈论主存和状态呢?几乎内核所做的一切都围绕着主存展开。

内核的任务之一就是将内存分割成许多子区域,并且必须始终维护有关这些子区域的某些状态信息。

每个进程都有自己的内存份额,内核必须确保每个进程都遵守自己的份额。

The kernel is in charge of managing tasks in four general system areas:

内核负责管理四大系统领域的任务:

o Processes. The kernel is responsible for determining which processes are allowed to use the CPU.
o Memory. The kernel needs to keep track of all memory—what is currently allocated to a particular process,
what might be shared between processes, and what is free.
o Device drivers. The kernel acts as an interface between hardware (such as a disk) and processes. It’s
usually the kernel’s job to operate the hardware.
o System calls and support. Processes normally use system calls to communicate with the kernel.
We’ll now briefly explore each of these areas.

  • 进程:内核负责确定哪些进程被允许使用 CPU。
  • 内存:内核需要跟踪所有的内存,包括当前分配给特定进程的内存、可能在进程之间共享的内存以及空闲的内存。
  • 设备驱动程序:内核充当硬件(如磁盘)和进程之间的接口。通常,内核负责操作硬件。
  • 系统调用和支持:进程通常使用系统调用与内核进行通信。

We’ll now briefly explore each of these areas.

下面我们简要探讨一下这些领域。

NOTE If you’re interested in the detailed workings of a kernel, two good textbooks are Operating System Concepts, 9th edition, by Abraham Silberschatz, Peter B. Galvin, and Greg Gagne (Wiley, 2012) and Modern Operating Systems, 4th edition, by Andrew S. Tanenbaum and Herbert Bos (Prentice Hall, 2014)

注意:如果你对内核的详细工作原理感兴趣,可以参考以下两本好书:

《操作系统概念》(第9版),作者:Abraham Silberschatz、Peter B. Galvin 和 Greg Gagne(Wiley,2012)和

现代操作系统》(第4版),作者:Andrew S. Tanenbaum 和 Herbert Bos(Prentice Hall,2014)。

1.3.1 Process Management (流程管理 )

Process management describes the starting, pausing, resuming, and terminating of processes. The concepts behind starting and terminating processes are fairly straightforward, but describing how a process uses the CPU in its normal course of operation is a bit more complex.

进程管理描述了进程的启动、暂停、恢复和终止。

启动和终止进程的概念相对简单,但描述进程在正常运行过程中如何使用CPU则更为复杂。

On any modern operating system, many processes run “simultaneously.” For example, you might have a web browser and a spreadsheet open on a desktop computer at the same time. However, things are not as they appear: The processes behind these applications typically do not run at exactly the same time.

在任何现代操作系统上,许多进程“同时”运行。

例如,你可能同时在桌面电脑上打开一个网页浏览器和一个电子表格。

然而,事实并非如此:这些应用程序背后的进程通常并不完全同时运行。

Consider a system with a one-core CPU. Many processes may be able to use the CPU, but only one process may actually use the CPU at any given time. In practice, each process uses the CPU for a small fraction of a second, then pauses; then another process uses the CPU for another small fraction of a second; then another process takes a turn, and so on. The act of one process giving up control of the CPU to another process is called a context switch.

考虑一个单核CPU的系统。

许多进程可能能够使用CPU,但在任何给定时间只有一个进程实际上在使用CPU。

实际上,每个进程会使用CPU一小部分时间,然后暂停;

然后另一个进程使用CPU一小部分时间;

然后又轮到另一个进程,依此类推。

一个进程将CPU的控制权交给另一个进程的行为被称为上下文切换。

Each piece of time—called a time slice—gives a process enough time for significant computation (and indeed, a process often finishes its current task during a single slice). However, because the slices are so small, humans can’t perceive them, and the system appears to be running multiple processes at the same time (a capability known as multitasking).

每个时间片段(称为时间片)给予一个进程足够的时间进行重要的计算(实际上,一个进程通常会在一个时间片内完成当前任务)。

然而,由于时间片非常短,人类无法感知它们,并且系统看起来像是同时运行多个进程(这种能力被称为多任务处理)。

The kernel is responsible for context switching. To understand how this works, let’s think about a situation in which a process is running in user mode but its time slice is up.

内核负责上下文切换。

为了理解这是如何工作的,让我们思考一种情况:一个进程在用户模式下运行,但它的时间片已经用完。

Here’s what happens:

下面是发生的情况:

  1. The CPU (the actual hardware) interrupts the current process based on an internal timer, switches into
    kernel mode, and hands control back to the kernel.
  2. The kernel records the current state of the CPU and memory, which will be essential to resuming the process that was just interrupted.
  3. The kernel performs any tasks that might have come up during the preceding time slice (such as collecting data from input and output, or I/O, operations).
  4. The kernel is now ready to let another process run. The kernel analyzes the list of processes that are ready to run and chooses one.
  5. The kernel prepares the memory for this new process, and then prepares the CPU.
  6. The kernel tells the CPU how long the time slice for the new process will last.
  7. The kernel switches the CPU into user mode and hands control of the CPU to the process.
  8. CPU(实际的硬件)根据内部计时器中断当前进程,切换到内核模式,并将控制权交还给内核。
  9. 内核记录CPU和内存的当前状态,这对于恢复刚刚被中断的进程是必要的。
  10. 内核执行在前一个时间片段中可能出现的任何任务(例如从输入输出(I/O)操作中收集数据)。
  11. 现在内核准备让另一个进程运行。内核分析准备好运行的进程列表并选择一个进程。
  12. 内核为这个新进程准备内存,然后准备CPU。
  13. 内核告诉CPU新进程的时间片持续多长时间。
  14. 内核将CPU切换到用户模式,并将CPU的控制权交给进程。

The context switch answers the important question of when the kernel runs. The answer is that it runs between process time slices during a context switch.

上下文切换回答了内核何时运行的重要问题。

答案是在上下文切换期间,在进程时间片之间运行。

In the case of a multi-CPU system, things become slightly more complicated because the kernel doesn’t need to relinquish control of its current CPU in order to allow a process to run on a different CPU. However, to maximize the usage of all available CPUs, the kernel typically does so anyway (and may use certain tricks to grab a little more CPU time for itself).

在多CPU系统的情况下,情况变得稍微复杂,因为内核不需要放弃对当前CPU的控制权以便让一个进程在另一个CPU上运行。然而,为了最大限度地利用所有可用的CPU,内核通常会这样做(并可能使用某些技巧来获取更多的CPU时间)。

1.3.2 Memory Management(内存管理)

Because the kernel must manage memory during a context switch, it has a complex job of memory management. The kernel’s job is complicated because the following conditions must hold:

由于内核在上下文切换期间必须管理内存,所以它的内存管理工作变得复杂。内核的工作复杂是因为必须满足以下条件:

o The kernel must have its own private area in memory that user processes can’t access.
o Each user process needs its own section of memory.
o One user process may not access the private memory of another process.
o User processes can share memory.
o Some memory in user processes can be read-only.
o The system can use more memory than is physically present by using disk space as auxiliary.

o 内核必须在内存中拥有自己的私有区域,用户进程无法访问。
o 每个用户进程需要自己的内存区域。
o 一个用户进程不能访问另一个进程的私有内存。
o 用户进程可以共享内存。
o 用户进程的某些内存可以是只读的。
o 系统可以通过使用磁盘空间作为辅助内存来使用超过物理内存的内存。

Fortunately for the kernel, there is help. Modern CPUs include a memory management unit (MMU) that enables a memory access scheme called virtual memory. When using virtual memory, a process does not directly access the memory by its physical location in the hardware. Instead, the kernel sets up each process to act as if it had an entire machine to itself. When the process accesses some of its memory, the MMU intercepts the access and uses a memory address map to translate the memory location from the process into an actual physical memory location on the machine. The kernel must still initialize and continuously maintain and alter this memory address map. For example, during a context switch, the kernel has to change the map from the outgoing process to the incoming process.

幸运的是,对于内核来说,有帮助可寻。

现代的CPU包括一个内存管理单元(MMU),它可以实现一种称为虚拟内存的内存访问方案。

在使用虚拟内存时,进程不会直接通过硬件中的物理位置来访问内存。

相反,内核会为每个进程设置一个仿佛拥有整个机器的环境。

当进程访问其某些内存时,MMU会拦截访问,并使用内存地址映射将进程的内存位置转换为实际的物理内存位置。

内核仍然必须初始化、持续维护和修改这个内存地址映射。

例如,在上下文切换期间,内核必须将映射从正在执行的进程切换到即将执行的进程。

NOTE The implementation of a memory address map is called a page table.

注意 内存地址映射的实现称为页表。

你将在第8章中了解更多关于如何查看内存性能的内容。

1.3.3 Device Drivers and Management(设备驱动程序和管理)

The kernel’s role with devices is pretty simple. A device is typically accessible only in kernel mode because improper access (such as a user process asking to turn off the power) could crash the machine. Another problem is that different devices rarely have the same programming interface, even if the devices do the same thing, such as two different network cards. Therefore, device drivers have traditionally been part of the kernel, and they strive to present a uniform interface to user processes in order to simplify the software developer’s job.

内核在设备方面的作用非常简单。设备通常只能在内核模式下访问,因为不正确的访问(例如用户进程请求关闭电源)可能会导致系统崩溃。

另一个问题是,即使设备执行相同的功能(例如两个不同的网络卡),它们的编程接口通常也不相同。

因此,设备驱动程序一直是内核的一部分,并且它们致力于向用户进程提供统一的接口,以简化软件开发人员的工作。

1.3.4 System Calls and Support(系统调用和支持)

There are several other kinds of kernel features available to user processes. For example, system calls (or syscalls) perform specific tasks that a user process alone cannot do well or at all. For example, the acts of opening, reading, and writing files all involve system calls.

用户进程可以使用几种其他类型的内核功能。例如,系统调用(或syscalls)执行用户进程无法很好或根本无法执行的特定任务。例如,打开、读取和写入文件都涉及系统调用。

Two system calls, fork() and exec(), are important to understanding how processes start up:

了解进程启动的重要内容是两个系统调用:fork()和exec():

o fork() When a process calls fork(), the kernel creates a nearly identical copy of the process.
o exec() When a process calls exec(program), the kernel starts program, replacing the current
process.

  • fork() 当一个进程调用fork()时,内核会创建一个几乎完全相同的进程副本。
  • exec() 当一个进程调用exec(program)时,内核会启动program,并替换当前进程。

Other than init (see Chapter 6), all user processes on a Linux system start as a result of fork(), and most of the time, you also run exec() to start a new program instead of running a copy of an existing process. A very simple example is any program that you run at the command line, such as the ls command to show the contents of a directory. When you enter ls into a terminal window, the shell that’s running inside the terminal window calls fork() to create a copy of the shell, and then the new copy of the shell calls exec(ls) to run ls. Figure 1-2 shows the flow of processes and system calls for starting a program like ls.

除了init(参见第6章)之外,Linux系统上的所有用户进程都是通过fork()启动的,大部分时间,您也会使用exec()来启动一个新程序,而不是运行现有进程的副本。

一个非常简单的例子是在命令行中运行的任何程序,比如ls命令来显示目录的内容。

当您在终端窗口中输入ls时,终端窗口内运行的shell会调用fork()来创建一个shell的副本,然后新的shell副本会调用exec(ls)来运行ls。

图1-2显示了启动类似ls的程序的进程和系统调用的流程。

Figure 1-2. Starting a new process

Figure 1-2. Starting a new process

NOTE System calls are normally denoted with parentheses. In the example shown in Figure 1-2, the process asking the kernel to create another process must perform a fork() system call. This notation derives from the way the call would be written in the C programming language. You don’t need to know C to understand this book; just remember that a system call is an interaction between a process and the kernel. In addition, this book simplifies certain groups of system calls. For example, exec() refers to an entire family of system calls that all perform a similar task but differ in programming.

系统调用通常用括号表示。

图1-2(上图)所示的例子中,要求内核创建另一个进程的进程必须执行fork()系统调用。

这种表示法源于C编程语言中的写法。你不需要了解C语言就能理解本书;只需记住系统调用是进程和内核之间的交互。

此外,本书简化了某些系统调用的组合。例如,exec()指的是一整组执行类似任务但编程不同的系统调用。

The kernel also supports user processes with features other than traditional system calls, the most common of which are pseudodevices. Pseudo-devices look like devices to user processes, but they’re implemented purely in software. As such, they don’t technically need to be in the kernel, but they are usually there for practical reasons. For example, the kernel random number generator device (/dev/random) would be difficult to implement securely with a user process.

内核还通过除传统系统调用之外的功能来支持用户进程,其中最常见的是伪设备。

伪设备在用户进程看起来像设备,但它们纯粹是通过软件实现的。

因此,它们在技术上不一定需要在内核中,但通常出于实际原因而存在。

例如,内核的随机数生成器设备(/dev/random)在用户进程中要实现安全性较困难。

NOTE Technically, a user process that accesses a pseudodevice still has to use a system call to open the device, so processes can’t entirely avoid system calls.
注意
从技术上讲,访问伪设备的用户进程仍然需要使用系统调用来打开设备,因此进程无法完全避免系统调用。

1.4 User Space(用户空间)

As mentioned earlier, the main memory that the kernel allocates for user processes is called user space. Because a process is simply a state (or image) in memory, user space also refers to the memory for the entire collection of running processes. (You may also hear the more informal term userland used for user space.)

正如之前提到的,内核为用户进程分配的主存被称为用户空间。

因为进程只是内存中的一个状态(或图像),用户空间也指的是整个运行进程的内存。

(你也可能听到更非正式的术语“用户空间”用于指代用户空间。)

Most of the real action on a Linux system happens in user space. Although all processes are essentially equal from the kernel’s point of view, they perform different tasks for users. There is a rudimentary service level (or layer) structure to the kinds of system components that user processes represent. Figure 1-3 shows how an example set of components fit together and interact on a Linux system. Basic services are at the bottom level (closest to the kernel), utility services are in the middle, and applications that users touch are at the top. Figure 1-3 is a greatly simplified diagram because only six components are shown, but you can see that the components at the top are closest to the user (the user interface and web browser); the components in the middle level has a mail server that the web browser uses; and there are several smaller components at the bottom.

Linux系统上的大部分真正活动都发生在用户空间。

虽然从内核的角度来看,所有进程基本上是平等的,但它们为用户执行不同的任务。

用户进程所代表的系统组件具有一种基本的服务级别(或层次)结构。

图1-3展示了一个示例组件集如何在Linux系统上配合并相互作用。基本服务位于底层(最接近内核),实用服务位于中间,用户接触的应用程序位于顶层。

图1-3是一个大大简化的图表,因为只显示了六个组件,但你可以看到顶部的组件最接近用户(用户界面和网页浏览器);

中间层有一个邮件服务器供网页浏览器使用;

底部还有几个较小的组件。

Figure 1-3. Process types and interactions

Figure 1-3. Process types and interactions

The bottom level tends to consist of small components that perform single, uncomplicated tasks. The middle level has larger components such as mail, print, and database services. Finally, components at the top level perform complicated tasks that the user often controls directly. Components also use other components. Generally, if one component wants to use another, the second component is either at the same service level or below.

底层通常由执行单一、简单任务的小组件组成。

中层有较大的组件,如邮件、打印和数据库服务。

最后,顶层组件执行复杂的任务,用户通常直接控制。组件还会使用其他组件。

通常,如果一个组件想要使用另一个组件,第二个组件要么在相同的服务级别,要么在下面。

However, Figure 1-3 is only an approximation of the arrangement of user space. In reality, there are no rules in user space. For example, most applications and services write diagnostic messages known as logs. Most programs use the standard syslog service to write log messages, but some prefer to do all of the logging themselves.

然而,图1-3只是用户空间排列的近似表示。

实际上,用户空间没有规则。例如,大多数应用程序和服务会写入被称为日志的诊断信息。

大多数程序使用标准的syslog服务来写入日志消息,但有些程序更喜欢自己完成所有的日志记录。

In addition, it’s difficult to categorize some user-space components. Server components such as web and database servers can be considered very high-level applications because their tasks are often complicated, so you might place these at the top level in Figure 1-3. However, user applications may depend on these servers to perform tasks that they’d rather not do themselves, so you could also make a case for placing them at the middle level.

此外,对于一些用户空间组件很难进行分类。

像Web和数据库服务器这样的服务器组件可以被认为是非常高级的应用程序,因为它们的任务通常很复杂,所以你可以将它们放在图1-3的顶层。

然而,用户应用程序可能依赖这些服务器来执行他们不愿意自己完成的任务,所以你也可以将它们放在中层。

1.5 Users(用户)

The Linux kernel supports the traditional concept of a Unix user. A user is an entity that can run processes and own files. A user is associated with a username. For example, a system could have a user named billyjoe. However, the kernel does not manage the usernames; instead, it identifies users by simple numeric identifiers called userids. (You’ll learn more about how the usernames correspond to userids in Chapter 7.)

Linux内核支持传统的Unix用户概念。

用户是可以运行进程和拥有文件的实体。用户与用户名相关联。

例如,系统可以有一个名为billyjoe的用户。

然而,内核不管理用户名,而是通过简单的数值标识符(称为用户ID)来识别用户。

(关于用户名如何对应用户ID的更多信息将在第7章中介绍。)

Users exist primarily to support permissions and boundaries. Every user-space process has a user owner, and processes are said to run as the owner. A user may terminate or modify the behavior of its own processes (within certain limits), but it cannot interfere with other users’ processes. In addition, users may own files and choose whether they share them with other users.

用户主要存在于支持权限和边界方面。

每个用户空间进程都有一个用户所有者,并且进程被称为以所有者身份运行。

用户可以终止或修改自己的进程的行为(在一定限制内),但不能干扰其他用户的进程。

此外,用户可以拥有文件,并选择是否与其他用户共享这些文件。

A Linux system normally has a number of users in addition to the ones that correspond to the real human beings who use the system. You’ll read about these in more detail in Chapter 3, but the most important user to know about is root. The root user is an exception to the preceding rules because root may terminate and alter another user’s processes and read any file on the local system. For this reason, root is known as the superuser. A person who can operate as root is said to have root access and is an administrator on a traditional Unix system.

Linux系统通常除了与真正使用系统的人相对应的用户之外,还有一些其他用户。

您将在第3章中详细了解这些用户,但最重要的用户是root。

root用户是前述规则的例外,因为root可以终止和修改其他用户的进程,并读取本地系统上的任何文件。

因此,root被称为超级用户。

能够以root身份操作的人被称为具有root访问权限,并且是传统Unix系统上的管理员。

NOTE Operating as root can be dangerous. It can be difficult to identify and correct mistakes because the system will let you do anything, even if what you’re doing is harmful to the system. For this reason, system designers constantly try to make root access as unnecessary as possible, for example, by not requiring root access to switch between wireless networks on a notebook. In addition, as powerful as the root user is, it still runs in the operating system’s user mode, not kernel mode.

注意

以root身份操作可能是危险的。

由于系统允许您执行任何操作,即使这些操作对系统有害,因此很难识别和纠正错误。

因此,系统设计师不断尝试使root访问尽可能不必要,例如,不要求在笔记本上切换无线网络时需要root访问权限。

此外,尽管root用户非常强大,但它仍在操作系统的用户模式下运行,而不是内核模式。

Groups are sets of users. The primary purpose of groups is to allow a user to share file access to other users in a group.

组是一组用户。

组的主要目的是允许用户与组中的其他用户共享文件访问权限。

1.6 Looking Forward(展望未来)

So far, you’ve seen what makes up a running Linux system. User processes make up the environment that you directly interact with; the kernel manages processes and hardware. Both the kernel and processes reside in memory.

This is great background information, but you can’t learn the details of a Linux system by reading about it alone; you need to get your hands dirty. The next chapter starts your journey by teaching you some user-space basics. Along the way, you’ll learn about a major part of the Linux system that this chapter doesn’t discuss— long-term storage (disks, files, etc.). After all, you need to store your programs and data somewhere.

到目前为止,您已经了解了构成一个运行的Linux系统的要素。

用户进程构成了您直接与之交互的环境;内核管理进程和硬件。内核和进程都驻留在内存中。

这是很好的背景信息,但单靠阅读无法了解Linux系统的细节;您需要亲自动手。

下一章将从教授一些用户空间基础知识开始您的学习之旅。

在此过程中,您将了解到本章未讨论的Linux系统的一个重要部分-长期存储(磁盘、文件等)。

毕竟,您需要将程序和数据存储在某个地方。


Xander
195 声望49 粉丝