linux進程和task_struct()簡介,linuxtask_struct

來源:互聯網
上載者:User

linux進程和task_struct()簡介,linuxtask_struct

一、構成進程的要素

    在linux系統中,進程主要具備以下要素:

    1)有一個程式供其運行。這段程式不一定是進程所專有,可以與其他進程一起使用;

    2)有起碼的“私人財產”,這就是進程專用的系統堆棧空間;

    3)有“身份證”,也就是task_struct結構,也稱之為“進程式控制制塊”(PCB)。有了這個資料結構,進程才能成為核心調度的一個基本單位接受核心的調度。同時,這個結構又是進程的“財產登記卡”,記錄著進程佔用的各項資源。

    4)有獨立的儲存空間,意味著擁有專有的使用者空間;還意味著除前述的系統空間堆棧外還有其專有的使用者空間堆棧。(PS:進程的系統空間是不能獨立的,除了各進程專屬的系統堆棧空間外,任何進程都不可能直接改變使用者空間的內容)。

    以上條件是必要條件,缺少其中一條,都不能稱其為“進程”。如果只缺第四條,那就成為“線程”。特別地,如果完全沒有使用者空間,就稱其為“核心線程”;而如果共用使用者空間則稱其為“使用者線程”。在不致引起混淆的場合,二者也往往簡稱為“線程”。注意!!!這裡的線程與有些作業系統中在使用者空間的同一進程內實現的“線程”不一樣!既然linux提供了對上述核心線程和使用者線程的支援,也就沒必要再在進程內部,即使用者空間內自行實現線程。

    關於進程和線程的區分不是十分嚴格,在linux系統中,許多進程在誕生之初都與其父進程共同用一個儲存空間,所以也就是使用者線程。但是子進程又可以建立自己的儲存空間,並與父進程“分道揚鑣”,成為與父進程一樣真正意義上的進程。

    在linux系統中,“進程”和“任務”是同一個意思,在核心的代碼中常混用這兩個名詞和概念。例如每個進程都要有一個task_struct資料結構,而其號碼卻又是pid、喚醒一個睡眠進程的函數名為wake_up_process(),之所以有這樣的情況是因為,linux源自Unix和i386系統結構,而unix中的進程在Intel的技術資料中稱為“任務”,嚴格來說有點區別,但是對於系統的實現來說是一回事。

    linux系統啟動並執行第一個進程是在初始化階段“捏造出來的”。而此後的線程或進程都是由一個已存在的進程像細胞分裂一樣通過系統調用複製出來的,成為“fork()”或者“clone()”。

   除了最起碼的“財產”,即task_struct資料結構和系統堆棧之外,一個進程還要有一些附加的資源。例如,進程擁有堵路的儲存空間,就要有用於虛擬記憶體管理的mm_struct資料結構以及附屬的vm_area資料結構,以及相應的頁面目錄項和頁面表,但這些都從屬於task_struct資源。task_struct資料結構在這方面相當於登記卡的作用。

二、task_struct的定義

    task_struct結構原始碼:

  1 struct task_struct  2 {  3     /*  4      * offsets of these are hardcoded elsewhere - touch with care  5      */  6     volatile long state;  /* -1 unrunnable, 0 runnable, >0 stopped */  7     unsigned long flags;  /* per process flags, defined below */  8     int sigpending;  9     mm_segment_t addr_limit;   /* thread address space: 10     0-0xBFFFFFFF for user-thead 11     0-0xFFFFFFFF for kernel-thread 12      */ 13     struct exec_domain *exec_domain; 14     volatile long need_resched; 15     unsigned long ptrace; 16     int lock_depth; /* Lock depth */ 17      18     /* 19      * offset 32 begins here on 32-bit platforms. We keep 20      * all fields in a single cacheline that are needed for 21      * the goodness() loop in schedule(). 22      */ 23     long counter; 24     long nice; 25     unsigned long policy; 26     struct mm_struct *mm; 27     int has_cpu, processor; 28     unsigned long cpus_allowed; 29      30     struct list_head run_list; 31     unsigned long sleep_time; 32      33     struct task_struct *next_task, *prev_task; 34     struct mm_struct *active_mm; 35     /* task state */ 36     struct linux_binfmt *binfmt; 37     int exit_code, exit_signal; 38     int pdeath_signal;  /*  The signal sent when the parent dies  */ 39      40     unsigned long personality; 41     int dumpable:1; 42     int did_exec:1; 43     pid_t pid; 44     pid_t pgrp; 45     pid_t tty_old_pgrp; 46     pid_t session; 47     pid_t tgid; 48     /* boolean value for session group leader */ 49     int leader; 50     /* 51      * pointers to (original) parent process, youngest child, younger sibling, 52      * older sibling, respectively.  (p->father can be replaced with 53      * p->p_pptr->pid) 54      */ 55     struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr; 56     struct list_head thread_group; 57     /* PID hash table linkage. */ 58     struct task_struct *pidhash_next; 59     struct task_struct **pidhash_pprev; 60     wait_queue_head_t wait_chldexit; /* for wait4() */ 61     struct semaphore *vfork_sem; /* for vfork() */ 62     unsigned long rt_priority; 63     unsigned long it_real_value, it_prof_value, it_virt_value; 64     unsigned long it_real_incr, it_prof_incr, it_virt_incr; 65     struct timer_list real_timer; 66     struct tms times; 67     unsigned long start_time; 68     long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; 69     /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ 70     unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap; 71     int swappable:1; 72     /* process credentials */ 73     uid_t uid,euid,suid,fsuid; 74     gid_t gid,egid,sgid,fsgid; 75     int ngroups; 76     gid_t groups[NGROUPS]; 77     kernel_cap_t   cap_effective, cap_inheritable, cap_permitted; 78     int keep_capabilities:1; 79     struct user_struct *user; 80     /* limits */ 81     struct rlimit rlim[RLIM_NLIMITS]; 82     unsigned short used_math; 83     char comm[16]; 84     /* file system info */ 85     int link_count; 86     struct tty_struct *tty; /* NULL if no tty */ 87     unsigned int locks; /* How many file locks are being held */ 88     /* ipc stuff */ 89     struct sem_undo *semundo; 90     struct sem_queue *semsleeping; 91     /* CPU-specific state of this task */ 92     struct thread_struct thread; 93     /* filesystem information */ 94     struct fs_struct *fs; 95     /* open file information */ 96     struct files_struct *files; 97     /* signal handlers */ 98     spinlock_t sigmask_lock; /* Protects signal and blocked */ 99     struct signal_struct *sig;100     101     sigset_t blocked;102     struct sigpending pending;103     104     unsigned long sas_ss_sp;105     size_t sas_ss_size;106     int (*notifier)(void *priv);107     void *notifier_data;108     sigset_t *notifier_mask;109     110     /* Thread group tracking */111     u32 parent_exec_id;112     u32 self_exec_id;113     /* Protection of (de-)allocation: mm, files, fs, tty */114     spinlock_t alloc_lock;115 };

下面對結構中幾個重要的成分做介紹:

1)state(第6行)

該變數表示進程當前啟動並執行狀態,具體定義如下:

1 #define TASK_RUNNING              02 #define TASK_INTERRUPTIBLE        13 #define TASK_UNINTERRUPTIBLE      24 #define TASK_ZOMBIE               4  //殭屍進程5 #define TASK_STOPPED              8

    狀態TASK_INTERRUPTIBLE和TASK_UNINTERRUPTIBLE均表示進程處於睡眠狀態。但是TASK_UNINTERRUPTIBLE表示進程處於“深度睡眠”,而不受“訊號”(signal,也稱非強制中斷)的打擾,而TASK_INTERRUPTIBLE則可以因訊號的到來而被喚醒。核心中提供了不同的函數,讓一個進程進入不同深度的睡眠或將進程從睡眠中喚醒。具體地說,函數sleep_on()和wake_up()用於深度睡眠,而interruptible_sleep_on()和wake_up_interruptible()則用於淺度睡眠。深度睡眠一般只用於臨界區和關鍵性的部位,而“可中斷”的睡眠那就是通用的了。特別地,當進程在“阻塞性”的系統調用中等待某一事件發生時,應該進入可中斷睡眠,否則就不能對別的中斷做出反應,別的進程就不能通過發一個訊號來“殺掉”這個進程了。

    TASK_RUNNING狀態並不是表示一個進程正在執行中,或者說這個進程就是“當前進程”,而是表示這個進程可以被調度執行而成為當前進程。當進程處於這樣的可執行(或就緒)狀態時,核心就將該進程的task_struct結構通過其隊列頭(見第30行)掛入一個“運行隊列”。

    TASK_ZOMBIE狀態表示進程已經“去世”而戶口尚未登出。

    TASK_STOPPED主要用於調試的目的,進程接收到 一個SIGSTOP訊號後就將運行狀態改成     TASK_STOPPED而進入“掛起”狀態,然後在接收到SIGCONT訊號時又恢複繼續運行。

2)flags(第7行)

      flags反應進程狀態相關資訊,但並不是運行狀態,而是與管理有關的其他資訊。

   

 1 #define PF_ALIGNWARN        0x00000001      /*print alignment warning msgs*/ 2 #define PF_STARTING         0x00000002      /*being created*/ 3 #define PF_EXITING          0x00000004      /*getting shut down*/ 4 #define PF_FORKNOEXEC       0x00000040      /*forked but did not exec*/ 5 #define PF_SUPERPRIV        0x00000100      /*uses super-user privileges*/ 6 #define PF_DUMPCORE         0x00000200      /*dumped core*/ 7 #define PF_SIGNALED         0x00000400      /*killed by signal*/ 8 #define PF_MEMALLOC         0x00000800      /*Allocating memory*/ 9 #define PF_VFORK            0x00001000      /*wake up parent in mm_release*/10 #define PF_USEDFPU          0x00100000      /*task used FPU this quantum(SMP)*/

3)sigpending(第8行)

    表示進程收到了“訊號”但是尚未處理。

4)counter(第23行)

    與進程調度有關

5)add_limit

    虛擬位址空間的上限,對進程而言是其使用者空間的上限,所以是0xbfff ffff;對核心線程而言則是系統空間額的上限,所以是0xffff ffff

6)binfnt

    應用程式的檔案格式。

7)pgrp,session,leader

    當一個使用者登入時,就開始了一個進程組(session),此後建立的進程都屬於這同一個session。

8)user

    指向一個user_struct結構,該資料結構代表進程所屬的使用者。

9)rlim

    這是一個結構數組,表明進程歲各種資源的使用數量所受的限制。

 

參考:

毛德操,胡希明《linux核心原始碼情景分析》(上冊)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.