1.7 进程+线程+虚拟内存

回到刚才hello的例子。当shell载入并运行hello程序,以及hello程序最终输出信息时,并不是程序直接访问了键盘、显示器、磁盘或者内存,而是依赖于由操作系统提供的各种服务。We can think of the operating system as
a layer of software interposed between the application program and the hardware,
as shown in Figure 1.10. All attempts by an application program to manipulate the
hardware must go through the operating system.

在这里插入图片描述
The operating system has two primary purposes: (1) to protect the hardware
from misuse by runaway applications and (2) to provide applications with simple
and uniform mechanisms for manipulating complicated and often wildly different
low-level hardware devices. The operating system achieves both goals via the
fundamental abstractions shown in Figure 1.11: processes, virtual memory, and
files. As this figure suggests, files are abstractions for I/O devices, virtual memory
is an abstraction for both the main memory and disk I/O devices, and processes
are abstractions for the processor, main memory, and I/O devices. We will discuss
each in turn.

1 进程

When a program such as hello runs on a modern system, the operating system
provides the illusion that the program is the only one running on the system. The
program appears to have exclusive use of both the processor, main memory, and
I/O devices. The processor appears to execute the instructions in the program, one
after the other, without interruption. And the code and data of the program appear
to be the only objects in the system’s memory. These illusions are provided by the
notion of a process, one of the most important and successful ideas in computer
science.

进程是操作系统对于一个运行时程序的抽象。多个进程可以并行运行在同一个系统上,and each process appears
to have exclusive use of the hardware. By concurrently, we mean that the instructions of one process are interleaved with the instructions of another process. In
most systems, there are more processes to run than there are CPUs to run them.

Traditional systems could only execute one program at a time, while newer multicore processors can execute several programs simultaneously. In either case, a
single CPU can appear to execute multiple processes concurrently by having the
processor switch among them. The operating system performs this interleaving
with a mechanism known as context switching. To simplify the rest of this discussion, we consider only a uniprocessor system containing a single CPU. We will
return to the discussion of multiprocessor systems in Section 1.9.2.

操作系统会持续追踪所有进程运行时所需的状态信息。这些状态信息被称为上下文,包括例如the current values of the PC, the register file, and the contents of main memory. 在任何时间,一个uniprocessor系统只能为一个进程执行代码。当操作系统决定从一个进程到另一个进程 transfer control 时,它会通过保存当前进程的上下文执行一次上下文切换。重新恢复新进程的上下文并且将控制移交给新进程。The new process picks up exactly where
it left off. Figure 1.12 shows the basic idea for our example hello scenario.

在这里插入图片描述
在我们的场景中存在两个进程:shell进程和hello进程。最开始,shell进程自己执行,等待命令行的输入。当我们执行hello程序时,shelll carries
out our request by invoking a special function known as a system call that passes
control to the operating system. 操作系统保存shell的上下文,创建新的hello进程以及对应的上下文,并且将控制移交给新的hello进程。在hello终止后,操作系统恢复shell的上下文并且将控制移交给shell进程,继续等待下一个命令行输入。

As Figure 1.12 indicates, the transition from one process to another is managed by the operating system kernel. The kernel is the portion of the operating
system code that is always resident in memory. When an application program
requires some action by the operating system, such as to read or write a file, it
executes a special system call instruction, transferring control to the kernel. The
kernel then performs the requested operation and returns back to the application
program. Note that the kernel is not a separate process. Instead, it is a collection
of code and data structures that the system uses to manage all the processes.

Implementing the process abstraction requires close cooperation between
both the low-level hardware and the operating system software. We will explore
how this works, and how applications can create and control their own processes,
in Chapter 8.

2 线程

尽管我们通常认为一个进程有着一个单一的控制流,但是在现代系统中一个进程可以实际上包含多个执行单元,称为线程,每个都运行在进程的上下文环境中并且共享同样的代码以及全局数据。线程是越来越重要的programming model,因为在网络服务器中对并发性的要求越来越高、在多个线程间共享数据要比多个进程间共享数据简单以及线程通常比进程效率更高。多线程同样是一种在多个处理器条件下加快程序运行的方式。as we will discuss in Section 1.9.2. You will learn the basic concepts of concurrency, including how to write threaded programs, in Chapter 12.

3 虚拟内存

Virtual memory is an abstraction that provides each process with the illusion that it
has exclusive use of the main memory. Each process has the same uniform view of
memory, which is known as its virtual address space. The virtual address space for
Linux processes is shown in Figure 1.13. (Other Unix systems use a similar layout.)
In Linux, the topmost region of the address space is reserved for code and data
in the operating system that is common to all processes. The lower region of the
address space holds the code and data defined by the user’s process. Note that
addresses in the figure increase from the bottom to the top.

The virtual address space seen by each process consists of a number of welldefined areas, each with a specific purpose. You will learn more about these areas
later in the book, but it will be helpful to look briefly at each, starting with the
lowest addresses and working our way up:

  • 程序代码及数据:Code begins at the same fixed address for all processes, followed by data locations that correspond to global C variables. The code and data areas are initialized directly from the contents of an executable object file—in our case, the hello executable. You will learn more about this part of the address space when we study linking and loading in Chapter 7.
  • Heap:The code and data areas are followed immediately by the run-time heap. Unlike the code and data areas, which are fixed in size once the process begins running, the heap expands and contracts dynamically at run time as a result of calls to C standard library routines such as malloc and free. We will study heaps in detail when we learn about managing virtual memory in Chapter 9.
  • Shared libraries:Near the middle of the address space is an area that holds the code and data for shared libraries such as the C standard library and the math library. The notion of a shared library is a powerful but somewhat difficult concept. You will learn how they work when we study dynamic linking in Chapter 7.
  • Stack:At the top of the user’s virtual address space is the user stack that the compiler uses to implement function calls. Like the heap, the user stack expands and contracts dynamically during the execution of the program. In particular, each time we call a function, the stack grows. Each time we return from a function, it contracts. You will learn how the compiler uses the stack in Chapter 3.
  • Kernel Virtual memory:The top region of the address space is reserved for the
    kernel. Application programs are not allowed to read or write the contents of
    this area or to directly call functions defined in the kernel code. Instead, they
    must invoke the kernel to perform these operations.

4 文件

一个文件是一串字节。每个 I/O 设备,包括磁盘、键盘、显示器甚至网络都被视为一个文件。系统中所有的数据和输出都通过读写文件,using a small set of system calls known as Unix I/O.

This simple and elegant notion of a file is nonetheless very powerful because
it provides applications with a uniform view of all the varied I/O devices that
might be contained in the system. For example, application programmers who
manipulate the contents of a disk file are blissfully unaware of the specific disk
technology. Further, the same program will run on different systems that use
different disk technologies. You will learn about Unix I/O in Chapter 10.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值