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

全部博文(47)

文章存档

2011年(1)

2008年(46)

我的朋友

分类: LINUX

2008-04-12 11:44:48

Process Descriptor

To manage processes, the kernel must have a clear picture of what each process is doing. It must know, for instance, the process's priority, whether it is running on a CPU or blocked on an event, what address space has been assigned to it, which files it is allowed to address, and so on. This is the role of the process descriptor a task_struct type structure whose fields contain all the information related to a single process.[*] As the repository of so much information, the process descriptor is rather complex. In addition to a large number of fields containing process attributes, the process descriptor contains several pointers to other data structures that, in turn, contain pointers to other structures. Figure 3-1 describes the Linux process descriptor schematically.

[*] The kernel also defines the task_t data type to be equivalent to struct task_struct.

为了管理进程,内核必须对每个进程正在做的事有清楚的认识。比如,它必须知道,进程的优先级,不管它是运行在某个CPU上还是阻塞在某事件上;分配给进程的地址空间;进程可以访问的文件等等。这就是进程描述符扮演的角色:一种task_struct结构,它的成员包含了所有与一个单独进程相关的信息。作为这么多信息的仓库,进程描述符那是相当的复杂。除了很多描述进程特性的成员,进程描述符包含了几个指向其他重要数据结构的指针。

The six data structures on the right side of the figure refer to specific resources owned by the process. Most of these resources will be covered in future chapters. This chapter focuses on two types of fields that refer to the process state and to process parent/child relationships.

右侧的6个数据结构表示进程拥有的资源。本章集中在2个成员:表示进程状态和进程父/子关系的成员。

Process State

As its name implies, the state field of the process descriptor describes what is currently happening to the process. It consists of an array of flags, each of which describes a possible process state. In the current Linux version, these states are mutually exclusive, and hence exactly one flag of state always is set; the remaining flags are cleared. The following are the possible process states:

进程描述符的state成员描述了进程当前的状态。它由一组标志组成,每种标志描述了一种可能的进程状态。在当前Linux版本中,这些标志是互斥的,因此设置了一个state;剩下的标志就是空的。下面是可能的进程标志:

TASK_RUNNING

The process is either executing on a CPU or waiting to be executed.

进程可能在CPU上运行,也可能等待运行。

TASK_INTERRUPTIBLE

The process is suspended (sleeping) until some condition becomes true. Raising a hardware interrupt, releasing a system resource the process is waiting for, or delivering a signal are examples of conditions that might wake up the process (put its state back to TASK_RUNNING).

进程被暂停(睡觉),直到某个条件变为真。这样的条件比如产生一个硬件中断、释放进程等待的系统资源、或者发送一个信号,都可以将进程唤醒(将它的状态变为TASK_RUNNING).

TASK_UNINTERRUPTIBLE

Like TASK_INTERRUPTIBLE, except that delivering a signal to the sleeping process leaves its state unchanged. This process state is seldom used. It is valuable, however, under certain specific conditions in which a process must wait until a given event occurs without being interrupted. For instance, this state may be used when a process opens a device file and the corresponding device driver starts probing for a corresponding hardware device. The device driver must not be interrupted until the probing is complete, or the hardware device could be left in an unpredictable state.

跟TASK_INTERRUPTIBLE一样,期待一个信号的发送给这个睡着的进程,让它改变状态。这个进程状态很少使用。然而,它是很有用的,在某种特殊的情形下,进程必须等待而不被中断直到某个给定的事件发生。比如,这个状态用于当一个进程打开设备文件并且对应的设备文件开始探测对应的硬件设备。这个设备驱动一定不能被中断,直到探测完成,否则硬件设备可能进入不可预知的状态。

TASK_STOPPED

Process execution has been stopped; the process enters this state after receiving a SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU signal.

进程运行已经被停止;进程在收到SIGSTOP、SIGTSTP、SIGTTOU信号后进入这个状态。

TASK_TRACED

Process execution has been stopped by a debugger. When a process is being monitored by another (such as when a debugger executes a ptrace( ) system call to monitor a test program), each signal may put the process in the TASK_TRACED state.

进程的运行被调试器停止。当进程别另一个进程(比如当调试器执行ptrace()系统调用监视一个测试程序)所监视,每个信号都会使进程设进入TASK_TRACED状态

Two additional states of the process can be stored both in the state field and in the exit_state field of the process descriptor; as the field name suggests, a process reaches one of these two states only when its execution is terminated:

还有两个进程状态可以被存储在进程描述符的state成员和exit_state成员;当进程运行被结束时,它进入其中之一。

EXIT_ZOMBIE

Process execution is terminated, but the parent process has not yet issued a wait4( ) or waitpid( ) system call to return information about the dead process.[*] Before the wait( )-like call is issued, the kernel cannot discard the data contained in the dead process descriptor because the parent might need it. (See the section "Process Removal" near the end of this chapter.)

[*] There are other wait( ) -like library functions, such as wait3( ) and wait( ), but in Linux they are implemented by means of the wait4( ) and waitpid( ) system calls.

进程执行被终止,但是父进程还没有调用wait4()或waitpid()系统调用来返回已死进程的状态。在wait()-类似函数调用之前,内核不能丢弃已死进程的描述符中的数据,因为父进程可能需要这些数据。

EXIT_DEAD

The final state: the process is being removed by the system because the parent process has just issued a wait4( ) or waitpid( ) system call for it. Changing its state from EXIT_ZOMBIE to EXIT_DEAD avoids race conditions due to other threads of execution that execute wait( )-like calls on the same process (see Chapter 5).

最后的状态:进程正由系统移走,因为父进程已经调用了wait4()或waitpid()系统调用。将进程的状态从EXIT_ZOMBIE改变到EXIT_DEAD避免了其他线程在同一进程上执行wait()-类似函数调用引发的竞态。

The value of the state field is usually set with a simple assignment. For instance:

    p->state = TASK_RUNNING;

The kernel also uses the set_task_state and set_current_state macros: they set the state of a specified process and of the process currently executed, respectively. Moreover, these macros ensure that the assignment operation is not mixed with other instructions by the compiler or the CPU control unit. Mixing the instruction order may sometimes lead to catastrophic results (see Chapter 5).
 
内核也使用set_task_state和set_current_state宏:它们分别设置一个指定进程和当前运行进程的状态。重要的是,这些宏保证了赋值操作不会被编译器或CPU控制器混合其他的指令。这是一个原子操作。
阅读(482) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~