Notes on task_struct structure of process control block under Linux

Task_struct structure annotation

  ==========================

  long state The running state of the task (-1 is not runnable, 0 is runnable (ready), >0 is stopped).

  long counter Task running time count (decrement) (number of ticks), running time slice.

  long priority Run priority number. When the task starts to run, counter = priority, the larger the task, the longer it runs.

  long signal signal. It is a bitmap, each bit represents a signal, the signal value = bit offset value + 1.

  struct sigaction sigaction[32] The signal execution attribute structure, corresponding to the operation to be performed by the signal and the flag information.

  long blocked Process signal shielding code (corresponding to signal bitmap).

  ————————–

  int exit_code The exit code of the task execution stop, which will be taken by the parent process.

  unsigned long start_code code segment address.

  unsigned long end_code code length (number of bytes).

  unsigned long end_data Code length + data length (number of bytes).

  unsigned long brk total length (number of bytes).

  unsigned long start_stack Stack segment address.

  long pid Process identification number (process number).

  long father parent process number.

  long pgrp parent process group number.

  long session session number.

  long leader The leader of the session.

  unsigned short uid User identification number (user id).

  unsigned short euid effective user id.

  The user id saved by unsigned short suid.

  unsigned short gid Group identification number (group id).

  unsigned short egid Effective group id.

  unsigned short sgid The saved group id.

  long alarm Alarm timing value (number of ticks).

  long utime User mode running time (number of ticks).

  long stime System state running time (number of ticks).

  long cutime User mode running time of the child process.

  long cstime The running time of the child process in the system state.

  long start_time The time when the process started running.

  unsigned short used_math flag: whether the coprocessor is used.

  ————————–

  The int tty process uses the tty's sub-device number. -1 means not used.

  The unsigned short umask file creates the attribute mask bit.

  struct m_inode * pwd The i-node structure of the current working directory.

  struct m_inode * root i-node structure of the root directory.

  struct m_inode * executable executable file i-node structure.

  unsigned long close_on_exec closes the file handle bitmap flag when executing. (See include/fcntl.h)

  struct file * filp[NR_OPEN] The file table structure used by the process.

  ————————–

  struct desc_struct ldt[3] The local table descriptor of this task. 0-empty, 1-code segment cs, 2-data and stack segment ds&ss.

  ————————–

  struct tss_struct tss The task status segment information structure of this process.

  ==========================

1. Schedule data members

  (1) volatile long states;

  Indicates the current state of the process:

  * TASK_RUNNING: Processes that are running or ready to run in the ready queue run-queue, actually participate in process scheduling.

  *
TASK_INTERRUPTIBLE: The process in the waiting queue, wake up when the resource becomes available, or other processes can enter the ready queue
run-queue after being awakened by a signal or timing interrupt .

  *
TASK_UNINTERRUPTIBLE: The process in the waiting queue, wake up when the resource becomes available, and cannot be awakened by other processes through signal or timing interrupt.

  * TASK_ZOMBIE: A state (zombie state) that represents the end of the process but has not yet died. At this point, the process has finished running and released most of the resources, but the process control block has not been released yet.

  *TASK_STOPPED: The process is suspended and can only be awakened by signals from other processes. There are two reasons for this state, either in response to receiving SIGSTOP, SIGSTP,
SIGTTIN, or SIGTTOU signals, or temporarily handing over the CPU to the control process under the control of the ptrace system call of other processes.

  * TASK_SWAPPING: The process whose page is swapped out of memory.

  (2) unsigned long flags;

  Process flag:

  *PF_ALIGNWARN prints "alignment" warning message.

  *PF_PTRACED is monitored by the ptrace system call.

  *PF_TRACESYS is tracking.

  *PF_FORKNOEXEC process has just been created, but it has not been executed yet.

  *PF_SUPERPRIV Super user privileges.

  *PF_DUMPCORE dumped core。

  *PF_SIGNALED process is killed by signal (signal).

  *PF_STARTING The process is being created.

  *PF_EXITING process started to shut down.

  *PF_USEDFPU The process uses FPU (SMP only).

  *PF_DTRACE delayed trace (used on m68k)。

  (3) long priority;

  Process priority.
The value of Priority gives the time (in jiffies) that the process can use each time it acquires the CPU. The priority can be changed by the system call sys_setpriorty (in
kernel/sys.c).

  (4) unsigned long rt_priority;

  rt_priority
gives the priority of the real-time process, and rt_priority+1000 gives the time that the process can use each time it acquires the CPU (also counted by jiffies). Priority real-time processes via system
calls sys_sched_setscheduler () change (see kernel / sched.c).

  (5) long counter;

  In
that the process is also currently running How long round-robin scheduling. Began to run in the process is to be assigned priority value, a tick (clock interrupt) is decremented by 1 after every other, causing a new round of adjustment when reduced to 0
degrees. The rescheduling will select the ready process with the largest counter value from the run_queue queue and give the CPU usage rights, so the counter functions as the dynamic priority of the process
(priority is a static priority).

  (6) unsigned long policy;

  The process scheduling strategy of this process can be changed through the system call sys_sched_setscheduler() (see kernel/sched.c). The scheduling strategies are:

  *SCHED_OTHER 0 Non-real-time process, round robin based on priority.

  *SCHED_FIFO 1 Real-time process, using first-in first-out algorithm.

  *SCHED_RR 2 Real-time process, using priority-based rotation method.

2. Signal Processing

  (1) unsigned long signal;

  The signal received by the process. Each bit represents a kind of signal, a total of 32 kinds. Set valid.

  (2) unsigned long blocked;

  The bit mask of the signal that the process can accept. Setting means shielding, and reset means not shielding.

  (3) struct signal_struct *sig;

  Due
to signal and blocked all 32 variables, Linux can only accept up to 32 signals. For each signal, the processes may be selected by the attribute sig PCB processing using custom
default handler function, or system. The structure for assigning various information processing functions is defined in include/linux/sched.h. Signal inspection arrangements at the end of system calls to
and "slow type" After the interrupt service routine (IRQ # _interrupt (), see section 9.5 "Starting the kernel").

3. Process queue pointer

  (1) struct task_struct *next_task,*prev_task;

  All processes (in the form of PCB) form a doubly linked list. The sum of next_task is the front and back pointers of the linked list. The head and tail of the linked list are init_task (that is, process 0).

  (2) struct task_struct *next_run,*prev_run;

  A two-way circular linked list composed of processes that are running or can be run and whose process status is TASK_RUNNING, that is, the run_queue ready queue. The forward and backward pointers of the linked list use next_run and prev_run, and the head and end of the linked list are both init_task (that is, process 0).

  (3) struct task_struct *p_opptr,*p_pptr;和struct task_struct
*p_cptr,*p_ysptr,*p_osptr;

  The above are pointers to the original parent, parent, youngest
child, and younger sibling (older sibling) respectively.

4. Process ID

  (1) unsigned short uid,gid;

  Uid and gid are the user ID and user group ID of the running process.

  (2) int groups[NGROUPS];

  Like most modern UNIX operating systems, Linux allows a process to have a set of user group numbers at the same time. When the process accesses the file, these group numbers can be used for legality checking.

  (3) unsigned short euid,egid;

  euid
and egid are also called effective uid and gid. For system security considerations, the legitimacy of euid and egid should be checked when running the program. Generally, uid is equal to euid and gid is equal to
egid. Sometimes, the system will give general users temporarily the uid and gid of root (euid and egid as the user process) for easy operation.

  (4) unsigned short fsuid,fsgid;

  The fsuid
and fsgid are called the uid and gid of the file system, which are used to check the legality of the file system operation and are unique identification types of Linux. They are generally the same as euid and egid, but in the
NFS file system, the NFS server needs to access files as a special process. At this time, only the fsuid and fsgid of the client process are modified.

  (5) unsigned short suid , sgid;

  The suid and sgid are introduced according to the POSIX standard and are used to retain the real uid and gid when the system call changes the uid and gid.

  (6) int pid,pgrp,session;

  Process identification number, process organization number and session identification number, related system calls (see program kernel/sys.c) include sys_setpgid, sys_getpgid,
sys_setpgrp, sys_getpgrp, sys_getsid, and sys_setsid.

  (7) int leader;

  Whether it is the supervisor of the session, boolean quantity.

5. Time data members

  (1) unsigned long timeout;

  Used for software timing, indicating how often the process will be re-awakened. Use tick as the unit.

  (2) unsigned long it_real_value,it_real_iner;

  Used for itimer (interval
timer) software timing. Using jiffies as the unit, each tick will signal SIGALRM to the process when it_real_value is reduced to 0, and reset the initial value. The initial value is
saved by it_real_incr. See the function it_real_fn() in kernel/itimer.c for the specific code.

  (3) struct timer_list real_timer;

  A timer structure (Linux has two timer structures, the other is called old_timer). The definition of the data structure is in include/linux/timer.h, and the related operation functions can be found in add_timer() and del_timer() in kernel/sched.c.

  (4) unsigned long it_virt_value,it_virt_incr;

  Off
to the process execution time itimer user mode software timing. Use jiffies as the unit. Processes running in user mode, so that each tick it_virt_value minus 1, reduced to 0 when the
signal SIGVTALRM to the process, and re-set the initial value. The initial value is saved by it_virt_incr. See the function
do_it_virt() in kernel/sched.c for the specific code .

  (5) unsigned long it_prof_value,it_prof_incr;

  The same is the
itimer software timing. Use jiffies as the unit. Regardless of whether the process is running in user mode or kernel mode, each tick reduces it_prof_value by 1, and when it reaches 0, it sends a signal
SIGPROF to the process and resets the initial value. The initial value is saved by it_prof_incr. See the function do_it_prof in kernel/sched.c for the specific code.

  (6) long utime,stime,cutime,cstime,start_time;

  The above are the running time of the process in the user mode, the running time of the process in the kernel mode, the sum of the running time of all levels of subprocesses in the user mode, the sum of the running time of all levels of subprocesses in the core mode, and the time to create the process.

6. Semaphore data members

  (1) struct sem_undo * semundo;

  Into the
process once per operating signal, generates an undo operation of this operation, which is described by sem_undo structure. The linked list of undo operations belonging to the same process is
indicated by the semundo attribute. When the process terminates abnormally, the system will call the undo operation. The member semadj of sem_undo points to a data array that represents the amount of each undo. The structure is defined in
include/linux/sem.h.

  (2) struct sem_queue *semsleeping;

  Each signal corresponding to the set amount a
th sem_queue wait queue (see include / linux / sem.h). When operating the process by blocking semaphore and set, which is linked to semsleeping off instruction
in the queue of this semaphore sem_queue set. In turn, semsleeping. The sleeper points to the PCB of the process.

7. Process context

  (1) struct desc_struct *ldt;

  Process pointer to the local descriptor table of CPU segment storage management, used to simulate WINE
Windows programs. In other cases, the value is NULL, and the ldt of the process is the default_ldt defined in arch/i386/traps.c.

  (2) struct thread_struct tss;

  The task status segment
corresponds to the TSS of the INTEL CPU, such as various general registers. When the CPU is scheduled, the TSS of the currently running process is saved to the tss of the PCB, and the content of the tss of the newly selected process is copied to the TSS of the CPU. The structure is defined in include/linux/tasks.h.

  (3) unsigned long saved_kernel_stack;

  The stack pointer saved for the MS-DOS emulation program (or system call vm86).

  (4) unsigned long kernel_stack_page;

  When running in kernel mode, each process has a kernel stack, and its base address is stored in kernel_stack_page.

8. File system data members

  (1) struct fs_struct *fs;

  fs
save the relationship messaging process itself and the VFS, which points to the root of the root node, pwd node points to the current directory, umask given access mode of new files (umask system calls can be more
changes), count attribute is reserved for Linux, As shown in the figure on the next page. The structure is defined in include/linux/sched.h.

  (2) struct files_struct *files;

  files contains the files currently opened by the process (struct file
*fd[NR_OPEN]). In Linux, a process can only open NR_OPEN files at the same time. Moreover, the first three items are preset as standard input, standard output, and error message output files.

  (3) int link_count;

  The number of file links.

9. Memory data members

  (1) struct mm_struct *mm;

  In linux
, the strategy of paging on demand is adopted to solve the memory requirements of the process. The data member mm of task_struct points to the mm_struct structure for storage management. It contains a virtual memory queue
mmap, which points to a virtual memory block described by a number of vm_area_struct. At the same time, in order to speed up the access speed, mmap_avl in mm maintains an AVL tree. In the tree, all
vm_area_struct virtual memory blocks point to the adjacent low virtual memory block from the left pointer, and the right pointer points to the adjacent high virtual memory block.
The structure is defined in include/linux/sched.h.

10. Page management

  (1) int swappable:1;

  Whether the memory pages occupied by the process can be swapped out. A swappable of 1 means swappable. The reset and setting of this flag are performed in the do_fork() function (see
kerenl/fork.c).

  (2) unsigned long swap_address;

  The process page whose virtual memory address is lower than swap_address has been swapped out or has been swapped out before. The next page that can be swapped out by the process starts from swap_address. See
swap_out_process() and swap_out_pmd() (see mm/vmscan.c).

  (3) unsigned long min_flt , maj_flt;

  The
process of the cumulative number of minor and major missing pages missing page number. maj_flt is basically the same as min_flt, but the count range is wider than the latter (see fs/buffer.c and
mm/page_alloc.c). min_flt only do_no_page (), do_wp_page () in (see mm / memory.c) count can be added
to writes pages.

  (4) unsigned long nswap;

  The cumulative number of pages swapped out by this process.

  (5) unsigned long cmin_flt,cmaj_flt,cnswap;

  Cumulative count of swap-in pages and swap-out pages of all levels of child processes with this process as ancestor.

  (6) unsigned long old_maj_flt,dec_flt;

  (7) unsigned long swap_cnt;

  The maximum number of pages that can be swapped out for the next signal.

11. Data members when supporting symmetric multi-processor mode (SMP)

  (1) int processor;

  The CPU being used by the process.

  (2) int last_processor;

  The last CPU used by the process.

  (3) int lock_depth;

  The depth of the system kernel lock during context switching.

12. Other data members

  (1) unsigned short used_math;

  Whether to use FPU.

  (2) char comm[16];

  The file name of the executable file that the process is running.

  (3) struct rlimit rlim[RLIM_NLIMITS];

  Junction
structure rlimit for resource management, is defined in linux / include / linux / resource.h, a total of two members: rlim_cur resource is the greatest
number; rlim_max member there is a maximum number of resources. In the i386 environment, there are RLIM_NLIMITS items in total for controlled resources, namely 10 items, defined in
linux/include/asm/resource.h, see the following table:

  (4) int errno;

  The error number of the system call that made the last error. 0 means no error. When the system call returns, the whole process also has the error number.

  (5) long debugreg[8];

  Save the value of the INTEL CPU debug register and use it in the ptrace system call.

  (6) struct exec_domain *exec_domain;

  Linux can run programs that conform to the iBCS2 standard generated by other UNIX operating systems on the 80386 platform. The information about the difference between this type of program and the Linux program
is saved by the exec_domain structure.

  (7) unsigned long personality;

  Linux can run programs that conform to the iBCS2 standard generated by other UNIX operating systems on the 80386 platform.
Personality further describes the "personality" information of what kind of UNIX platform the program executed by the process belongs to. Usually there are PER_Linux, PER_Linux_32BIT,
PER_Linux_EM86, PER_SVR3, PER_SCOSVR3, PER_WYSEV386, PER_ISCR4, PER_BSD,
PER_XENIX and PER_MASK, etc. See include/linux/personality.h.

  (8) struct linux_binfmt *binfmt;

  Point to the global execution file format structure to which the process belongs, with a in total. Four types: out, script, elf and java. The structure is defined in include/linux/
binfmts.h (core_dump, load_shlib(fd), load_binary, use_count).

  (9) int exit_code,exit_signal;

  The return code exit_code that caused the process to exit, and the signal name exit_signal that caused the error.

  (10) int dumpable:1;

  Boolean, indicating whether memory dump can be performed when an error occurs.

  (11) int did_exec:1;

  The boolean quantity designed according to POSIX requirements distinguishes whether the process is executing the old program code or executing the new code loaded by execve.

  (12) int tty_old_pgrp;

  The process displays the group ID of the terminal.

  (13) struct tty_struct *tty;

  Point to the information of the display terminal where the process is located. If the process does not need to display the terminal, such as process 0, the pointer is empty. The structure is defined in include/linux/tty.h.

  (14) struct wait_queue *wait_chldexit;

  At the end of the process, or after issuing the system call wait4, in order to wait for the end of the child process, sleep itself (the parent process) on the queue. The structure is defined in include/linux/
wait.h.

13. Global variables of the process queue

  (1) current;

  The pointer of the currently running process. In SMP, it points to the current process of the CPU that is being scheduled in the CPU group:

  #define current(0+current_set[smp_processor_id()])/sched.h/

  struct task_struct *current_set[NR_CPUS];

  (2) struct task_struct init_task;

  The PCB of process 0 is the "root" of the process and always maintains the initial value INIT_TASK.

  (3) struct task_struct *task[NR_TASKS];

  Into the
maximum number of processes drive array queue, the provisions of the system can run at the same time (see kernel / sched.c). NR_TASKS is defined in include/linux/tasks.h
with a value of 512. Each process occupies an array element (the subscript of the element is not necessarily the pid of the process), task[0] must point to init_task (process number 0).
The PCB of all processes can be traversed through the task[] array. But Linux also provides a macro definition for_each_task() (see
include/linux/sched.h), which traverses the PCB of all processes through next_task:

  #define for_each_task(p) \

  for(p=&init_task;(p=p->next_task)!=&init_task;)

  (4) unsigned long volatile jiffies;

  Base time for Linux (see kernal/sched.c). Cleared to 0 when the system is initialized, and then incremented by 1 by the clock interrupt service routine do_timer() every 10ms.

  (5) int need_resched;

  Reschedule flag (see kernal/sched.c). Set when Linux scheduling is required. Before the system call returns (or under other circumstances), determine whether the flag is set. If set, immediately call schedule for CPU scheduling.

  (6) unsigned long intr_count;

  Hutchison
recorded nesting interrupt service routine (see kernal / softirq.c). In normal operation, intr_count is 0. When the hardware interrupt is processed, tasks or perform tasks in the queue
when the line bottom half task queue, intr_count non-zero. At this time, the kernel prohibits certain operations, such as not allowing rescheduling.

Guess you like

Origin blog.csdn.net/lxp_mujinhuakai/article/details/69945474