Linux 源码阅读 进程管理

Linux 源码阅读 进程管理

版本:2.6.24

1.准备知识

1.1 Linux系统中,进程是最小的调度单位;

1.2 PCB数据结构:task_struct (Location:linux-2.6.24\include\linux\sched.h)

  1 struct task_struct {
  2     volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
  3     void *stack;
  4     atomic_t usage;
  5     unsigned int flags;    /* per process flags, defined below */
  6     unsigned int ptrace;
  7 
  8     int lock_depth;        /* BKL lock depth */
  9 
 10 #ifdef CONFIG_SMP
 11 #ifdef __ARCH_WANT_UNLOCKED_CTXSW
 12     int oncpu;
 13 #endif
 14 #endif
 15 
 16     int prio, static_prio, normal_prio;
 17     struct list_head run_list;
 18     const struct sched_class *sched_class;
 19     struct sched_entity se;
 20 
 21 #ifdef CONFIG_PREEMPT_NOTIFIERS
 22     /* list of struct preempt_notifier: */
 23     struct hlist_head preempt_notifiers;
 24 #endif
 25 
 26     unsigned short ioprio;
 27     /*
 28      * fpu_counter contains the number of consecutive context switches
 29      * that the FPU is used. If this is over a threshold, the lazy fpu
 30      * saving becomes unlazy to save the trap. This is an unsigned char
 31      * so that after 256 times the counter wraps and the behavior turns
 32      * lazy again; this to deal with bursty apps that only use FPU for
 33      * a short time
 34      */
 35     unsigned char fpu_counter;
 36     s8 oomkilladj; /* OOM kill score adjustment (bit shift). */
 37 #ifdef CONFIG_BLK_DEV_IO_TRACE
 38     unsigned int btrace_seq;
 39 #endif
 40 
 41     unsigned int policy;
 42     cpumask_t cpus_allowed;
 43     unsigned int time_slice;
 44 
 45 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
 46     struct sched_info sched_info;
 47 #endif
 48 
 49     struct list_head tasks;
 50     /*
 51      * ptrace_list/ptrace_children forms the list of my children
 52      * that were stolen by a ptracer.
 53      */
 54     struct list_head ptrace_children;
 55     struct list_head ptrace_list;
 56 
 57     struct mm_struct *mm, *active_mm;
 58 
 59 /* task state */
 60     struct linux_binfmt *binfmt;
 61     int exit_state;
 62     int exit_code, exit_signal;
 63     int pdeath_signal;  /*  The signal sent when the parent dies  */
 64     /* ??? */
 65     unsigned int personality;
 66     unsigned did_exec:1;
 67     pid_t pid;
 68     pid_t tgid;
 69 
 70 #ifdef CONFIG_CC_STACKPROTECTOR
 71     /* Canary value for the -fstack-protector gcc feature */
 72     unsigned long stack_canary;
 73 #endif
 74     /* 
 75      * pointers to (original) parent process, youngest child, younger sibling,
 76      * older sibling, respectively.  (p->father can be replaced with 
 77      * p->parent->pid)
 78      */
 79     struct task_struct *real_parent; /* real parent process (when being debugged) */
 80     struct task_struct *parent;    /* parent process */
 81     /*
 82      * children/sibling forms the list of my children plus the
 83      * tasks I'm ptracing.
 84      */
 85     struct list_head children;    /* list of my children */
 86     struct list_head sibling;    /* linkage in my parent's children list */
 87     struct task_struct *group_leader;    /* threadgroup leader */
 88 
 89     /* PID/PID hash table linkage. */
 90     struct pid_link pids[PIDTYPE_MAX];
 91     struct list_head thread_group;
 92 
 93     struct completion *vfork_done;        /* for vfork() */
 94     int __user *set_child_tid;        /* CLONE_CHILD_SETTID */
 95     int __user *clear_child_tid;        /* CLONE_CHILD_CLEARTID */
 96 
 97     unsigned int rt_priority;
 98     cputime_t utime, stime, utimescaled, stimescaled;
 99     cputime_t gtime;
100     cputime_t prev_utime, prev_stime;
101     unsigned long nvcsw, nivcsw; /* context switch counts */
102     struct timespec start_time;         /* monotonic time */
103     struct timespec real_start_time;    /* boot based time */
104 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
105     unsigned long min_flt, maj_flt;
106 
107       cputime_t it_prof_expires, it_virt_expires;
108     unsigned long long it_sched_expires;
109     struct list_head cpu_timers[3];
110 
111 /* process credentials */
112     uid_t uid,euid,suid,fsuid;
113     gid_t gid,egid,sgid,fsgid;
114     struct group_info *group_info;
115     kernel_cap_t   cap_effective, cap_inheritable, cap_permitted;
116     unsigned keep_capabilities:1;
117     struct user_struct *user;
118 #ifdef CONFIG_KEYS
119     struct key *request_key_auth;    /* assumed request_key authority */
120     struct key *thread_keyring;    /* keyring private to this thread */
121     unsigned char jit_keyring;    /* default keyring to attach requested keys to */
122 #endif
123     char comm[TASK_COMM_LEN]; /* executable name excluding path
124                      - access with [gs]et_task_comm (which lock
125                        it with task_lock())
126                      - initialized normally by flush_old_exec */
127 /* file system info */
128     int link_count, total_link_count;
129 #ifdef CONFIG_SYSVIPC
130 /* ipc stuff */
131     struct sysv_sem sysvsem;
132 #endif
133 /* CPU-specific state of this task */
134     struct thread_struct thread;
135 /* filesystem information */
136     struct fs_struct *fs;
137 /* open file information */
138     struct files_struct *files;
139 /* namespaces */
140     struct nsproxy *nsproxy;
141 /* signal handlers */
142     struct signal_struct *signal;
143     struct sighand_struct *sighand;
144 
145     sigset_t blocked, real_blocked;
146     sigset_t saved_sigmask;        /* To be restored with TIF_RESTORE_SIGMASK */
147     struct sigpending pending;
148 
149     unsigned long sas_ss_sp;
150     size_t sas_ss_size;
151     int (*notifier)(void *priv);
152     void *notifier_data;
153     sigset_t *notifier_mask;
154 #ifdef CONFIG_SECURITY
155     void *security;
156 #endif
157     struct audit_context *audit_context;
158     seccomp_t seccomp;
159 
160 /* Thread group tracking */
161        u32 parent_exec_id;
162        u32 self_exec_id;
163 /* Protection of (de-)allocation: mm, files, fs, tty, keyrings */
164     spinlock_t alloc_lock;
165 
166     /* Protection of the PI data structures: */
167     spinlock_t pi_lock;
168 
169 #ifdef CONFIG_RT_MUTEXES
170     /* PI waiters blocked on a rt_mutex held by this task */
171     struct plist_head pi_waiters;
172     /* Deadlock detection and priority inheritance handling */
173     struct rt_mutex_waiter *pi_blocked_on;
174 #endif
175 
176 #ifdef CONFIG_DEBUG_MUTEXES
177     /* mutex deadlock detection */
178     struct mutex_waiter *blocked_on;
179 #endif
180 #ifdef CONFIG_TRACE_IRQFLAGS
181     unsigned int irq_events;
182     int hardirqs_enabled;
183     unsigned long hardirq_enable_ip;
184     unsigned int hardirq_enable_event;
185     unsigned long hardirq_disable_ip;
186     unsigned int hardirq_disable_event;
187     int softirqs_enabled;
188     unsigned long softirq_disable_ip;
189     unsigned int softirq_disable_event;
190     unsigned long softirq_enable_ip;
191     unsigned int softirq_enable_event;
192     int hardirq_context;
193     int softirq_context;
194 #endif
195 #ifdef CONFIG_LOCKDEP
196 # define MAX_LOCK_DEPTH 30UL
197     u64 curr_chain_key;
198     int lockdep_depth;
199     struct held_lock held_locks[MAX_LOCK_DEPTH];
200     unsigned int lockdep_recursion;
201 #endif
202 
203 /* journalling filesystem info */
204     void *journal_info;
205 
206 /* stacked block device info */
207     struct bio *bio_list, **bio_tail;
208 
209 /* VM state */
210     struct reclaim_state *reclaim_state;
211 
212     struct backing_dev_info *backing_dev_info;
213 
214     struct io_context *io_context;
215 
216     unsigned long ptrace_message;
217     siginfo_t *last_siginfo; /* For ptrace use.  */
218 #ifdef CONFIG_TASK_XACCT
219 /* i/o counters(bytes read/written, #syscalls */
220     u64 rchar, wchar, syscr, syscw;
221 #endif
222     struct task_io_accounting ioac;
223 #if defined(CONFIG_TASK_XACCT)
224     u64 acct_rss_mem1;    /* accumulated rss usage */
225     u64 acct_vm_mem1;    /* accumulated virtual memory usage */
226     cputime_t acct_stimexpd;/* stime since last update */
227 #endif
228 #ifdef CONFIG_NUMA
229       struct mempolicy *mempolicy;
230     short il_next;
231 #endif
232 #ifdef CONFIG_CPUSETS
233     nodemask_t mems_allowed;
234     int cpuset_mems_generation;
235     int cpuset_mem_spread_rotor;
236 #endif
237 #ifdef CONFIG_CGROUPS
238     /* Control Group info protected by css_set_lock */
239     struct css_set *cgroups;
240     /* cg_list protected by css_set_lock and tsk->alloc_lock */
241     struct list_head cg_list;
242 #endif
243 #ifdef CONFIG_FUTEX
244     struct robust_list_head __user *robust_list;
245 #ifdef CONFIG_COMPAT
246     struct compat_robust_list_head __user *compat_robust_list;
247 #endif
248     struct list_head pi_state_list;
249     struct futex_pi_state *pi_state_cache;
250 #endif
251     atomic_t fs_excl;    /* holding fs exclusive resources */
252     struct rcu_head rcu;
253 
254     /*
255      * cache last used pipe for splice
256      */
257     struct pipe_inode_info *splice_pipe;
258 #ifdef    CONFIG_TASK_DELAY_ACCT
259     struct task_delay_info *delays;
260 #endif
261 #ifdef CONFIG_FAULT_INJECTION
262     int make_it_fail;
263 #endif
264     struct prop_local_single dirties;
265 };
View Code

2.设计思路

3.数据结构

4.主要函数

猜你喜欢

转载自www.cnblogs.com/yocichen/p/10053219.html
今日推荐