Research task_struct structure

Process description PCB

  task_struct - one kind of PCB, the process described in linux structure is called task_struct.

task_struct content categories:

  • Identifier: a unique identifier description of this process, to distinguish other processes
  • Status: task status, the introduction of the code and exit signals
  • Priority: Relative to other process priority
  • Program Counter: the next instruction in the program to be executed address
  • Memory pointer: the process comprising program codes and data pointer, as well as other processes and shared memory blocks of the pointer
  • Contextual data: the processor's registers when the process execution data to Central Europe
  • Includes a display list file I / O request, the assignment process I / O devices and processes used: I / O status information
  • Accounting information: a processor may comprise the sum of time, using the sum of the clock, the time limit, the account credited like
  • other information

Source:

  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     unsigned int rt_priority;
 18     const struct sched_class *sched_class;
 19     struct sched_entity se;
 20     struct sched_rt_entity rt;
 21 
 22 #ifdef CONFIG_PREEMPT_NOTIFIERS
 23     /* list of struct preempt_notifier: */
 24     struct hlist_head preempt_notifiers;
 25 #endif
 26 
 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 #ifdef CONFIG_BLK_DEV_IO_TRACE
 37     unsigned int btrace_seq;
 38 #endif
 39 
 40     unsigned int policy;
 41     cpumask_t cpus_allowed;
 42 
 43 #ifdef CONFIG_TREE_PREEMPT_RCU
 44     int rcu_read_lock_nesting;
 45     char rcu_read_unlock_special;
 46     struct rcu_node *rcu_blocked_node;
 47     struct list_head rcu_node_entry;
 48 #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
 49 
 50 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
 51     struct sched_info sched_info;
 52 #endif
 53 
 54     struct list_head tasks;
 55     struct plist_node pushable_tasks;
 56 
 57     struct mm_struct *mm, *active_mm;
 58 
 59 /* task state */
 60     int exit_state;
 61     int exit_code, exit_signal;
 62     int pdeath_signal;  /*  The signal sent when the parent dies  */
 63     unsigned int personality;
 64     unsigned did_exec:1;
 65     unsigned in_execve:1;   /* Tell the LSMs that the process is doing an
 66                  * execve */
 67     unsigned in_iowait:1;
 68 
 69 
 70     /* Revert to default priority/policy when forking */
 71     unsigned sched_reset_on_fork:1;
 72 
 73     pid_t pid;
 74     pid_t tgid;
 75 
 76 #ifdef CONFIG_CC_STACKPROTECTOR
 77     /* Canary value for the -fstack-protector gcc feature */
 78     unsigned long stack_canary;
 79 #endif
 80 
 81     /* 
 82      * pointers to (original) parent process, youngest child, younger sibling,
 83      * older sibling, respectively.  (p->father can be replaced with 
 84      * p->real_parent->pid)
 85      */
 86     struct task_struct *real_parent; /* real parent process */
 87     struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */
 88     /*
 89      * children/sibling forms the list of my natural children
 90      */
 91     struct list_head children;  /* list of my children */
 92     struct list_head sibling;   /* linkage in my parent's children list */
 93     struct task_struct *group_leader;   /* threadgroup leader */
 94 
 95     /*
 96      * ptraced is the list of tasks this task is using ptrace on.
 97      * This includes both natural children and PTRACE_ATTACH targets.
 98      * p->ptrace_entry is p's link on the p->parent->ptraced list.
 99      */
100     struct list_head ptraced;
101     struct list_head ptrace_entry;
102 
103     /*
104      * This is the tracer handle for the ptrace BTS extension.
105      * This field actually belongs to the ptracer task.
106      */
107     struct bts_context *bts;
108 
109     /* PID/PID hash table linkage. */
110     struct pid_link pids[PIDTYPE_MAX];
111     struct list_head thread_group;
112 
113     struct completion *vfork_done;      /* for vfork() */
114     int __user *set_child_tid;      /* CLONE_CHILD_SETTID */
115     int __user *clear_child_tid;        /* CLONE_CHILD_CLEARTID */
116 
117     cputime_t utime, stime, utimescaled, stimescaled;
118     cputime_t gtime;
119     cputime_t prev_utime, prev_stime;
120     unsigned long nvcsw, nivcsw; /* context switch counts */
121     struct timespec start_time;         /* monotonic time */
122     struct timespec real_start_time;    /* boot based time */
123 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
124     unsigned long min_flt, maj_flt;
125 
126     struct task_cputime cputime_expires;
127     struct list_head cpu_timers[3];
128 
129 /* process credentials */
130     const struct cred *real_cred;   /* objective and real subjective task
131                      * credentials (COW) */
132     const struct cred *cred;    /* effective (overridable) subjective task
133                      * credentials (COW) */
134     struct mutex cred_guard_mutex;  /* guard against foreign influences on
135                      * credential calculations
136                      * (notably. ptrace) */
137     struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */
138 
139     char comm[TASK_COMM_LEN]; /* executable name excluding path
140                      - access with [gs]et_task_comm (which lock
141                        it with task_lock())
142                      - initialized normally by flush_old_exec */
143 /* file system info */
144     int link_count, total_link_count;
145 #ifdef CONFIG_SYSVIPC
146 /* ipc stuff */
147     struct sysv_sem sysvsem;
148 #endif
149 #ifdef CONFIG_DETECT_HUNG_TASK
150 /* hung task detection */
151     unsigned long last_switch_count;
152 #endif
153 /* CPU-specific state of this task */
154     struct thread_struct thread;
155 /* filesystem information */
156     struct fs_struct *fs;
157 /* open file information */
158     struct files_struct *files;
159 /* namespaces */
160     struct nsproxy *nsproxy;
161 /* signal handlers */
162     struct signal_struct *signal;
163     struct sighand_struct *sighand;
164 
165     sigset_t blocked, real_blocked;
166     sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
167     struct sigpending pending;
168 
169     unsigned long sas_ss_sp;
170     size_t sas_ss_size;
171     int (*notifier)(void *priv);
172     void *notifier_data;
173     sigset_t *notifier_mask;
174     struct audit_context *audit_context;
175 #ifdef CONFIG_AUDITSYSCALL
176     uid_t loginuid;
177     unsigned int sessionid;
178 #endif
179     seccomp_t seccomp;
180 
181 /* Thread group tracking */
182     u32 parent_exec_id;
183     u32 self_exec_id;
184 /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
185  * mempolicy */
186     spinlock_t alloc_lock;
187 
188 #ifdef CONFIG_GENERIC_HARDIRQS
189     /* IRQ handler threads */
190     struct irqaction *irqaction;
191 #endif
192 
193     /* Protection of the PI data structures: */
194     spinlock_t pi_lock;
195 
196 #ifdef CONFIG_RT_MUTEXES
197     /* PI waiters blocked on a rt_mutex held by this task */
198     struct plist_head pi_waiters;
199     /* Deadlock detection and priority inheritance handling */
200     struct rt_mutex_waiter *pi_blocked_on;
201 #endif
202 
203 #ifdef CONFIG_DEBUG_MUTEXES
204     /* mutex deadlock detection */
205     struct mutex_waiter *blocked_on;
206 #endif
207 #ifdef CONFIG_TRACE_IRQFLAGS
208     unsigned int irq_events;
209     int hardirqs_enabled;
210     unsigned long hardirq_enable_ip;
211     unsigned int hardirq_enable_event;
212     unsigned long hardirq_disable_ip;
213     unsigned int hardirq_disable_event;
214     int softirqs_enabled;
215     unsigned long softirq_disable_ip;
216     unsigned int softirq_disable_event;
217     unsigned long softirq_enable_ip;
218     unsigned int softirq_enable_event;
219     int hardirq_context;
220     int softirq_context;
221 #endif
222 #ifdef CONFIG_LOCKDEP
223 # define MAX_LOCK_DEPTH 48UL
224     u64 curr_chain_key;
225     int lockdep_depth;
226     unsigned int lockdep_recursion;
227     struct held_lock held_locks[MAX_LOCK_DEPTH];
228     gfp_t lockdep_reclaim_gfp;
229 #endif
230 
231 /* journalling filesystem info */
232     void *journal_info;
233 
234 /* stacked block device info */
235     struct bio *bio_list, **bio_tail;
236 
237 /* VM state */
238     struct reclaim_state *reclaim_state;
239 
240     struct backing_dev_info *backing_dev_info;
241 
242     struct io_context *io_context;
243 
244     unsigned long ptrace_message;
245     siginfo_t *last_siginfo; /* For ptrace use.  */
246     struct task_io_accounting ioac;
247 #if defined(CONFIG_TASK_XACCT)
248     u64 acct_rss_mem1;  /* accumulated rss usage */
249     u64 acct_vm_mem1;   /* accumulated virtual memory usage */
250     cputime_t acct_timexpd; /* stime + utime since last update */
251 #endif
252 #ifdef CONFIG_CPUSETS
253     nodemask_t mems_allowed;    /* Protected by alloc_lock */
254     int cpuset_mem_spread_rotor;
255 #endif
256 #ifdef CONFIG_CGROUPS
257     /* Control Group info protected by css_set_lock */
258     struct css_set *cgroups;
259     /* cg_list protected by css_set_lock and tsk->alloc_lock */
260     struct list_head cg_list;
261 #endif
262 #ifdef CONFIG_FUTEX
263     struct robust_list_head __user *robust_list;
264 #ifdef CONFIG_COMPAT
265     struct compat_robust_list_head __user *compat_robust_list;
266 #endif
267     struct list_head pi_state_list;
268     struct futex_pi_state *pi_state_cache;
269 #endif
270 #ifdef CONFIG_PERF_EVENTS
271     struct perf_event_context *perf_event_ctxp;
272     struct mutex perf_event_mutex;
273     struct list_head perf_event_list;
274 #endif
275 #ifdef CONFIG_NUMA
276     struct mempolicy *mempolicy;    /* Protected by alloc_lock */
277     short il_next;
278 #endif
279     atomic_t fs_excl;   /* holding fs exclusive resources */
280     struct rcu_head rcu;
281 
282     /*
283      * cache last used pipe for splice
284      */
285     struct pipe_inode_info *splice_pipe;
286 #ifdef  CONFIG_TASK_DELAY_ACCT
287     struct task_delay_info *delays;
288 #endif
289 #ifdef CONFIG_FAULT_INJECTION
290     int make_it_fail;
291 #endif
292     struct prop_local_single dirties;
293 #ifdef CONFIG_LATENCYTOP
294     int latency_record_count;
295     struct latency_record latency_record[LT_SAVECOUNT];
296 #endif
297     /*
298      * time slack values; these are used to round up poll() and
299      * select() etc timeout values. These are in nanoseconds.
300      */
301     unsigned long timer_slack_ns;
302     unsigned long default_timer_slack_ns;
303 
304     struct list_head    *scm_work_list;
305 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
306     /* Index of current stored adress in ret_stack */
307     int curr_ret_stack;
308     /* Stack of return addresses for return function tracing */
309     struct ftrace_ret_stack *ret_stack;
310     /* time stamp for last schedule */
311     unsigned long long ftrace_timestamp;
312     /*
313      * Number of functions that haven't been traced
314      * because of depth overrun.
315      */
316     atomic_t trace_overrun;
317     /* Pause for the tracing */
318     atomic_t tracing_graph_pause;
319 #endif
320 #ifdef CONFIG_TRACING
321     /* state flags for use by tracers */
322     unsigned long trace;
323     /* bitmask of trace recursion */
324     unsigned long trace_recursion;
325 #endif /* CONFIG_TRACING */
326     unsigned long stack_start;
327 };

Resolve task_struct structure

Identify (1) the process PID (process identifier):

  pid_t pid; // unique identification process
  pid_t tgid; pid value of the leading members of the threads of a thread group //
  32-bit unsigned integer data. However, to take maximum 32767. An identifier that represents each process. Excuse kernel is also provided to the user program, user program by pid procedures. Because of the introduction of Unix also introduced the concept of thread group. Called: tgid. All threads in a thread group and pid first use a lightweight thread of the thread group, the presence tgid members. When the process is no thread, tgid = pid; When multiple threads, tgid id represents the main thread, and each thread represents pid own id.

State (2) the process of volatile long state

  Possible values ​​state that:

    #define TASK_RUNNING 0 // process is either executing or ready to execute

    #define TASK_INTERRUPTIBLE 1 // can interrupt sleep, you can wake up by a signal

    #define TASK_UNINTERRUPTIBLE 2 // uninterruptible sleep, not to wake up the signal

    #define __TASK_STOPPED 4 // process stops execution

    #define __TASK_TRACED 8 // process being tracked

    /* in tsk->exit_state */

    #define EXIT_ZOMBIE 16 // zombie state process, that the process is terminated, but the parent process has not yet acquired its termination information, such information is not executing the process and so on.  

    #Define EXIT_DEAD 32 // final state of the process, the process of death

    /* in tsk->state again */

    #define TASK_DEAD 64 //死亡

    #define TASK_WAKEKILL 128 // wake up and kill processes

    #define TASK_WAKING 256 // wake-up process

Priority long priority (3) process

  Priority values ​​are given per the process times (in jiffies) acquiring the CPU can be used. Priority may be changed by the system sys_setpriorty (in kernel / sys.c in).

  Program counter: address of the next instruction in the program to be executed.
  Memory pointer: the process comprising program codes and data pointer, and a pointer to the memory block is shared with other processes.
  Context data: data register of the processor when the process is performed. 
   I / O status information: I includes a display / O requests assigned to process I / O device (such as a tape drive), and a list of files to be used by the process. 
  Audit information: a processor may comprise the sum of the time, use the total number of clocks, time limits, auditing number. 
 
(4) process scheduling information

  It represents the current process or a process to allow time to run, until the operation end time slice process, CPU execution another process to run out from the run queue.

  need_resched: scheduling flag
  Nice: static priority
  Counter: dynamic priority; maximum value of Counter process will be elected at the run_queue re-scheduling process. Also represents a time slice of the process, the operation continues to decline.
  Policy: values are assigned scheduling policy starts running
  rt_priority: real-time priority
information (IPC: Inter_Process Communication) (5 ) Process Communication

  unsigned long signal: process the received signals. Each signal represents one of 32 species. Set valid.
  unsigned long blocked: the process can accept a bit mask signal. It represents a mask set, the reset means no shield.
  Spinlock_t sigmask_lock: spin lock signal mask
  Long blocked: signal mask
  Struct sem_undo * semundo: Cancel avoid deadlock is provided on the semaphore operation
  Struct sem_queue * semsleeping: queue associated with the semaphore operation
  struct signal_struct * sig: signal processing function
(6) process information

  Linux are multi-process, multi-process and the relationship between the father-son relationship in the process may be, brothers.
  In addition to the ancestor process, other processes have a parent process to execute the program to create a child process by folk. In addition to that outside their pid, the vast majority of information is a copy of the child's parent process information. And the parent process child process holding the reins, that is when the child process is created out of the parent, and the parent can also send commands to kill the child process.
  

(7) time information

  Start_time: process creation time
  Per_cpu_utime: in the implementation process spent in user mode time.
  Pre_cpu_stime: consuming process execution time on the system state.
  ITIMER_REAL: real-time timer, regardless of whether the process is running, all updated in real time.
  ITIMER_VIRTUAL: Virtual timer, only the process run only updated when the user mode.
  ITIMER_PROF: Overview timer, the process of updating the user state and the state system is running in.  
(8) File Information

  File opening and closing of operating resources are, in Linux task_struct structure has two stores both information.

  Sruct fs_struct * fs: executable image process where the file system, there are two index points, called root and pwd, point to correspond to the root directory of the current directory.

  Struct files_struct * files: the process open files

(8) address space / virtual memory information

  Each process has its own piece of virtual memory space, is represented by mm_struct, mm_struct used two pointer indicates a period of virtual address space, and then mapped to the final table on page through real physical memory.

(9) management information page

  Int swappable: whether the process memory page can be swapped out.
  Unsigned long min_flat, maj_flt, nswap: cumulative process swapped out, for the number of pages.
  Unsigned long cmin_flat, cmaj_flt, cnswap: this process as the ancestor process, a total swap out all of its levels of child processes, change the number of pages.
(10) symmetrically to the information processor

  Int has_cpu: whether the current process has the CPU
  Int Processor: process is currently being used by the CPU
  Int lock_depth: context switching depth kernel lock
(11) context information:  

  struct desc_struct * ldt: Process pointer on CPU local segment descriptor table in the storage management.
  struct thread_struct tss: Task State Segment. Intel's interaction with TSS, TSS currently running save tss PCB's, tss newly selected process is stored in the TSS.
(12) the amount of signal data members

  struct sem_undo * semundo: process every time a semaphore operation will generate an undo operation. Sem_undo stored in the structure, the final time at the end of the abnormal termination process, the members of semadj sem_undo will point to an array that represents the amount of each member before each of undo.
  truct sem_queue * semsleeping: semaphore operation process clogging, the queue process will be sent on the sem_queue semsleeping semaphore instruction.
(13) Process queue pointer

  struct task_struct * next_task, * prev_task: All processes have their own PCB. And each PCB will be strung together, forming a doubly linked list. Which represents next_task and prev_task on or next to a PCB, i.e., the front and rear pointers. Process list head and tail are 0 process.
  struct task_struct * next_run, * prev_run: run_queue produced by the action of the process, a process on a point or a next run, both the head and tail of the list 0 process.
  struct task_struct * p_opptr: original parent (ancestor process)
  struct task_struct * p_pptr: parent process  
  struct task_struct * p_cptr: child process
  struct task_struct * p_ysptr: Brother process
  struct task_struct * p_osptr: Brother processes
  above are pointing to the original parent (original parent ), the pointer parent process (parent), the child process (youngest child) process and the new old brother (younger sibling, older sibling) of.  

  current: pointer currently running processes.
  struct task_struct init_task: 0 process of PCB, the process with the root =, is always INIT_TASK.
  char comm [16]: filename of the executable process being executed.
  int errno: Error number of the process last error. 0 indicates no error.

Guess you like

Origin www.cnblogs.com/cuckoo-/p/10966158.html