進(jìn)程是處于執(zhí)行期的程序以及它所管理的資源(如打開的文件、掛起的信號(hào)、進(jìn)程狀態(tài)、地址空間等等)的總稱。注意,程序并不是進(jìn)程,實(shí)際上兩個(gè)或多個(gè)進(jìn)程不僅有可能執(zhí)行同一程序,而且還有可能共享地址空間等資源。
Linux內(nèi)核通過(guò)一個(gè)被稱為進(jìn)程描述符的task_struct結(jié)構(gòu)體來(lái)管理進(jìn)程,這個(gè)結(jié)構(gòu)體包含了一個(gè)進(jìn)程所需的所有信息。它定義在include/linux/sched.h文件中。
談到task_struct結(jié)構(gòu)體,可以說(shuō)她是linux內(nèi)核源碼中最復(fù)雜的一個(gè)結(jié)構(gòu)體了,成員之多,占用內(nèi)存之大。
鑒于她的復(fù)雜,我們不能簡(jiǎn)單的褻瀆,而是要深入“窺探”.
下面來(lái)慢慢介紹這些復(fù)雜成員
進(jìn)程狀態(tài)
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
state成員的可能取值如下
/*
* 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_DEAD 16
#define EXIT_ZOMBIE 32
#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
/* in tsk->state again */
#define TASK_DEAD 64
#define TASK_WAKEKILL 128 /** wake on signals that are deadly **/
#define TASK_WAKING 256
#define TASK_PARKED 512
#define TASK_NOLOAD 1024
#define TASK_STATE_MAX 2048
/* Convenience macros for the sake of set_task_state */
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
#define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
5個(gè)互斥狀態(tài)
state域能夠取5個(gè)互為排斥的值(通俗一點(diǎn)就是這五個(gè)值任意兩個(gè)不能一起使用,只能單獨(dú)使用)。系統(tǒng)中的每個(gè)進(jìn)程都必然處于以上所列進(jìn)程狀態(tài)中的一種。

2個(gè)終止?fàn)顟B(tài)
其實(shí)還有兩個(gè)附加的進(jìn)程狀態(tài)既可以被添加到state域中,又可以被添加到exit_state域中。只有當(dāng)進(jìn)程終止的時(shí)候,才會(huì)達(dá)到這兩種狀態(tài).
/* task state */
int exit_state;
int exit_code, exit_signal;

新增睡眠狀態(tài)
進(jìn)程狀態(tài) TASK_UNINTERRUPTIBLE 和 TASK_INTERRUPTIBLE 都是睡眠狀態(tài)?,F(xiàn)在,我們來(lái)看看內(nèi)核如何將進(jìn)程置為睡眠狀態(tài)。
內(nèi)核如何將進(jìn)程置為睡眠狀態(tài)
Linux 內(nèi)核提供了兩種方法將進(jìn)程置為睡眠狀態(tài)。
將進(jìn)程置為睡眠狀態(tài)的普通方法是將進(jìn)程狀態(tài)設(shè)置為 TASK_INTERRUPTIBLE 或 TASK_UNINTERRUPTIBLE 并調(diào)用調(diào)度程序的 schedule() 函數(shù)。這樣會(huì)將進(jìn)程從 CPU 運(yùn)行隊(duì)列中移除。
-
如果進(jìn)程處于可中斷模式的睡眠狀態(tài)(通過(guò)將其狀態(tài)設(shè)置為 TASK_INTERRUPTIBLE),那么可以通過(guò)顯式的喚醒呼叫(wakeup_process())或需要處理的信號(hào)來(lái)喚醒它。
-
但是,如果進(jìn)程處于非可中斷模式的睡眠狀態(tài)(通過(guò)將其狀態(tài)設(shè)置為 TASK_UNINTERRUPTIBLE),那么只能通過(guò)顯式的喚醒呼叫將其喚醒。除非萬(wàn)不得已,否則我們建議您將進(jìn)程置為可中斷睡眠模式,而不是不可中斷睡眠模式(比如說(shuō)在設(shè)備 I/O 期間,處理信號(hào)非常困難時(shí))。
當(dāng)處于可中斷睡眠模式的任務(wù)接收到信號(hào)時(shí),它需要處理該信號(hào)(除非它已被屏弊),離開之前正在處理的任務(wù)(此處需要清除代碼),并將 -EINTR 返回給用戶空間。再一次,檢查這些返回代碼和采取適當(dāng)操作的工作將由程序員完成。
因此,懶惰的程序員可能比較喜歡將進(jìn)程置為不可中斷模式的睡眠狀態(tài),因?yàn)樾盘?hào)不會(huì)喚醒這類任務(wù)。
但需要注意的一種情況是,對(duì)不可中斷睡眠模式的進(jìn)程的喚醒呼叫可能會(huì)由于某些原因不會(huì)發(fā)生,這會(huì)使進(jìn)程無(wú)法被終止,從而最終引發(fā)問(wèn)題,因?yàn)槲┮坏慕鉀Q方法就是重啟系統(tǒng)。一方面,您需要考慮一些細(xì)節(jié),因?yàn)椴贿@樣做會(huì)在內(nèi)核端和用戶端引入 bug。另一方面,您可能會(huì)生成永遠(yuǎn)不會(huì)停止的進(jìn)程(被阻塞且無(wú)法終止的進(jìn)程)。
現(xiàn)在,我們?cè)趦?nèi)核中實(shí)現(xiàn)了一種新的睡眠方法
Linux Kernel 2.6.25 引入了一種新的進(jìn)程睡眠狀態(tài)

它定義如下:
#define TASK_WAKEKILL 128 /** wake on signals that are deadly **/
/* Convenience macros for the sake of set_task_state */
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
#define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
換句話說(shuō),TASK_UNINTERRUPTIBLE + TASK_WAKEKILL = TASK_KILLABLE。
而TASK_WAKEKILL 用于在接收到致命信號(hào)時(shí)喚醒進(jìn)程
新的睡眠狀態(tài)允許 TASK_UNINTERRUPTIBLE 響應(yīng)致命信號(hào)
進(jìn)程狀態(tài)的切換過(guò)程和原因大致如下圖

進(jìn)程標(biāo)識(shí)符(PID)
pid_t pid;
pid_t tgid;
Unix系統(tǒng)通過(guò)pid來(lái)標(biāo)識(shí)進(jìn)程,linux把不同的pid與系統(tǒng)中每個(gè)進(jìn)程或輕量級(jí)線程關(guān)聯(lián),而unix程序員希望同一組線程具有共同的pid,遵照這個(gè)標(biāo)準(zhǔn)linux引入線程組的概念。一個(gè)線程組所有線程與領(lǐng)頭線程具有相同的pid,存入tgid字段,getpid()返回當(dāng)前進(jìn)程的tgid值而不是pid的值。
在CONFIG_BASE_SMALL配置為0的情況下,PID的取值范圍是0到32767,即系統(tǒng)中的進(jìn)程數(shù)最大為32768個(gè)。
#define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000)
在Linux系統(tǒng)中,一個(gè)線程組中的所有線程使用和該線程組的領(lǐng)頭線程(該組中的第一個(gè)輕量級(jí)進(jìn)程)相同的PID,并被存放在tgid成員中。只有線程組的領(lǐng)頭線程的pid成員才會(huì)被設(shè)置為與tgid相同的值。注意,getpid()系統(tǒng)調(diào)用返回的是當(dāng)前進(jìn)程的tgid值而不是pid值。
進(jìn)程內(nèi)核棧
void *stack;
內(nèi)核棧與線程描述符
對(duì)每個(gè)進(jìn)程,Linux內(nèi)核都把兩個(gè)不同的數(shù)據(jù)結(jié)構(gòu)緊湊的存放在一個(gè)單獨(dú)為進(jìn)程分配的內(nèi)存區(qū)域中
-
一個(gè)是內(nèi)核態(tài)的進(jìn)程堆棧,
-
另一個(gè)是緊挨著進(jìn)程描述符的小數(shù)據(jù)結(jié)構(gòu)thread_info,叫做線程描述符。
Linux把thread_info(線程描述符)和內(nèi)核態(tài)的線程堆棧存放在一起,這塊區(qū)域通常是8192K(占兩個(gè)頁(yè)框),其實(shí)地址必須是8192的整數(shù)倍。
在
linux/arch/x86/include/asm/page_32_types.h中,
#define THREAD_SIZE_ORDER 1
#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)
出于效率考慮,內(nèi)核讓這8K空間占據(jù)連續(xù)的兩個(gè)頁(yè)框并讓第一個(gè)頁(yè)框的起始地址是213的倍數(shù)。
內(nèi)核態(tài)的進(jìn)程訪問(wèn)處于內(nèi)核數(shù)據(jù)段的棧,這個(gè)棧不同于用戶態(tài)的進(jìn)程所用的棧。
用戶態(tài)進(jìn)程所用的棧,是在進(jìn)程線性地址空間中;
而內(nèi)核棧是當(dāng)進(jìn)程從用戶空間進(jìn)入內(nèi)核空間時(shí),特權(quán)級(jí)發(fā)生變化,需要切換堆棧,那么內(nèi)核空間中使用的就是這個(gè)內(nèi)核棧。因?yàn)閮?nèi)核控制路徑使用很少的??臻g,所以只需要幾千個(gè)字節(jié)的內(nèi)核態(tài)堆棧。
需要注意的是,內(nèi)核態(tài)堆棧僅用于內(nèi)核例程,Linux內(nèi)核另外為中斷提供了單獨(dú)的硬中斷棧和軟中斷棧
下圖中顯示了在物理內(nèi)存中存放兩種數(shù)據(jù)結(jié)構(gòu)的方式。線程描述符駐留與這個(gè)內(nèi)存區(qū)的開始,而棧頂末端向下增長(zhǎng)。下圖摘自ULK3,進(jìn)程內(nèi)核棧與進(jìn)程描述符的關(guān)系如下圖:

但是較新的內(nèi)核代碼中,進(jìn)程描述符task_struct結(jié)構(gòu)中沒(méi)有直接指向thread_info結(jié)構(gòu)的指針,而是用一個(gè)void指針類型的成員表示,然后通過(guò)類型轉(zhuǎn)換來(lái)訪問(wèn)thread_info結(jié)構(gòu)。
相關(guān)代碼在include/linux/sched.h中
#define task_thread_info(task) ((struct thread_info *)(task)->stack)
在這個(gè)圖中,esp寄存器是CPU棧指針,用來(lái)存放棧頂單元的地址。在80x86系統(tǒng)中,棧起始于頂端,并朝著這個(gè)內(nèi)存區(qū)開始的方向增長(zhǎng)。從用戶態(tài)剛切換到內(nèi)核態(tài)以后,進(jìn)程的內(nèi)核??偸强盏?。因此,esp寄存器指向這個(gè)棧的頂端。一旦數(shù)據(jù)寫入堆棧,esp的值就遞減。
內(nèi)核棧數(shù)據(jù)結(jié)構(gòu)描述thread_info和thread_union
thread_info是體系結(jié)構(gòu)相關(guān)的,結(jié)構(gòu)的定義在thread_info.h中

Linux內(nèi)核中使用一個(gè)聯(lián)合體來(lái)表示一個(gè)進(jìn)程的線程描述符和內(nèi)核棧:
union thread_union
{
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
獲取當(dāng)前在CPU上正在運(yùn)行進(jìn)程的thread_info
下面來(lái)說(shuō)說(shuō)如何通過(guò)esp棧指針來(lái)獲取當(dāng)前在CPU上正在運(yùn)行進(jìn)程的thread_info結(jié)構(gòu)。
實(shí)際上,上面提到,thread_info結(jié)構(gòu)和內(nèi)核態(tài)堆棧是緊密結(jié)合在一起的,占據(jù)兩個(gè)頁(yè)框的物理內(nèi)存空間。而且,這兩個(gè)頁(yè)框的起始起始地址是213對(duì)齊的。
早期的版本中,不需要對(duì)64位處理器的支持,所以,內(nèi)核通過(guò)簡(jiǎn)單的屏蔽掉esp的低13位有效位就可以獲得thread_info結(jié)構(gòu)的基地址了。
我們?cè)谙旅鎸?duì)比了,獲取正在運(yùn)行的進(jìn)程的thread_info的實(shí)現(xiàn)方式

早期版本
當(dāng)前的棧指針(current_stack_pointer == sp)就是esp,
THREAD_SIZE為8K,二進(jìn)制的表示為0000 0000 0000 0000 0010 0000 0000 0000。
~(THREAD_SIZE-1)的結(jié)果剛好為1111 1111 1111 1111 1110 0000 0000 0000,第十三位是全為零,也就是剛好屏蔽了esp的低十三位,最終得到的是thread_info的地址。
進(jìn)程最常用的是進(jìn)程描述符結(jié)構(gòu)task_struct而不是thread_info結(jié)構(gòu)的地址。為了獲取當(dāng)前CPU上運(yùn)行進(jìn)程的task_struct結(jié)構(gòu),內(nèi)核提供了current宏,由于task_struct *task在thread_info的起始位置,該宏本質(zhì)上等價(jià)于current_thread_info()->task,在
include/asm-generic/current.h中定義:
#define get_current() (current_thread_info()->task)
#define current get_current()
這個(gè)定義是體系結(jié)構(gòu)無(wú)關(guān)的,當(dāng)然linux也為各個(gè)體系結(jié)構(gòu)定義了更加方便或者快速的current
分配和銷毀thread_info
進(jìn)程通過(guò)alloc_thread_info_node函數(shù)分配它的內(nèi)核棧,通過(guò)free_thread_info函數(shù)釋放所分配的內(nèi)核棧。
# if THREAD_SIZE >= PAGE_SIZE
static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
int node)
{
struct page *page = alloc_kmem_pages_node(node, THREADINFO_GFP,
THREAD_SIZE_ORDER);
return page ? page_address(page) : NULL;
}
static inline void free_thread_info(struct thread_info *ti)
{
free_kmem_pages((unsigned long)ti, THREAD_SIZE_ORDER);
}
# else
static struct kmem_cache *thread_info_cache;
static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
int node)
{
return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node);
}
static void free_thread_info(struct thread_info *ti)
{
kmem_cache_free(thread_info_cache, ti);
}
其中,THREAD_SIZE_ORDER宏的定義請(qǐng)查看

進(jìn)程標(biāo)記
unsigned int flags; /* per process flags, defined below */
反應(yīng)進(jìn)程狀態(tài)的信息,但不是運(yùn)行狀態(tài),用于內(nèi)核識(shí)別進(jìn)程當(dāng)前的狀態(tài),以備下一步操作
flags成員的可能取值如下,這些宏以PF(ProcessFlag)開頭
例如
PF_FORKNOEXEC 進(jìn)程剛創(chuàng)建,但還沒(méi)執(zhí)行。
PF_SUPERPRIV 超級(jí)用戶特權(quán)。
PF_DUMPCORE dumped core。
PF_SIGNALED 進(jìn)程被信號(hào)(signal)殺出。
PF_EXITING 進(jìn)程開始關(guān)閉。
/*
* Per process flags
*/
#define PF_EXITING 0x00000004 /* getting shut down */
#define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
#define PF_VCPU 0x00000010 /* I'm a virtual CPU */
#define PF_WQ_WORKER 0x00000020 /* I'm a workqueue worker */
#define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
#define PF_MCE_PROCESS 0x00000080 /* process policy on mce errors */
#define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
#define PF_DUMPCORE 0x00000200 /* dumped core */
#define PF_SIGNALED 0x00000400 /* killed by a signal */
#define PF_MEMALLOC 0x00000800 /* Allocating memory */
#define PF_NPROC_EXCEEDED 0x00001000 /* set_user noticed that RLIMIT_NPROC was exceeded */
#define PF_USED_MATH 0x00002000 /* if unset the fpu must be initialized before use */
#define PF_USED_ASYNC 0x00004000 /* used async_schedule*(), used by module init */
#define PF_NOFREEZE 0x00008000 /* this thread should not be frozen */
#define PF_FROZEN 0x00010000 /* frozen for system suspend */
#define PF_FSTRANS 0x00020000 /* inside a filesystem transaction */
#define PF_KSWAPD 0x00040000 /* I am kswapd */
#define PF_MEMALLOC_NOIO 0x00080000 /* Allocating memory without IO involved */
#define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */
#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
#define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
#define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
#define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
#define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
#define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
#define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezable */
#define PF_SUSPEND_TASK 0x80000000 /* this thread called freeze_processes and should not be frozen */
表示進(jìn)程親屬關(guān)系的成員
/*
* pointers to (original) parent process, youngest child, younger sibling,
* older sibling, respectively. (p->father can be replaced with
* p->real_parent->pid)
*/
struct task_struct __rcu *real_parent; /* real parent process */
struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
/*
* children/sibling forms the list of my natural children
*/
struct list_head children; /* list of my children */
struct list_head sibling; /* linkage in my parent's children list */
struct task_struct *group_leader; /* threadgroup leader */
在Linux系統(tǒng)中,所有進(jìn)程之間都有著直接或間接地聯(lián)系,每個(gè)進(jìn)程都有其父進(jìn)程,也可能有零個(gè)或多個(gè)子進(jìn)程。擁有同一父進(jìn)程的所有進(jìn)程具有兄弟關(guān)系。

ptrace系統(tǒng)調(diào)用
Ptrace 提供了一種父進(jìn)程可以控制子進(jìn)程運(yùn)行,并可以檢查和改變它的核心image。
它主要用于實(shí)現(xiàn)斷點(diǎn)調(diào)試。一個(gè)被跟蹤的進(jìn)程運(yùn)行中,直到發(fā)生一個(gè)信號(hào)。則進(jìn)程被中止,并且通知其父進(jìn)程。在進(jìn)程中止的狀態(tài)下,進(jìn)程的內(nèi)存空間可以被讀寫。父進(jìn)程還可以使子進(jìn)程繼續(xù)執(zhí)行,并選擇是否是否忽略引起中止的信號(hào)。
unsigned int ptrace;
ptraced is the list of tasks this task is using ptrace on.
* This includes both natural children and PTRACE_ATTACH targets.
* p->ptrace_entry is p's link on the p->parent->ptraced list.
*/
struct list_head ptraced;
struct list_head ptrace_entry;
unsigned long ptrace_message;
siginfo_t *last_siginfo; /* For ptrace use. */
成員ptrace被設(shè)置為0時(shí)表示不需要被跟蹤,它的可能取值如下:
/*
* Ptrace flags
*
* The owner ship rules for task->ptrace which holds the ptrace
* flags is simple. When a task is running it owns it's task->ptrace
* flags. When the a task is stopped the ptracer owns task->ptrace.
*/
#define PT_SEIZED 0x00010000 /* SEIZE used, enable new behavior */
#define PT_PTRACED 0x00000001
#define PT_DTRACE 0x00000002 /* delayed trace (used on m68k, i386) */
#define PT_PTRACE_CAP 0x00000004 /* ptracer can follow suid-exec */
#define PT_OPT_FLAG_SHIFT 3
/* PT_TRACE_* event enable flags */
#define PT_EVENT_FLAG(event) (1 << (PT_OPT_FLAG_SHIFT + (event)))
#define PT_TRACESYSGOOD PT_EVENT_FLAG(0)
#define PT_TRACE_FORK PT_EVENT_FLAG(PTRACE_EVENT_FORK)
#define PT_TRACE_VFORK PT_EVENT_FLAG(PTRACE_EVENT_VFORK)
#define PT_TRACE_CLONE PT_EVENT_FLAG(PTRACE_EVENT_CLONE)
#define PT_TRACE_EXEC PT_EVENT_FLAG(PTRACE_EVENT_EXEC)
#define PT_TRACE_VFORK_DONE PT_EVENT_FLAG(PTRACE_EVENT_VFORK_DONE)
#define PT_TRACE_EXIT PT_EVENT_FLAG(PTRACE_EVENT_EXIT)
#define PT_TRACE_SECCOMP PT_EVENT_FLAG(PTRACE_EVENT_SECCOMP)
#define PT_EXITKILL (PTRACE_O_EXITKILL << PT_OPT_FLAG_SHIFT)
#define PT_SUSPEND_SECCOMP (PTRACE_O_SUSPEND_SECCOMP << PT_OPT_FLAG_SHIFT)
/* single stepping state bits (used on ARM and PA-RISC) */
#define PT_SINGLESTEP_BIT 31
#define PT_SINGLESTEP (1<
#define PT_BLOCKSTEP_BIT 30
#define PT_BLOCKSTEP (1<
Performance Event
Performance Event是一款隨 Linux 內(nèi)核代碼一同發(fā)布和維護(hù)的性能診斷工具。這些成員用于幫助PerformanceEvent分析進(jìn)程的性能問(wèn)題。
#ifdef CONFIG_PERF_EVENTS
struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
struct mutex perf_event_mutex;
struct list_head perf_event_list;
#endif
進(jìn)程調(diào)度
優(yōu)先級(jí)
int prio, static_prio, normal_prio;
unsigned int rt_priority;

實(shí)時(shí)優(yōu)先級(jí)范圍是0到MAX_RT_PRIO-1(即99),而普通進(jìn)程的靜態(tài)優(yōu)先級(jí)范圍是從MAX_RT_PRIO到MAX_PRIO-1(即100到139)。值越大靜態(tài)優(yōu)先級(jí)越低。
/* http://lxr.free-electrons.com/source/include/linux/sched/prio.h#L21 */
#define MAX_USER_RT_PRIO 100
#define MAX_RT_PRIO MAX_USER_RT_PRIO
/* http://lxr.free-electrons.com/source/include/linux/sched/prio.h#L24 */
#define MAX_PRIO (MAX_RT_PRIO + 40)
#define DEFAULT_PRIO (MAX_RT_PRIO + 20)
調(diào)度策略相關(guān)字段
/* http://lxr.free-electrons.com/source/include/linux/sched.h?v=4.5#L1426 */
unsigned int policy;
/* http://lxr.free-electrons.com/source/include/linux/sched.h?v=4.5#L1409 */
const struct sched_class *sched_class;
struct sched_entity se;
struct sched_rt_entity rt;
cpumask_t cpus_allowed;

調(diào)度策略
policy表示進(jìn)程的調(diào)度策略,目前主要有以下五種:
/*
* Scheduling policies
*/
#define SCHED_NORMAL 0
#define SCHED_FIFO 1
#define SCHED_RR 2
#define SCHED_BATCH 3
/* SCHED_ISO: reserved but not implemented yet */
#define SCHED_IDLE 5
#define SCHED_DEADLINE 6

調(diào)度類
sched_class結(jié)構(gòu)體表示調(diào)度類,目前內(nèi)核中有實(shí)現(xiàn)以下四種:
extern const struct sched_class stop_sched_class;
extern const struct sched_class dl_sched_class;
extern const struct sched_class rt_sched_class;
extern const struct sched_class fair_sched_class;
extern const struct sched_class idle_sched_class;

目前系統(tǒng)中,Scheduling Class的優(yōu)先級(jí)順序?yàn)镾topTask > RealTime > Fair > IdleTask
開發(fā)者可以根據(jù)己的設(shè)計(jì)需求,來(lái)把所屬的Task配置到不同的Scheduling Class中.
進(jìn)程地址空間
/* http://lxr.free-electrons.com/source/include/linux/sched.h?V=4.5#L1453 */
struct mm_struct *mm, *active_mm;
/* per-thread vma caching */
u32 vmacache_seqnum;
struct vm_area_struct *vmacache[VMACACHE_SIZE];
#if defined(SPLIT_RSS_COUNTING)
struct task_rss_stat rss_stat;
#endif
/* http://lxr.free-electrons.com/source/include/linux/sched.h?V=4.5#L1484 */
#ifdef CONFIG_COMPAT_BRK
unsigned brk_randomized:1;
#endif

因此如果當(dāng)前內(nèi)核線程被調(diào)度之前運(yùn)行的也是另外一個(gè)內(nèi)核線程時(shí)候,那么其mm和avtive_mm都是NULL
判斷標(biāo)志
int exit_code, exit_signal;
int pdeath_signal; /* The signal sent when the parent dies */
unsigned long jobctl; /* JOBCTL_*, siglock protected */
/* Used for emulating ABI behavior of previous Linux versions */
unsigned int personality;
/* scheduler bits, serialized by scheduler locks */
unsigned sched_reset_on_fork:1;
unsigned sched_contributes_to_load:1;
unsigned sched_migrated:1;
unsigned :0; /* force alignment to the next boundary */
/* unserialized, strictly 'current' */
unsigned in_execve:1; /* bit to tell LSMs we're in execve */
unsigned in_iowait:1;

時(shí)間
cputime_t utime, stime, utimescaled, stimescaled;
cputime_t gtime;
struct prev_cputime prev_cputime;
#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
seqcount_t vtime_seqcount;
unsigned long long vtime_snap;
enum {
/* Task is sleeping or running in a CPU with VTIME inactive */
VTIME_INACTIVE = 0,
/* Task runs in userspace in a CPU with VTIME active */
VTIME_USER,
/* Task runs in kernelspace in a CPU with VTIME active */
VTIME_SYS,
} vtime_snap_whence;
#endif
unsigned long nvcsw, nivcsw; /* context switch counts */
u64 start_time; /* monotonic time in nsec */
u64 real_start_time; /* boot based time in nsec */
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
unsigned long min_flt, maj_flt;
struct task_cputime cputime_expires;
struct list_head cpu_timers[3];
/* process credentials */
const struct cred __rcu *real_cred; /* objective and real subjective task
* credentials (COW) */
const struct cred __rcu *cred; /* effective (overridable) subjective task
* credentials (COW) */
char comm[TASK_COMM_LEN]; /* executable name excluding path
- access with [gs]et_task_comm (which lock
it with task_lock())
- initialized normally by setup_new_exec */
/* file system info */
struct nameidata *nameidata;
#ifdef CONFIG_SYSVIPC
/* ipc stuff */
struct sysv_sem sysvsem;
struct sysv_shm sysvshm;
#endif
#ifdef CONFIG_DETECT_HUNG_TASK
/* hung task detection */
unsigned long last_switch_count;
#endif

信號(hào)處理
/* signal handlers */
struct signal_struct *signal;
struct sighand_struct *sighand;
1583
sigset_t blocked, real_blocked;
sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
struct sigpending pending;
1587
unsigned long sas_ss_sp;
size_t sas_ss_size;

其他
(1)、用于保護(hù)資源分配或釋放的自旋鎖
/* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
* mempolicy */
spinlock_t alloc_lock;
(2)、進(jìn)程描述符使用計(jì)數(shù),被置為2時(shí),表示進(jìn)程描述符正在被使用而且其相應(yīng)的進(jìn)程處于活動(dòng)狀態(tài)
atomic_t usage;
(3)、用于表示獲取大內(nèi)核鎖的次數(shù),如果進(jìn)程未獲得過(guò)鎖,則置為-1。
int lock_depth; /* BKL lock depth */
(4)、在SMP上幫助實(shí)現(xiàn)無(wú)加鎖的進(jìn)程切換(unlocked context switches)
#ifdef CONFIG_SMP
#ifdef __ARCH_WANT_UNLOCKED_CTXSW
int oncpu;
#endif
#endif
(5)、preempt_notifier結(jié)構(gòu)體鏈表
#ifdef CONFIG_PREEMPT_NOTIFIERS
/* list of struct preempt_notifier: */
struct hlist_head preempt_notifiers;
#endif
(6)、FPU使用計(jì)數(shù)
unsigned char fpu_counter;
(7)、 blktrace是一個(gè)針對(duì)Linux內(nèi)核中塊設(shè)備I/O層的跟蹤工具。
#ifdef CONFIG_BLK_DEV_IO_TRACE
unsigned int btrace_seq;
#endif
(8)、RCU同步原語(yǔ)
#ifdef CONFIG_PREEMPT_RCU
int rcu_read_lock_nesting;
char rcu_read_unlock_special;
struct list_head rcu_node_entry;
#endif /* #ifdef CONFIG_PREEMPT_RCU */
#ifdef CONFIG_TREE_PREEMPT_RCU
struct rcu_node *rcu_blocked_node;
#endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
#ifdef CONFIG_RCU_BOOST
struct rt_mutex *rcu_boost_mutex;
#endif /* #ifdef CONFIG_RCU_BOOST */
(9)、用于調(diào)度器統(tǒng)計(jì)進(jìn)程的運(yùn)行信息
#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
struct sched_info sched_info;
#endif
(10)、用于構(gòu)建進(jìn)程鏈表
struct list_head tasks;
(11)、to limit pushing to one attempt
#ifdef CONFIG_SMP
struct plist_node pushable_tasks;
#endif
(12)、防止內(nèi)核堆棧溢出
#ifdef CONFIG_CC_STACKPROTECTOR
/* Canary value for the -fstack-protector gcc feature */
unsigned long stack_canary;
#endif
在GCC編譯內(nèi)核時(shí),需要加上-fstack-protector選項(xiàng)。
(13)、PID散列表和鏈表
/* PID/PID hash table linkage. */
struct pid_link pids[PIDTYPE_MAX];
struct list_head thread_group; //線程組中所有進(jìn)程的鏈表
(14)、do_fork函數(shù)
struct completion *vfork_done; /* for vfork() */
int __user *set_child_tid; /* CLONE_CHILD_SETTID */
int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
在執(zhí)行do_fork()時(shí),如果給定特別標(biāo)志,則vfork_done會(huì)指向一個(gè)特殊地址。
如果copy_process函數(shù)的clone_flags參數(shù)的值被置為CLONE_CHILD_SETTID或CLONE_CHILD_CLEARTID,則會(huì)把child_tidptr參數(shù)的值分別復(fù)制到set_child_tid和clear_child_tid成員。這些標(biāo)志說(shuō)明必須改變子進(jìn)程用戶態(tài)地址空間的child_tidptr所指向的變量的值。
(15)、缺頁(yè)統(tǒng)計(jì)
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
unsigned long min_flt, maj_flt;
(16)、進(jìn)程權(quán)能
const struct cred __rcu *real_cred; /* objective and real subjective task
* credentials (COW) */
const struct cred __rcu *cred; /* effective (overridable) subjective task
* credentials (COW) */
struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */
(17)、相應(yīng)的程序名
char comm[TASK_COMM_LEN];
(18)、文件
/* file system info */
int link_count, total_link_count;
/* filesystem information */
struct fs_struct *fs;
/* open file information */
struct files_struct *files;
fs用來(lái)表示進(jìn)程與文件系統(tǒng)的聯(lián)系,包括當(dāng)前目錄和根目錄。
files表示進(jìn)程當(dāng)前打開的文件。
(19)、進(jìn)程通信(SYSVIPC)
#ifdef CONFIG_SYSVIPC
/* ipc stuff */
struct sysv_sem sysvsem;
#endif
(20)、處理器特有數(shù)據(jù)
/* CPU-specific state of this task */
struct thread_struct thread;
(21)、命名空間
/* namespaces */
struct nsproxy *nsproxy;
(22)、進(jìn)程審計(jì)
struct audit_context *audit_context;
#ifdef CONFIG_AUDITSYSCALL
uid_t loginuid;
unsigned int sessionid;
#endif
(23)、secure computing
seccomp_t seccomp;
(24)、用于copy_process函數(shù)使用CLONE_PARENT 標(biāo)記時(shí)
/* Thread group tracking */
u32 parent_exec_id;
u32 self_exec_id;
(25)、中斷
#ifdef CONFIG_GENERIC_HARDIRQS
/* IRQ handler threads */
struct irqaction *irqaction;
#endif
#ifdef CONFIG_TRACE_IRQFLAGS
unsigned int irq_events;
unsigned long hardirq_enable_ip;
unsigned long hardirq_disable_ip;
unsigned int hardirq_enable_event;
unsigned int hardirq_disable_event;
int hardirqs_enabled;
int hardirq_context;
unsigned long softirq_disable_ip;
unsigned long softirq_enable_ip;
unsigned int softirq_disable_event;
unsigned int softirq_enable_event;
int softirqs_enabled;
int softirq_context;
#endif
(26)、task_rq_lock函數(shù)所使用的鎖
/* Protection of the PI data structures: */
raw_spinlock_t pi_lock;
(27)、基于PI協(xié)議的等待互斥鎖,其中PI指的是priority inheritance(優(yōu)先級(jí)繼承)
#ifdef CONFIG_RT_MUTEXES
/* PI waiters blocked on a rt_mutex held by this task */
struct plist_head pi_waiters;
/* Deadlock detection and priority inheritance handling */
struct rt_mutex_waiter *pi_blocked_on;
#endif
(28)、死鎖檢測(cè)
#ifdef CONFIG_DEBUG_MUTEXES
/* mutex deadlock detection */
struct mutex_waiter *blocked_on;
#endif
(29)、JFS文件系統(tǒng)
/* journalling filesystem info */
void *journal_info;
(30)、塊設(shè)備鏈表
/* stacked block device info */
struct bio_list *bio_list;
(31)、內(nèi)存回收
struct reclaim_state *reclaim_state;
(32)、存放塊設(shè)備I/O數(shù)據(jù)流量信息
struct backing_dev_info *backing_dev_info;
(33)、I/O調(diào)度器所使用的信息
struct io_context *io_context;
(34)、記錄進(jìn)程的I/O計(jì)數(shù)
struct task_io_accounting ioac;
if defined(CONFIG_TASK_XACCT)
u64 acct_rss_mem1; /* accumulated rss usage */
u64 acct_vm_mem1; /* accumulated virtual memory usage */
cputime_t acct_timexpd; /* stime + utime since last update */
endif
在Ubuntu 11.04上,執(zhí)行cat獲得進(jìn)程1的I/O計(jì)數(shù)如下:

輸出的數(shù)據(jù)項(xiàng)剛好是task_io_accounting結(jié)構(gòu)體的所有成員。
(35)、CPUSET功能
#ifdef CONFIG_CPUSETS
nodemask_t mems_allowed; /* Protected by alloc_lock */
int mems_allowed_change_disable;
int cpuset_mem_spread_rotor;
int cpuset_slab_spread_rotor;
#endif
(36)、Control Groups
#ifdef CONFIG_CGROUPS
/* Control Group info protected by css_set_lock */
struct css_set __rcu *cgroups;
/* cg_list protected by css_set_lock and tsk->alloc_lock */
struct list_head cg_list;
#endif
#ifdef CONFIG_CGROUP_MEM_RES_CTLR /* memcg uses this to do batch job */
struct memcg_batch_info {
int do_batch; /* incremented when batch uncharge started */
struct mem_cgroup *memcg; /* target memcg of uncharge */
unsigned long bytes; /* uncharged usage */
unsigned long memsw_bytes; /* uncharged mem+swap usage */
} memcg_batch;
#endif
(37)、futex同步機(jī)制
#ifdef CONFIG_FUTEX
struct robust_list_head __user *robust_list;
#ifdef CONFIG_COMPAT
struct compat_robust_list_head __user *compat_robust_list;
#endif
struct list_head pi_state_list;
struct futex_pi_state *pi_state_cache;
#endif
(38)、非一致內(nèi)存訪問(wèn)(NUMA Non-Uniform Memory Access)
#ifdef CONFIG_NUMA
struct mempolicy *mempolicy; /* Protected by alloc_lock */
short il_next;
#endif
(39)、文件系統(tǒng)互斥資源
atomic_t fs_excl; /* holding fs exclusive resources */
(40)、RCU鏈表
struct rcu_head rcu;
(41)、管道
struct pipe_inode_info *splice_pipe;
(42)、延遲計(jì)數(shù)
#ifdef CONFIG_TASK_DELAY_ACCT
struct task_delay_info *delays;
#endif
(43)、fault injection
#ifdef CONFIG_FAULT_INJECTION
int make_it_fail;
#endif
(44)、FLoating proportions
struct prop_local_single dirties;
(45)、Infrastructure for displayinglatency
#ifdef CONFIG_LATENCYTOP
int latency_record_count;
struct latency_record latency_record[LT_SAVECOUNT];
#endif
(46)、time slack values,常用于poll和select函數(shù)
unsigned long timer_slack_ns;
unsigned long default_timer_slack_ns;
(48)、socket控制消息(control message)
struct list_head *scm_work_list;
(47)、ftrace跟蹤器
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
/* Index of current stored address in ret_stack */
int curr_ret_stack;
/* Stack of return addresses for return function tracing */
struct ftrace_ret_stack *ret_stack;
/* time stamp for last schedule */
unsigned long long ftrace_timestamp;
/*
* Number of functions that haven't been traced
* because of depth overrun.
*/
atomic_t trace_overrun;
/* Pause for the tracing */
atomic_t tracing_graph_pause;
#endif
#ifdef CONFIG_TRACING
/* state flags for use by tracers */
unsigned long trace;
/* bitmask of trace recursion */
unsigned long trace_recursion;
#endif /* CONFIG_TRACING */
原文標(biāo)題:Linux進(jìn)程描述符task_struct結(jié)構(gòu)體詳解
文章出處:【微信公眾號(hào):一口Linux】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
-
內(nèi)核
+關(guān)注
關(guān)注
4文章
1476瀏覽量
43107 -
Linux
+關(guān)注
關(guān)注
88文章
11831瀏覽量
219670 -
結(jié)構(gòu)體
+關(guān)注
關(guān)注
1文章
131瀏覽量
11417
原文標(biāo)題:Linux進(jìn)程描述符task_struct結(jié)構(gòu)體詳解
文章出處:【微信號(hào):yikoulinux,微信公眾號(hào):一口Linux】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
一文詳解Linux內(nèi)核源碼組織結(jié)構(gòu)
Linux下的進(jìn)程結(jié)構(gòu)
Linux內(nèi)核結(jié)構(gòu)詳解
詳解task_struct的結(jié)構(gòu)
Linux內(nèi)核創(chuàng)建新進(jìn)程的過(guò)程分析
Linux內(nèi)核源碼目錄結(jié)構(gòu)
簡(jiǎn)單分析linux內(nèi)核中的結(jié)構(gòu)體使用方法
Linux內(nèi)核中的數(shù)據(jù)結(jié)構(gòu)的一點(diǎn)認(rèn)識(shí)
LINUX 進(jìn)程源代碼分析
Linux0.11-內(nèi)存組織和進(jìn)程結(jié)構(gòu)
你了解Linux0.11-進(jìn)程相關(guān)的數(shù)據(jù)結(jié)構(gòu)?
詳解linux內(nèi)核VFS
linux內(nèi)核源代碼分析:進(jìn)程的task_struct 結(jié)構(gòu)資料下載
Linux內(nèi)核如何使用結(jié)構(gòu)體和函數(shù)指針?
如何查看一個(gè)線程的ID
linux內(nèi)核源碼中的task_struct結(jié)構(gòu)體
評(píng)論