進程是處于執行期的程序以及它所管理的資源(如打開的文件、掛起的信號、進程狀態、地址空間等等)的總稱。注意,程序并不是進程,實際上兩個或多個進程不僅有可能執行同一程序,而且還有可能共享地址空間等資源。
Linux內核通過一個被稱為進程描述符的task_struct結構體來管理進程,這個結構體包含了一個進程所需的所有信息。它定義在include/linux/sched.h文件中。
談到task_struct結構體,可以說她是linux內核源碼中最復雜的一個結構體了,成員之多,占用內存之大。
鑒于她的復雜,我們不能簡單的褻瀆,而是要深入“窺探”.
下面來慢慢介紹這些復雜成員
進程狀態
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個互斥狀態
state域能夠取5個互為排斥的值(通俗一點就是這五個值任意兩個不能一起使用,只能單獨使用)。系統中的每個進程都必然處于以上所列進程狀態中的一種。
2個終止狀態
其實還有兩個附加的進程狀態既可以被添加到state域中,又可以被添加到exit_state域中。只有當進程終止的時候,才會達到這兩種狀態.
/* task state */
int exit_state;
int exit_code, exit_signal;
新增睡眠狀態
進程狀態 TASK_UNINTERRUPTIBLE 和 TASK_INTERRUPTIBLE 都是睡眠狀態。現在,我們來看看內核如何將進程置為睡眠狀態。
內核如何將進程置為睡眠狀態
Linux 內核提供了兩種方法將進程置為睡眠狀態。
將進程置為睡眠狀態的普通方法是將進程狀態設置為 TASK_INTERRUPTIBLE 或 TASK_UNINTERRUPTIBLE 并調用調度程序的 schedule() 函數。這樣會將進程從 CPU 運行隊列中移除。
-
如果進程處于可中斷模式的睡眠狀態(通過將其狀態設置為 TASK_INTERRUPTIBLE),那么可以通過顯式的喚醒呼叫(wakeup_process())或需要處理的信號來喚醒它。
-
但是,如果進程處于非可中斷模式的睡眠狀態(通過將其狀態設置為 TASK_UNINTERRUPTIBLE),那么只能通過顯式的喚醒呼叫將其喚醒。除非萬不得已,否則我們建議您將進程置為可中斷睡眠模式,而不是不可中斷睡眠模式(比如說在設備 I/O 期間,處理信號非常困難時)。
當處于可中斷睡眠模式的任務接收到信號時,它需要處理該信號(除非它已被屏弊),離開之前正在處理的任務(此處需要清除代碼),并將 -EINTR 返回給用戶空間。再一次,檢查這些返回代碼和采取適當操作的工作將由程序員完成。
因此,懶惰的程序員可能比較喜歡將進程置為不可中斷模式的睡眠狀態,因為信號不會喚醒這類任務。
但需要注意的一種情況是,對不可中斷睡眠模式的進程的喚醒呼叫可能會由于某些原因不會發生,這會使進程無法被終止,從而最終引發問題,因為惟一的解決方法就是重啟系統。一方面,您需要考慮一些細節,因為不這樣做會在內核端和用戶端引入 bug。另一方面,您可能會生成永遠不會停止的進程(被阻塞且無法終止的進程)。
現在,我們在內核中實現了一種新的睡眠方法
Linux Kernel 2.6.25 引入了一種新的進程睡眠狀態
它定義如下:
#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)
換句話說,TASK_UNINTERRUPTIBLE + TASK_WAKEKILL = TASK_KILLABLE。
而TASK_WAKEKILL 用于在接收到致命信號時喚醒進程
新的睡眠狀態允許 TASK_UNINTERRUPTIBLE 響應致命信號
進程狀態的切換過程和原因大致如下圖
進程標識符(PID)
pid_t pid;
pid_t tgid;
Unix系統通過pid來標識進程,linux把不同的pid與系統中每個進程或輕量級線程關聯,而unix程序員希望同一組線程具有共同的pid,遵照這個標準linux引入線程組的概念。一個線程組所有線程與領頭線程具有相同的pid,存入tgid字段,getpid()返回當前進程的tgid值而不是pid的值。
在CONFIG_BASE_SMALL配置為0的情況下,PID的取值范圍是0到32767,即系統中的進程數最大為32768個。
#define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000)
在Linux系統中,一個線程組中的所有線程使用和該線程組的領頭線程(該組中的第一個輕量級進程)相同的PID,并被存放在tgid成員中。只有線程組的領頭線程的pid成員才會被設置為與tgid相同的值。注意,getpid()系統調用返回的是當前進程的tgid值而不是pid值。
進程內核棧
void *stack;
內核棧與線程描述符
對每個進程,Linux內核都把兩個不同的數據結構緊湊的存放在一個單獨為進程分配的內存區域中
-
一個是內核態的進程堆棧,
-
另一個是緊挨著進程描述符的小數據結構thread_info,叫做線程描述符。
Linux把thread_info(線程描述符)和內核態的線程堆棧存放在一起,這塊區域通常是8192K(占兩個頁框),其實地址必須是8192的整數倍。
在
linux/arch/x86/include/asm/page_32_types.h中,
#define THREAD_SIZE_ORDER 1
#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)
出于效率考慮,內核讓這8K空間占據連續的兩個頁框并讓第一個頁框的起始地址是213的倍數。
內核態的進程訪問處于內核數據段的棧,這個棧不同于用戶態的進程所用的棧。
用戶態進程所用的棧,是在進程線性地址空間中;
而內核棧是當進程從用戶空間進入內核空間時,特權級發生變化,需要切換堆棧,那么內核空間中使用的就是這個內核棧。因為內核控制路徑使用很少的棧空間,所以只需要幾千個字節的內核態堆棧。
需要注意的是,內核態堆棧僅用于內核例程,Linux內核另外為中斷提供了單獨的硬中斷棧和軟中斷棧
下圖中顯示了在物理內存中存放兩種數據結構的方式。線程描述符駐留與這個內存區的開始,而棧頂末端向下增長。下圖摘自ULK3,進程內核棧與進程描述符的關系如下圖:
但是較新的內核代碼中,進程描述符task_struct結構中沒有直接指向thread_info結構的指針,而是用一個void指針類型的成員表示,然后通過類型轉換來訪問thread_info結構。
相關代碼在include/linux/sched.h中
#define task_thread_info(task) ((struct thread_info *)(task)->stack)
在這個圖中,esp寄存器是CPU棧指針,用來存放棧頂單元的地址。在80x86系統中,棧起始于頂端,并朝著這個內存區開始的方向增長。從用戶態剛切換到內核態以后,進程的內核棧總是空的。因此,esp寄存器指向這個棧的頂端。一旦數據寫入堆棧,esp的值就遞減。
內核棧數據結構描述thread_info和thread_union
thread_info是體系結構相關的,結構的定義在thread_info.h中
Linux內核中使用一個聯合體來表示一個進程的線程描述符和內核棧:
union thread_union
{
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
獲取當前在CPU上正在運行進程的thread_info
下面來說說如何通過esp棧指針來獲取當前在CPU上正在運行進程的thread_info結構。
實際上,上面提到,thread_info結構和內核態堆棧是緊密結合在一起的,占據兩個頁框的物理內存空間。而且,這兩個頁框的起始起始地址是213對齊的。
早期的版本中,不需要對64位處理器的支持,所以,內核通過簡單的屏蔽掉esp的低13位有效位就可以獲得thread_info結構的基地址了。
我們在下面對比了,獲取正在運行的進程的thread_info的實現方式
早期版本
當前的棧指針(current_stack_pointer == sp)就是esp,
THREAD_SIZE為8K,二進制的表示為0000 0000 0000 0000 0010 0000 0000 0000。
~(THREAD_SIZE-1)的結果剛好為1111 1111 1111 1111 1110 0000 0000 0000,第十三位是全為零,也就是剛好屏蔽了esp的低十三位,最終得到的是thread_info的地址。
進程最常用的是進程描述符結構task_struct而不是thread_info結構的地址。為了獲取當前CPU上運行進程的task_struct結構,內核提供了current宏,由于task_struct *task在thread_info的起始位置,該宏本質上等價于current_thread_info()->task,在
include/asm-generic/current.h中定義:
#define get_current() (current_thread_info()->task)
#define current get_current()
這個定義是體系結構無關的,當然linux也為各個體系結構定義了更加方便或者快速的current
分配和銷毀thread_info
進程通過alloc_thread_info_node函數分配它的內核棧,通過free_thread_info函數釋放所分配的內核棧。
# 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宏的定義請查看
進程標記
unsigned int flags; /* per process flags, defined below */
反應進程狀態的信息,但不是運行狀態,用于內核識別進程當前的狀態,以備下一步操作
flags成員的可能取值如下,這些宏以PF(ProcessFlag)開頭
例如
PF_FORKNOEXEC 進程剛創建,但還沒執行。
PF_SUPERPRIV 超級用戶特權。
PF_DUMPCORE dumped core。
PF_SIGNALED 進程被信號(signal)殺出。
PF_EXITING 進程開始關閉。
/*
* 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 */
表示進程親屬關系的成員
/*
* 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系統中,所有進程之間都有著直接或間接地聯系,每個進程都有其父進程,也可能有零個或多個子進程。擁有同一父進程的所有進程具有兄弟關系。
ptrace系統調用
Ptrace 提供了一種父進程可以控制子進程運行,并可以檢查和改變它的核心image。
它主要用于實現斷點調試。一個被跟蹤的進程運行中,直到發生一個信號。則進程被中止,并且通知其父進程。在進程中止的狀態下,進程的內存空間可以被讀寫。父進程還可以使子進程繼續執行,并選擇是否是否忽略引起中止的信號。
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被設置為0時表示不需要被跟蹤,它的可能取值如下:
/*
* 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 內核代碼一同發布和維護的性能診斷工具。這些成員用于幫助PerformanceEvent分析進程的性能問題。
#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
進程調度
優先級
int prio, static_prio, normal_prio;
unsigned int rt_priority;
實時優先級范圍是0到MAX_RT_PRIO-1(即99),而普通進程的靜態優先級范圍是從MAX_RT_PRIO到MAX_PRIO-1(即100到139)。值越大靜態優先級越低。
/* 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)
調度策略相關字段
/* 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;
調度策略
policy表示進程的調度策略,目前主要有以下五種:
/*
* 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
調度類
sched_class結構體表示調度類,目前內核中有實現以下四種:
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;
目前系統中,Scheduling Class的優先級順序為StopTask > RealTime > Fair > IdleTask
開發者可以根據己的設計需求,來把所屬的Task配置到不同的Scheduling Class中.
進程地址空間
/* 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
因此如果當前內核線程被調度之前運行的也是另外一個內核線程時候,那么其mm和avtive_mm都是NULL
判斷標志
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;
時間
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
信號處理
/* 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)、用于保護資源分配或釋放的自旋鎖
/* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
* mempolicy */
spinlock_t alloc_lock;
(2)、進程描述符使用計數,被置為2時,表示進程描述符正在被使用而且其相應的進程處于活動狀態
atomic_t usage;
(3)、用于表示獲取大內核鎖的次數,如果進程未獲得過鎖,則置為-1。
int lock_depth; /* BKL lock depth */
(4)、在SMP上幫助實現無加鎖的進程切換(unlocked context switches)
#ifdef CONFIG_SMP
#ifdef __ARCH_WANT_UNLOCKED_CTXSW
int oncpu;
#endif
#endif
(5)、preempt_notifier結構體鏈表
#ifdef CONFIG_PREEMPT_NOTIFIERS
/* list of struct preempt_notifier: */
struct hlist_head preempt_notifiers;
#endif
(6)、FPU使用計數
unsigned char fpu_counter;
(7)、 blktrace是一個針對Linux內核中塊設備I/O層的跟蹤工具。
#ifdef CONFIG_BLK_DEV_IO_TRACE
unsigned int btrace_seq;
#endif
(8)、RCU同步原語
#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)、用于調度器統計進程的運行信息
#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
struct sched_info sched_info;
#endif
(10)、用于構建進程鏈表
struct list_head tasks;
(11)、to limit pushing to one attempt
#ifdef CONFIG_SMP
struct plist_node pushable_tasks;
#endif
(12)、防止內核堆棧溢出
#ifdef CONFIG_CC_STACKPROTECTOR
/* Canary value for the -fstack-protector gcc feature */
unsigned long stack_canary;
#endif
在GCC編譯內核時,需要加上-fstack-protector選項。
(13)、PID散列表和鏈表
/* PID/PID hash table linkage. */
struct pid_link pids[PIDTYPE_MAX];
struct list_head thread_group; //線程組中所有進程的鏈表
(14)、do_fork函數
struct completion *vfork_done; /* for vfork() */
int __user *set_child_tid; /* CLONE_CHILD_SETTID */
int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
在執行do_fork()時,如果給定特別標志,則vfork_done會指向一個特殊地址。
如果copy_process函數的clone_flags參數的值被置為CLONE_CHILD_SETTID或CLONE_CHILD_CLEARTID,則會把child_tidptr參數的值分別復制到set_child_tid和clear_child_tid成員。這些標志說明必須改變子進程用戶態地址空間的child_tidptr所指向的變量的值。
(15)、缺頁統計
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
unsigned long min_flt, maj_flt;
(16)、進程權能
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)、相應的程序名
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用來表示進程與文件系統的聯系,包括當前目錄和根目錄。
files表示進程當前打開的文件。
(19)、進程通信(SYSVIPC)
#ifdef CONFIG_SYSVIPC
/* ipc stuff */
struct sysv_sem sysvsem;
#endif
(20)、處理器特有數據
/* CPU-specific state of this task */
struct thread_struct thread;
(21)、命名空間
/* namespaces */
struct nsproxy *nsproxy;
(22)、進程審計
struct audit_context *audit_context;
#ifdef CONFIG_AUDITSYSCALL
uid_t loginuid;
unsigned int sessionid;
#endif
(23)、secure computing
seccomp_t seccomp;
(24)、用于copy_process函數使用CLONE_PARENT 標記時
/* 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函數所使用的鎖
/* Protection of the PI data structures: */
raw_spinlock_t pi_lock;
(27)、基于PI協議的等待互斥鎖,其中PI指的是priority inheritance(優先級繼承)
#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)、死鎖檢測
#ifdef CONFIG_DEBUG_MUTEXES
/* mutex deadlock detection */
struct mutex_waiter *blocked_on;
#endif
(29)、JFS文件系統
/* journalling filesystem info */
void *journal_info;
(30)、塊設備鏈表
/* stacked block device info */
struct bio_list *bio_list;
(31)、內存回收
struct reclaim_state *reclaim_state;
(32)、存放塊設備I/O數據流量信息
struct backing_dev_info *backing_dev_info;
(33)、I/O調度器所使用的信息
struct io_context *io_context;
(34)、記錄進程的I/O計數
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上,執行cat獲得進程1的I/O計數如下:
輸出的數據項剛好是task_io_accounting結構體的所有成員。
(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同步機制
#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)、非一致內存訪問(NUMA Non-Uniform Memory Access)
#ifdef CONFIG_NUMA
struct mempolicy *mempolicy; /* Protected by alloc_lock */
short il_next;
#endif
(39)、文件系統互斥資源
atomic_t fs_excl; /* holding fs exclusive resources */
(40)、RCU鏈表
struct rcu_head rcu;
(41)、管道
struct pipe_inode_info *splice_pipe;
(42)、延遲計數
#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函數
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 */
原文標題:Linux進程描述符task_struct結構體詳解
文章出處:【微信公眾號:一口Linux】歡迎添加關注!文章轉載請注明出處。
-
內核
+關注
關注
3文章
1415瀏覽量
41256 -
Linux
+關注
關注
87文章
11497瀏覽量
213266 -
結構體
+關注
關注
1文章
130瀏覽量
11071
原文標題:Linux進程描述符task_struct結構體詳解
文章出處:【微信號:yikoulinux,微信公眾號:一口Linux】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
Linux下的進程結構
Linux內核結構詳解
詳解task_struct的結構
Linux內核創建新進程的過程分析
Linux內核中的數據結構的一點認識
Linux0.11-內存組織和進程結構

你了解Linux0.11-進程相關的數據結構?

詳解linux內核VFS
linux內核源代碼分析:進程的task_struct 結構資料下載

如何查看一個線程的ID

評論