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

全部博文(47)

文章存档

2011年(1)

2008年(46)

我的朋友

分类: LINUX

2008-04-13 14:08:06

Identifying a Process

As a general rule, each execution context that can be independently scheduled must have its own process descriptor; therefore, even lightweight processes, which share a large portion of their kernel data structures, have their own task_struct structures.

一个基本的原则是,每个可以被独立调度的执行上下文都必须有它自己的进程描述符;因此,即使是轻进程,共享了大部分内核数据结构,也有自己的task_struct结构。

The strict one-to-one correspondence between the process and process descriptor makes the 32-bit address[*] of the task_struct structure a useful means for the kernel to identify processes. These addresses are referred to as process descriptor pointers. Most of the references to processes that the kernel makes are through process descriptor pointers.

[*] As already noted in the section "Segmentation in Linux" in Chapter 2, although technically these 32 bits are only the offset component of a logical address, they coincide with the linear address.

进程与进程描述符之间严格的一对一关系,使得task_struct结构的32bit地址是内核标识进程的有效手段。这些地址也就是进程描述符的指针。通常,内核对进程的引用都是通过操作进程描述符指针。

On the other hand, Unix-like operating systems allow users to identify processes by means of a number called the Process ID (or PID), which is stored in the pid field of the process descriptor. PIDs are numbered sequentially: the PID of a newly created process is normally the PID of the previously created process increased by one. Of course, there is an upper limit on the PID values; when the kernel reaches such limit, it must start recycling the lower, unused PIDs. By default, the maximum PID number is 32,767 (PID_MAX_DEFAULT - 1); the system administrator may reduce this limit by writing a smaller value into the /proc/sys/kernel/pid_max file (/proc is the mount point of a special filesystem, see the section "Special Filesystems" in Chapter 12). In 64-bit architectures, the system administrator can enlarge the maximum PID number up to 4,194,303.

另外,类Unix-OS 允许用户通过一个数字PID标识进程,PID存储在进程描述符的pid成员中。PID是按顺序计数的:新创建进程的PID通常是前一个进程PID值加一。当然,PID值有一个上限;当内核达到这个上限时,它必须回收更低的不用的PID值。默认最大的PID数目是32,767(PID_MAX_DEFAULT-1);系统管理员可以通过往/proc/sys/kernel/pid_max文件写入一个较小的值来降低这个上限。在64bit架构上,系统管理员可以将PID数目扩大到4,193,303。

When recycling PID numbers, the kernel must manage a pidmap_array bitmap that denotes which are the PIDs currently assigned and which are the free ones. Because a page frame contains 32,768 bits, in 32-bit architectures the pidmap_array bitmap is stored in a single page. In 64-bit architectures, however, additional pages can be added to the bitmap when the kernel assigns a PID number too large for the current bitmap size. These pages are never released.

当内核回收PID数字时,它必须管理一个pidmap_array的数据结构,表示当前分配了的PID和没分配的PID。因为一个页帧包含了32,768bit,在32bit架构上,pidmap_array存储在一个页帧里。然而,64bit架构中,当PID数目太大时,需要额外的页加入这个结构。这些页面不会被释放。

Linux associates a different PID with each process or lightweight process in the system. (As we shall see later in this chapter, there is a tiny exception on multiprocessor systems.) This approach allows the maximum flexibility, because every execution context in the system can be uniquely identified.

Linux在系统中将不同的PID值关联每个进程或轻进程。这种方法容许最大的灵活性,因为系统中每个执行上下文都可以被唯一地被标识。

On the other hand, Unix programmers expect threads in the same group to have a common PID. For instance, it should be possible to a send a signal specifying a PID that affects all threads in the group. In fact, the POSIX 1003.1c standard states that all threads of a multithreaded application must have the same PID.

另外,Unix程序员希望在同一组中的线程有相同的PID。比如,有可能发送一个指定PID的信号影响组内的所有线程。事实上,POSIX 1003.1c标准声明一个多线程程序的所有线程都必须拥有相同的PID。

To comply with this standard, Linux makes use of thread groups. The identifier shared by the threads is the PID of the thread group leader , that is, the PID of the first lightweight process in the group; it is stored in the tgid field of the process descriptors. The getpid( ) system call returns the value of tgid relative to the current process instead of the value of pid, so all the threads of a multithreaded application share the same identifier. Most processes belong to a thread group consisting of a single member; as thread group leaders, they have the tgid field equal to the pid field, thus the getpid( ) system call works as usual for this kind of process.

为了遵循这个标准,Linux使用了线程组。线程共享的标识是线程组leader的PID,也就是组中第一个轻进程的PID;这个值存储在进程描述符的tgid成员中。系统调用getpid()返回的时与进程对应的tgid值,而不再是pid的值,所以一个多线程程序的所有线程共享了同一个标识。大多数进程属于仅由一个成员组成的线程组;与线程组leader一样,它们的tgid成员与pid成员有相同值,因此geipid()系统调用可以在这种进程上获得期望的数据。

Later, we'll show you how it is possible to derive a true process descriptor pointer efficiently from its respective PID. Efficiency is important because many system calls such as kill( ) use the PID to denote the affected process.

下面将向你展示怎样根据进程的PID快速地得到它的进程描述符的指针。

阅读(435) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~