The Task_struct of kernel learning

Source: Internet
Author: User
Tags data structures file system memory usage mutex thread linux

Procedures and processes:

A program is an executable mirror of a series of code and data stored on a disk, and is a static entity;

Process is an executing program, it is a dynamic entity, in addition to include instruction segments, data segments, such as static data (data can be dynamic changes), but also include current state information, such as temporary data stack information, current processor register information and other dynamic information. These dynamic information is often referred to as the process context.

From the kernel point of view, the process is the operating system allocated memory, CPU time slices, such as the smallest unit of resources. Most of the data and information used in it are dynamically changing. In the Linux kernel, process contexts are usually described by task_struct, and process switching is responsible for saving the context of the current process, restoring the context of the appropriate process to the CPU and registers.

Processes and Threads:

With the development of computer industry, the application of computer is more and more wide, the range of computer to solve from processor-intensive scientific computing to IO-intensive user interactive program. In order to solve the increasingly complex problems. People put forward the idea of divide and conquer (divide and Comquer), which is to propose the process. With the development of computer and the research of this technology, it is found that the switching between processes brings about considerable overhead (overload), and the concept of thread is proposed. A thread is a further abstraction of the process. A process consists of two parts: a collection of threads and a collection of resources. A thread is a dynamic object in a process, a set of dynamic instruction streams. All threads in the process will share resources in the process, but each thread has separate program counters, stacks, and registers.

Linux threads, processes are described with struct task_struct. The process descriptor task_struct is used to characterize the state attributes of a process and is the only means by which the kernel operates and maintains the state of the process, defined in the Linux 2.6.xx/include/linux/sched.h. This structure is quite large.

Truct task_struct {/* This is the run-time state of the process-1 represents not operational, 0 is operational, and the >0 representative has stopped/volatile long;  
    /*-1 unrunnable, 0 runnable, >0 stopped/void *stack;  
    atomic_t usage; unsigned int flags;  
      
    /* per process flags, defined below * * unsigned int ptrace;     int lock_depth;  
/* BKL Lock depth */#ifdef CONFIG_SMP #ifdef __ARCH_WANT_UNLOCKED_CTXSW int oncpu;  
    #endif #endif int Prio, Static_prio, Normal_prio;  
    /* Indicates the running priority of this process * * unsigned int rt_priority;  
    const struct Sched_class *sched_class;  
    struct sched_entity se;  
      
struct sched_rt_entity RT;  
#ifdef config_preempt_notifiers/* List of struct preempt_notifier: * * struct hlist_head preempt_notifiers; #endif/* View this column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/* Fpu_counter contains the n Umber of consecutive context switches * This FPU is used. 
     If This is over a threshold, the lazy FPU* Saving becomes unlazy to save the trap. This is a unsigned char * so, 256 times the counter wraps and the behavior turns * lazy again;  
This to deal with Bursty apps is only the use of FPU for * A short time */unsigned char fpu_counter;  
#ifdef config_blk_dev_io_trace unsigned int btrace_seq;  
    #endif unsigned int policy;  
      
cpumask_t cpus_allowed;  
    #ifdef CONFIG_PREEMPT_RCU int rcu_read_lock_nesting;  
    Char rcu_read_unlock_special;  
struct List_head rcu_node_entry;  
#endif/* #ifdef CONFIG_PREEMPT_RCU * * #ifdef config_tree_preempt_rcu struct Rcu_node *rcu_blocked_node; #endif/* #ifdef CONFIG_TREE_PREEMPT_RCU * * #if defined (config_schedstats) | |  
Defined (CONFIG_TASK_DELAY_ACCT) struct sched_info sched_info;  
    #endif struct list_head tasks;  
    struct Plist_node pushable_tasks;  
* * This structure records the relevant situation of process memory usage * * struct mm_struct *mm, *active_mm; #if defined (split_rss_counting) struct Task_rss_stat rss_stat;  
    #endif/* Task state///////* The status of the process exit */int exit_state;  
    int Exit_code, exit_signal;  int pdeath_signal; /* The signal sent when the parent dies * * *???  
    * * unsigned int personality;  
    unsigned did_exec:1;   unsigned in_execve:1;  
      
      
    * Tell the Lsms this process is doing a * execve * * unsigned in_iowait:1;  
      
    /* Revert to the default priority/policy when forking * * unsigned sched_reset_on_fork:1;  
    /* Process number */pid_t pid;  
      
/* Group Process Number * * pid_t tgid; #ifdef config_cc_stackprotector/* Canary value for the-fstack-protector gcc feature/unsigned long Stack_can  
ary #endif * * pointers to (original) parent process, youngest child, younger sibling, * older Sibl  ING, respectively. (P->father can be replaced with * p->real_parent->pid) *///* Create the process's parent process * * struct TASK_STRUCT *real_parent; /* Real Parent process//*parent is the process now the parent process, there may be "stepfather" * * * struct task_struct *parent; 
     /* recipient of SIGCHLD, WAIT4 () Reports/* Children/sibling forms the list of my natural children * *  /* Here children refers to the process of the child's linked list, you can get all the children's process descriptor/struct list_head children;   /* List of my children//*sibling The process Brother's list, which is the list of all the children of his father/struct list_head sibling; /* Linkage in my parent's children list///* This is the main thread of the process descriptor, Linux does not implement the threads of the relevant structure, but only a process to replace the thread, and then do some special processing/* * struct   Task_struct *group_leader; 
     /* Threadgroup leader * * * ptraced is the list of tasks this task was using Ptrace on. 
     * This includes both natural children and ptrace_attach targets. 
     * P->ptrace_entry is P ' s link on the p->parent->ptraced list.  
    * * struct list_head ptraced;  
      
    struct List_head ptrace_entry; /* pid/pid hash table linkage.  
    * * struct pid_link Pids[pidtype_max]; /* The link for the thread of the process */sTruct List_head Thread_group;      struct completion *vfork_done;      /* for vfork () */int __user *set_child_tid;        /* Clone_child_settid */int __user *clear_child_tid; /* Clone_child_cleartid * * This is the process using CPU time information, Utime is in the user state execution time, Stime is in the kernel state of execution time * * cputime_t utime, stime, u  
    Timescaled, stimescaled;  
cputime_t Gtime;  
#ifndef config_virt_cpu_accounting cputime_t prev_utime, Prev_stime; #endif unsigned long NVCSW, NIVCSW;         /* Context switch counts/* Start time, only the time benchmark is not the same * * struct TIMESPEC start_time;    /* monotonic time * * struct TIMESPEC real_start_time;
    /* Boot based time///* mm fault and swap info:this can arguably be seen as either mm-specific or thread-specific * *  
      
    unsigned long min_flt, Maj_flt;  
    struct Task_cputime cputime_expires;  
      
struct List_head cpu_timers[3]; /* Process credentials/const struct CRED __rcu *real_cred; 
 /* Objective and real subjective task                    * Credentials (COW)/const struct CRED __rcu *cred; /* Effective (overridable) subjective task * credentials (COW)/struct cred on_keyring; /* for Keyctl_session_to_parent *///* To save the process name of the character array/char Comm[task_comm_len]; 
                       /* executable name excluding path-access with [Gs]et_task_comm (which lock It with Task_lock ())-initialized normally by setup_new_exec///* File system Info//* FileSystem information count  
* * int link_count, total_link_count;  
#ifdef CONFIG_SYSVIPC/* IPC Stuff * * struct SYSV_SEM sysvsem;  
#endif #ifdef config_detect_hung_task/* HUNG TASK detection/unsigned long last_switch_count;  
#endif/* cpu-specific State of the This task *//////////* The status of the process under a specific CPU/struct thread_struct thread;  
/* FileSystem Information * * * file system Related information structure * * struct fs_struct *fs; /* Open File Information *////////////////////////////will be common to * * struct files_struct *files;  
/* Namespaces * * struct nsproxy *nsproxy;  
    /* signal handlers/////////////struct signal_struct *signal;  
      
    struct Sighand_struct *sighand;  
    sigset_t blocked, real_blocked; sigset_t Saved_sigmask;  
      
    /* restored if Set_restore_sigmask () was used * * struct sigpending pending;  
    unsigned long sas_ss_sp;  
    size_t Sas_ss_size;  
    Int (*notifier) (void *priv);  
    void *notifier_data;  
    sigset_t *notifier_mask;  
struct Audit_context *audit_context;  
    #ifdef Config_auditsyscall uid_t Loginuid;  
unsigned int sessionid;  
      
#endif seccomp_t Seccomp;  
    /* THREAD Group Tracking * * U32 parent_exec_id;  
U32 self_exec_id;  
      
/* Protection of (de-) allocation:mm, files, FS, TTY, keyrings, mems_allowed, * mempolicy/spinlock_t;  
#ifdef CONFIG_GENERIC_HARDIRQS/* IRQ Handler threads * * struct irqaction *irqaction;  
      
#endif    /* Protection of the PI data structures: * * raw_spinlock_t pi_lock;  
    #ifdef config_rt_mutexes/* PI Waiters blocked on a rt_mutex held from this task * * struct plist_head;  
/* deadlock detection and priority inheritance handling * struct rt_mutex_waiter;  
#endif #ifdef config_debug_mutexes/* Mutex deadlock detection * * struct mutex_waiter *blocked_on;  
    #endif #ifdef config_trace_irqflags unsigned int irq_events;  
    unsigned long hardirq_enable_ip;  
    unsigned long hardirq_disable_ip;  
    unsigned int hardirq_enable_event;  
    unsigned int hardirq_disable_event;  
    int hardirqs_enabled;  
    int hardirq_context;  
    unsigned long softirq_disable_ip;  
    unsigned long softirq_enable_ip;  
    unsigned int softirq_disable_event;  
    unsigned int softirq_enable_event;  
    int softirqs_enabled;  
int softirq_context; #endif #ifdef CONFIG_LOCKDEP # define MAX_LOCK_DEPTH 48UL U64 Curr_chain_key;  
    int lockdep_depth;  
    unsigned int lockdep_recursion;  
    struct Held_lock held_locks[max_lock_depth];  
gfp_t LOCKDEP_RECLAIM_GFP;  
      
#endif/* journalling filesystem Info */void *journal_info;  
      
/* Stacked block Device info */struct bio_list *bio_list;  
      
    /* VM state */struct reclaim_state *reclaim_state;  
      
    struct Backing_dev_info *backing_dev_info;  
      
    struct Io_context *io_context;  
    unsigned long ptrace_message; siginfo_t *last_siginfo;  /* for ptrace use.  
* * struct task_io_accounting IOAC;  #if defined (CONFIG_TASK_XACCT) U64 acct_rss_mem1;   /* Accumulated RSS Usage * * U64 acct_vm_mem1; /* Accumulated Virtual memory Usage * * cputime_t ACCT_TIMEXPD;    /* stime + utime since last update * * * #endif #ifdef config_cpusets nodemask_t mems_allowed;  
    /* Protected by Alloc_lock */int mems_allowed_change_disable; int Cpuset_mem_spread_rOtor;  
int cpuset_slab_spread_rotor;   
    #endif #ifdef config_cgroups/* Control Group Info protected by Css_set_lock * * struct css_set __rcu;  
/* Cg_list protected by Css_set_lock and Tsk->alloc_lock/struct list_head;  
#endif #ifdef config_futex struct robust_list_head __user *robust_list;  
#ifdef config_compat struct compat_robust_list_head __user *compat_robust_list;  
    #endif struct List_head pi_state_list;  
struct Futex_pi_state *pi_state_cache;  
    #endif #ifdef config_perf_events struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];  
    struct Mutex Perf_event_mutex;  
struct List_head perf_event_list;    #endif #ifdef config_numa struct mempolicy *mempolicy;  
/* Protected by Alloc_lock * * Short il_next;   #endif atomic_t fs_excl;  
      
    * Holding FS Exclusive resources * * struct rcu_head RCU; * * Cache last used pipe for splice */STRuct Pipe_inode_info *splice_pipe;  
#ifdef CONFIG_TASK_DELAY_ACCT struct Task_delay_info *delays;  
#endif #ifdef config_fault_injection int make_it_fail;  
#endif struct Prop_local_single dirties;  
    #ifdef Config_latencytop int latency_record_count;  
struct Latency_record Latency_record[lt_savecount]; #endif * * Time slack values; These are used to round up poll () and * Select () etc timeout values. 
     These are are in nanoseconds.  
    * * These are slack time values to specify the timeout for select () and poll () in nanoseconds nanoseconds/unsigned long Timer_slack_ns;  
      
    unsigned long Default_timer_slack_ns;  
struct List_head *scm_work_list;  
    #ifdef config_function_graph_tracer/* Index of current stored address in Ret_stack */int curr_ret_stack;  
    /* Stack of return addresses to return function tracing */struct ftrace_ret_stack *ret_stack;  
    /* time stamp to last schedule/unsigned long long ftrace_timestamp; 
  /*   * Number of functions that haven ' t been traced * because of depth overrun.  
    * * atomic_t Trace_overrun;  
/* Pause for the tracing * * atomic_t tracing_graph_pause;  
    #endif #ifdef config_tracing/* State flags for use by tracers */unsigned long trace;  
/* Bitmask of trace recursion */unsigned long trace_recursion; #endif/* config_tracing/#ifdef CONFIG_CGROUP_MEM_RES_CTLR/* MEMCG uses this to do batch job * * struct MEMCG_B   atch_info {int do_batch; /* Incremented when batch uncharge started * * struct mem_cgroup;        /* Target MEMCG of Uncharge * * unsigned long bytes; /* uncharged Usage * * unsigned long memsw_bytes;  
/* uncharged mem+swap usage * * MEMCG_BATCH; #endif};

It's just a few simple comments, followed by an emphasis on some important structures, and their related operations and uses.

AUTHOR:CSDN Blog muge0913

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.