Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Unix知識 >> Unix基礎知識

核心學習:task_struct 進程描述符逐欄解析

要理解 Linux 的進程管理,就繞不開 task_struct。它是核心描述與維護進程狀態的唯一手段,也是所有調度、切換、訊號處理的資料基礎。

一、程式與進程

程式是存放在磁碟上的可執行映像,是靜態實體;進程則是「正在執行的程式」,除了指令段與資料段,還包含當前狀態、暫存堆疊、暫存器內容等動態資訊,統稱為進程上下文。從核心角度看,進程是分配記憶體與 CPU 時間的最小單位。

二、進程與執行緒

隨著應用從 CPU 密集型走向 I/O 密集型,人們用「分而治之」提出進程;但進程間切換開銷龐大,於是再抽象出執行緒。一個進程由「執行緒集合」與「資源集合」組成,同一進程內的所有執行緒共享資源,但各自擁有獨立的程式計數器、堆疊與暫存器。

Linux 並未為執行緒設計獨立結構,進程與執行緒都以 task_struct 描述,定義於 2.6 版的 include/linux/sched.h。

三、重要欄位分類

  • state:-1 不可執行、0 可執行、>0 已停止。
  • prio / static_prio / normal_prio / rt_priority:動態、靜態、普通與實時優先級。
  • sched_class / se / rt:所屬調度類別與各自的排程實體。
  • mm / active_mm:記憶體描述符,記錄位址空間與 RSS 統計。
  • tasks / children / sibling:把所有進程串成雙向鏈表,並維護父子與兄弟關係。
  • real_parent / parent:真正的父進程與目前名義上的父進程(可能被 reparent)。
  • group_leader / thread_group:主執行緒描述符與同組執行緒鏈表。
  • pid / tgid:進程號與執行緒群組號。
  • utime / stime:使用者態與核心態的 CPU 時間。
  • fs / files:檔案系統資訊與開啟檔案表,驅動開發時常見。
  • signal / sighand / blocked / pending:訊號處理與待處理集合。
  • comm:進程名稱字元陣列。
  • thread:特定 CPU 下的硬體上下文。

四、原始碼節錄

struct task_struct {
    /* -1 unrunnable, 0 runnable, >0 stopped */
    volatile long state;
    void *stack;
    atomic_t usage;
    unsigned int flags;
    unsigned int ptrace;
    int lock_depth;

    int prio, static_prio, normal_prio;   /* 運行優先級 */
    unsigned int rt_priority;
    const struct sched_class *sched_class;
    struct sched_entity se;
    struct sched_rt_entity rt;

    struct list_head tasks;
    struct plist_node pushable_tasks;

    struct mm_struct *mm, *active_mm;     /* 記憶體使用情況 */

    int exit_state;
    int exit_code, exit_signal;
    int pdeath_signal;                    /* 父進程死亡時送出的訊號 */
    unsigned int personality;
    unsigned did_exec:1;
    unsigned in_execve:1;
    unsigned in_iowait:1;
    unsigned sched_reset_on_fork:1;

    pid_t pid;                            /* 進程號 */
    pid_t tgid;                           /* 執行緒群組號 */

    struct task_struct *real_parent;      /* 真正的父進程 */
    struct task_struct *parent;           /* 名義上的父進程 */
    struct list_head children;            /* 我的孩子們 */
    struct list_head sibling;             /* 我在父親的孩子鏈表中 */
    struct task_struct *group_leader;     /* 執行緒群組領袖 */

    struct list_head ptraced;
    struct list_head ptrace_entry;
    struct pid_link pids[PIDTYPE_MAX];
    struct list_head thread_group;        /* 同組所有執行緒 */
    struct completion *vfork_done;
    int __user *set_child_tid;
    int __user *clear_child_tid;

    cputime_t utime, stime, utimescaled, stimescaled;  /* 用戶態/核心態時間 */
    cputime_t gtime;
    unsigned long nvcsw, nivcsw;          /* 上下文切換次數 */

    struct timespec start_time;           /* 啟動時間 */
    struct timespec real_start_time;
    unsigned long min_flt, maj_flt;       /* 缺頁與主缺頁 */

    const struct cred __rcu *real_cred;
    const struct cred __rcu *cred;

    char comm[TASK_COMM_LEN];             /* 進程名稱 */
    int link_count, total_link_count;

    struct thread_struct thread;          /* 特定 CPU 的狀態 */
    struct fs_struct *fs;                 /* 檔案系統資訊 */
    struct files_struct *files;           /* 開啟檔案資訊 */
    struct nsproxy *nsproxy;              /* 命名空間 */
    struct signal_struct *signal;
    struct sighand_struct *sighand;
    sigset_t blocked, real_blocked;
    sigset_t saved_sigmask;
    struct sigpending pending;

    spinlock_t alloc_lock;
    raw_spinlock_t pi_lock;
    struct list_head pi_waiters;
    struct rt_mutex_waiter *pi_blocked_on;

    struct reclaim_state *reclaim_state;
    struct backing_dev_info *backing_dev_info;
    struct io_context *io_context;
    struct task_io_accounting ioac;

    nodemask_t mems_allowed;
    struct css_set __rcu *cgroups;
    struct list_head cg_list;

    struct robust_list_head __user *robust_list;
    struct list_head pi_state_list;
    struct futex_pi_state *pi_state_cache;

    atomic_t fs_excl;
    struct rcu_head rcu;
    struct pipe_inode_info *splice_pipe;
    struct prop_local_single dirties;

    /* 松弛時間值,用於 select() 與 poll() 的超時,單位為奈秒 */
    unsigned long timer_slack_ns;
    unsigned long default_timer_slack_ns;
    struct list_head *scm_work_list;
};

以上是節錄與要點註解,後續會逐一深入探討記憶體、檔案、訊號等子結構及其操作介面。理解 task_struct 的欄位分區,等於拿到了一張核心進程管理的地圖。

Copyright © Linux教程網 All Rights Reserved