1. 程式人生 > >linux進程和task_struct()簡介

linux進程和task_struct()簡介

wak fff inf fec smp lock print wake 阻塞

一、構成進程的要素

在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              0
2 #define TASK_INTERRUPTIBLE        1
3 #define TASK_UNINTERRUPTIBLE      2
4 #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進程和task_struct()簡介