進程狀態
在進程描述符中,long類型的state描述了進程的當前狀態。
如下所示:進程狀態可以分為三大類:>0 停止運行,=0可以運行,-1不可運行。
2019-02-02_193524.png
實際上系統中的進程可以處於以下五種狀態之一:
- TASK_RUNNING(運行)——進程是可執行的;其正在執行或在運行隊列中等待執行。
- TASK_INTERRUPTIBLE(可中斷)——進程被阻塞處於睡眠狀態,其正在等待某些條件達成(比如I/O操作)。但條件達成時,內核就會把該進程的狀態設置為運行。
- TASK_UNINTERRUPTIBLE(不可中斷)——處於此狀態的進程接收到信號也不會被喚醒,此外與可中斷狀態相同。
- __TASK_TRACED——被其他進程跟蹤的進程,比如ptrace。
- __TASK_STOPPED(停止)——進程停止執行。進程在接收到SIGSTOP、SIGSTP、SIGTTIN、SIGTTOU等信號時轉為此狀態,在調試期間接收到任何信號,也會進入該狀態。
各狀態的轉換關係如下所示:
figure_3.3_600.png
我們可以在內核代碼中找到該定義:linux2.6.34/include/linux/sched.h#L183
/*
* Task state bitmask. NOTE! These bits are also
* encoded in fs/proc/array.c: get_task_state().
*
* We have two separate sets of flags: task->state
* is about runnability, while task->exit_state are
* about the task exiting. Confusing, but this way
* modifying one set can't modify the other one by
* mistake.
*/
#define TASK_RUNNING 0
#define TASK_INTERRUPTIBLE 1
#define TASK_UNINTERRUPTIBLE 2
#define __TASK_STOPPED 4
#define __TASK_TRACED 8
/* in tsk->exit_state */
#define EXIT_ZOMBIE 16
#define EXIT_DEAD 32
/* in tsk->state again */
#define TASK_DEAD 64
#define TASK_WAKEKILL 128
#define TASK_WAKING 256
#define TASK_STATE_MAX 512
#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKW
設置當前進程狀態
內核中進程需要調整某個進程的狀態。但是一般不是通過直接修改state的值來完成狀態的改變,而是使用以下四個宏之一:
- set_task_state
- __set_task_state
- set_current_state
- __set_current_state
這些宏定義在linux2.3.34/include/linux/sched.h#L224
#define __set_task_state(tsk, state_value) \
do { (tsk)->state = (state_value); } while (0)
#define set_task_state(tsk, state_value) \
set_mb((tsk)->state, (state_value))
/*
* set_current_state() includes a barrier so that the write of current->state
* is correctly serialised wrt the caller's subsequent test of whether to
* actually sleep:
*
* set_current_state(TASK_UNINTERRUPTIBLE);
* if (do_i_need_to_sleep())
* schedule();
*
* If the caller does not need such serialisation then use __set_current_state()
*/
#define __set_current_state(state_value) \
do { current->state = (state_value); } while (0)
#define set_current_state(state_value) \
set_mb(current->state, (state_value))
從上述代碼中可以清楚地看到set_current_state(state_value)等將於set_task_state(current, state_value)。
通過注釋也能明白是否添加__的區別再去,set__current_state()包括了一個屏障,以便調用者在之後測試是否在睡眠時實際寫入了state的值,而__set__current()則是簡單地直接修改state的值。
註:current是一個宏,表示當前的進程,該宏的定義與體系結構相關,對於x86體系結構,其定義在linux2.6.34/arch/x86/include/asm/current.h#L17。由於current.h的內容較少,我把它完整地貼在這裡了。
#ifndef _ASM_X86_CURRENT_H
#define _ASM_X86_CURRENT_H
#include <linux/compiler.h>
#include <asm/percpu.h>
#ifndef __ASSEMBLY__
struct task_struct;
DECLARE_PER_CPU(struct task_struct *, current_task);
static __always_inline struct task_struct *get_current(void)
{
return percpu_read_stable(current_task);
}
#define current get_current()
#endif /* __ASSEMBLY__ */
#endif /* _ASM_X86_CURRENT_H */
進程上下文
可執行程序代碼是進程的重要組成部分,這些代碼一般是從一個可執行文件載入到進程的地址空間中執行的。一般的程序在用戶空間執行,但其請求系統調用或者觸發異常時,其就陷入了內核空間。此時,稱內核代表進程執行並處於進程上下文中。在此上下文中的current宏是有效的,除非在此間隙有更高優先級的進程需要執行並由調度器做出了相應調整,否則在內核退出時,程序恢復在用戶空間執行。