Chinaunix首页 | 论坛 | 博客
  • 博客访问: 56283
  • 博文数量: 47
  • 博客积分: 2095
  • 博客等级: 大尉
  • 技术积分: 560
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-01 18:42
文章分类

全部博文(47)

文章存档

2011年(1)

2008年(46)

我的朋友

分类: LINUX

2008-04-12 11:43:10

Processes

The concept of a process is fundamental to any multiprogramming operating system. A process is usually defined as an instance of a program in execution; thus, if 16 users are running vi at once, there are 16 separate processes (although they can share the same executable code). Processes are often called tasks or threads in the Linux source code.

进程是多任务OS的基础。进程通常定义为程序运行的实体;因此如果16个用户同时运行vi,那么就有16个独立的进程(尽管它们共享相同的可执行代码)。在Linux源码中,进程也称为任务或线程。

In this chapter, we discuss static properties of processes and then describe how process switching is performed by the kernel. The last two sections describe how processes can be created and destroyed. We also describe how Linux supports multithreaded applications as mentioned in Chapter 1, it relies on so-called lightweight processes (LWP).

这一章,将会讨论进程的静态特性,描述内核是如何切换进程的。最后描述了进程是如何被创建和销毁的。

Processes, Lightweight Processes, and Threads

The term "process" is often used with several different meanings. In this book, we stick to the usual OS textbook definition: a process is an instance of a program in execution. You might think of it as the collection of data structures that fully describes how far the execution of the program has progressed.

术语“进程”通常有多个不同的意思。在这本书中,我们坚持使用通用OS教材的定义:进程是程序运行时的实体。你可以把它想成是描述“程序的运行进行到哪里”的数据结构的集合。

Processes are like human beings: they are generated, they have a more or less significant life, they optionally generate one or more child processes, and eventually they die. A small difference is that sex is not really common among processes each process has just one parent.

进程有点像人:它们被生出来,有一段或多或少有意义的生活,它们可选择地生成一个或更多的子进程,最后它们也会死去。不同的是,进程没有性别,每个进程只有一个父辈。

From the kernel's point of view, the purpose of a process is to act as an entity to which system resources (CPU time, memory, etc.) are allocated.

从内核的角度看,进程的目的是进行系统资源分配的实体,比如CPU时间、内存等。

When a process is created, it is almost identical to its parent. It receives a (logical) copy of the parent's address space and executes the same code as the parent, beginning at the next instruction following the process creation system call. Although the parent and child may share the pages containing the program code (text), they have separate copies of the data (stack and heap), so that changes by the child to a memory location are invisible to the parent (and vice versa).

当一个进程被创建时,它和它的父进程是一样的。它复制了父进程的地址空间,执行相同的代码,从进程创建系统调用后那个指令开始运行。尽管父进程和子进程可以共享包含程序代码的页面,然而它们还是有独立的数据(栈和堆),这样子进程修改了属于它的内存,父进程是不知道的(反之亦然)。

While earlier Unix kernels employed this simple model, modern Unix systems do not. They support multithreaded applications--user programs having many relatively independent execution flows sharing a large portion of the application data structures. In such systems, a process is composed of several user threads (or simply threads), each of which represents an execution flow of the process. Nowadays, most multithreaded applications are written using standard sets of library functions called pthread (POSIX thread) libraries .

早期UNIX内核采用了这样简单的模式,现代Unix则没有。它们支持多线程—用户程序有着若干相对独立、共享一大片应用程序数据的执行序列。在这样的系统,进程由若干用户线程组成,每个线程表示进程的一个独立的执行序列。如今,大多数多线程程序都是采用标准的称为 pthread库的线程库编写。

Older versions of the Linux kernel offered no support for multithreaded applications. From the kernel point of view, a multithreaded application was just a normal process. The multiple execution flows of a multithreaded application were created, handled, and scheduled entirely in User Mode, usually by means of a POSIX-compliant pthread library.

较老版本的Linux内核没有提供对多线程的支持。从内核的角度看,一个多线程程序只是一个普通的进程。多线程的创建,处理,调度都是通过POSIX标准的pthread库在用户空间进行,内核并不知道多线程。

However, such an implementation of multithreaded applications is not very satisfactory. For instance, suppose a chess program uses two threads: one of them controls the graphical chessboard, waiting for the moves of the human player and showing the moves of the computer, while the other thread ponders the next move of the game. While the first thread waits for the human move, the second thread should run continuously, thus exploiting the thinking time of the human player. However, if the chess program is just a single process, the first thread cannot simply issue a blocking system call waiting for a user action; otherwise, the second thread is blocked as well. Instead, the first thread must employ sophisticated nonblocking techniques to ensure that the process remains runnable.

当然,这样很不好,跟不上时代要求。

Linux uses lightweight processes to offer better support for multithreaded applications. Basically, two lightweight processes may share some resources, like the address space, the open files, and so on. Whenever one of them modifies a shared resource, the other immediately sees the change. Of course, the two processes must synchronize themselves when accessing the shared resource.

Linux使用“轻进程”提供对多线程程序更好的支持。基本上,两个轻进程可以共享一些资源,比如地址空间,打开的文件等等。无论何时其中一个修改了共享的资源,另外一个立刻就能看到。当然,两个进程在访问共享资源的时候必须同步。

A straightforward way to implement multithreaded applications is to associate a lightweight process with each thread. In this way, the threads can access the same set of application data structures by simply sharing the same memory address space, the same set of open files, and so on; at the same time, each thread can be scheduled independently by the kernel so that one may sleep while another remains runnable. Examples of POSIX-compliant pthread libraries that use Linux's lightweight processes are LinuxThreads, Native POSIX Thread Library (NPTL), and IBM's Next Generation Posix Threading Package (NGPT).

一种实现多线程程序最直接的方法就是用一个轻进程关联每个线程。这样,线程可以通过共享相同的内存地址空间访问相同的应用程序的数据结构;同时,每个进程也会被内核独立的调度,使得一个线程睡着了,而别的依然保持运行。使用Linux轻进程的POSIX标准pthread库的例子是LinuxThreads,NPTL,和IBM的NGPT。

POSIX-compliant multithreaded applications are best handled by kernels that support "thread groups ." In Linux a thread group is basically a set of lightweight processes that implement a multithreaded application and act as a whole with regards to some system calls such as getpid( ) , kill( ) , and _exit( ) . We are going to describe them at length later in this chapter.

支持线程组的POSIX标准的多线程程序可以被内核很好的处理。在Linux中,线程组是一组实现同一多线程程序的轻进程,并且在使用某些系统调用时表现得好像一个整体,比如getpid(), kill(), _exit()。
阅读(419) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~