This is the unveiling of process management , A brief introduction of a process in the kernel's eyes , Create... For the next process , Process scheduling , Process management and other chapters to prepare for learning .

From program to process to kernel

What is the procedure , What is process , A picture can explain :

Linux What a process looks like in the eyes of the kernel ?

 

The process of converting a program into a process is not the focus of this article , I don't want to elaborate here , Details please see 《 Linux The context of the program compilation process  》. Next we change the camera , Standing in the kernel OS From the perspective of what is the procedure , What is a process .

ELF After the executable is sent to the kernel ,OS How do you think of it ? In other words , kernel OS In my eyes, there is only progress :

Linux What a process looks like in the eyes of the kernel ?

 

adopt top Order us to see linux All kinds of processes ( That's right ).

The kernel passes through task_struct Describe the process

Command pstree You can let the kernel tree the relationships between processes , Here's the picture :

Linux What a process looks like in the eyes of the kernel ?

 

This is the structure of the process in the kernel , How does the kernel manage these processes in a tree structure ? The data structure used to describe the process , It can be understood as the properties of a process . Like the state of the process 、 Identification of the process (PID) etc. , All of them are encapsulated in the data structure of process descriptor , Let's take a look at today's protagonist —— task_struct  Structure .

struct task_struct {
    volatile long state;  // Indicates whether the process can execute , Or interruptible information  -1 unrunnable, 0     runnable, >0 stoppedunsigned long flags;  
//Flage  Is a process, , Calling fork() When given int sigpending;    
// Is the signal to be processed on the process mm_segment_t addr_limit; 
// Process address space , Distinguish the location of kernel process and normal process in memory                         //0-0xBFFFFFFF for user-thead                        
//0-0xFFFFFFFF for kernel-thread// Dispatch sign , Indicates whether the process needs to be rescheduled , If not 0, When returning from kernel state to user state , There will be scheduling
volatile long need_resched;int lock_depth;  
// Lock depth long nice;       // The basic time slice of the process
// Process scheduling strategy , There are three kinds of , Real time process :SCHED_FIFO,SCHED_RR,  Time sharing process :SCHED_OTHER
unsigned long policy;
struct mm_struct *mm; // Process memory management information
int processor;// If the process is not in any CPU Up operation , cpus_runnable  The value of is 0, It is 1  This value is updated when the run queue is locked
unsigned long cpus_runnable, cpus_allowed;
struct list_head run_list; // Pointer to run queue
unsigned long sleep_time;  // Sleep time of process // It is used to link all processes in the system into a two-way circular list ,  Its root is init_taskstruct task_struct *next_task, *prev_task;
struct mm_struct *active_mm;
struct list_head local_pages;       // Point to the local page       
unsigned int allocation_order, nr_local_pages;
struct linux_binfmt *binfmt;  // The format of the executable that the process runs int exit_code, exit_signal;
int pdeath_signal;     // Signal sent to child process when parent process terminates
unsigned long personality;//Linux Can be run by other UNIX The conformity generated by the operating system iBCS2 Standard procedure int did_exec:1; 
pid_t pid;    // Process identifier , To represent a process
pid_t pgrp;   // Process group identification , Indicates the process group to which the process belongs pid_t tty_old_pgrp;  // The group ID of the process control terminal pid_t session;  // Session ID of the process
pid_t tgid;int leader;     // Indicates whether the process is a session supervisor
struct task_struct *p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr;struct list_head thread_group;   // Thread list
struct task_struct *pidhash_next; // Used to chain processes into HASH surface
struct task_struct **pidhash_pprev;
wait_queue_head_t wait_chldexit;  // for wait4() Use
struct completion *vfork_done;  // for vfork()  Use unsigned long rt_priority; // Real time priority , It is used to calculate the weight value
struct timer_list real_timer;   // A pointer to a real-time timer
struct tms times;      // Record the time consumed by the process
unsigned long start_time;  // When the process was created // Record the progress in each CPU User state time and core state time consumed on
long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; 
int swappable:1; // Indicates whether the virtual address space of the process is allowed to be swapped out
int ngroups; // Record how many user groups the process is in
gid_t groups[NGROUPS]; // Record the group of processes // The power of the process , They are the set of significant bits , Set of inheritance bits , Allow bit sets kernel_cap_t cap_effective, cap_inheritable, cap_permitted;int keep_capabilities:1;
struct user_struct *user;
struct rlimit rlim[RLIM_NLIMITS];  // Resource restriction information related to the process
unsigned short used_math;   // Whether to use FPUchar comm[16];   // The name of the executable that the process is running  // File system information
int link_count, total_link_count;//NULL if no tty  The control terminal where the process is located , If you don't need a control terminal , Then the pointer is null
struct tty_struct *tty;
unsigned int locks;// Interprocess communication information struct sem_undo *semundo;  // All the progress on the semaphore undo operation
struct sem_queue *semsleeping; // When a process is suspended due to a semaphore operation , He records the waiting operations in the queue // Process CPU state , When switching , To save to the task_struct in
struct thread_struct thread;  // File system information
struct fs_struct *fs;  // Open file information
struct files_struct *files;  // Signal processing functions spinlock_t sigmask_lock;
struct signal_struct *sig; // Signal processing functions
sigset_t blocked;  // Signals that the process is currently blocking , Each signal corresponds to a bit
struct sigpending pending;  // Is the signal to be processed on the process ......
};

The kernel is through list_head The chain list manages the process relationships in a tree structure .

task_struct Too much content in the structure , Only some member variables are listed here , Interested readers can go to source include/linux/sched.h Header file view .

task_struct The main categories of information in :

1.  Identifier : Unique identifier describing the process , Used to differentiate other processes .
2.  state : Task status , Exit code , Exit signal, etc  
3.  priority : Priority relative to other processes  
4.  Program counter : The address of the next instruction to be executed in the program  
5.  Memory pointer : Including program code and process related data pointer , There are pointers to memory blocks shared with other processes  
6.  Context data : Data in the processor's registers at process execution time  
7. I/O State information : Including the displayed I/O request , The process of distribution I/O List of files used by devices and processes  
8.  Billing information : It may include the sum of processor time , The sum of the clocks used , The time limit , Write down the account number, etc 

Each of these types of information can be explained in a separate chapter , Here is a brief description of the task state transition , In the next chapter, I will introduce each category in depth .

Task state transition

You can see the comments after the variable definition above , It describes the variable content <0 It doesn't work ,=0 It's running state ,>0 It's a stop state .

Let's introduce some commonly used values :

Linux What a process looks like in the eyes of the kernel ?

 

The state transition of task state in different situations is as follows :

Linux What a process looks like in the eyes of the kernel ?

 

Figure from https://www.lagou.com/lgeduarticle/96239.html

How does the kernel store task_struct

We know that the stack space occupied by a process is useful for user stack and kernel stack , See the previous article for the distribution of user stacks 《 C Language in ARM When a function is called in , How the stack changes ? 》. So how does the kernel stack store process descriptors ?

The kernel stack is invisible to applications , Because it's in kernel space . During application execution , If something unusual happens 、 Interrupt or system call , The application will be suspended , The system goes into kernel state , Go ahead and execute code such as exception response , The stack used at this time is the kernel stack .

Linux What a process looks like in the eyes of the kernel ?

 

To save space ,linux Put the kernel stack next to task_struct Of thread_info Put together , As shown above ,thread_info The process is stored in / Threads ( The kernel doesn't differentiate between processes and threads ) Some of the data , It includes pointing to task_struct Pointer to structure . Array stack The kernel stack ,stack occupy 8K/4K( Depending on the configuration ) Space .

union thread_union {#ifndef CONFIG_THREAD_INFO_IN_TASK  struct thread_info thread_info;#endif  unsigned long stack[THREAD_SIZE/sizeof(long)];};

Last

By now, I should have learned how to convert a program into a process , How does the kernel describe processes , How to store processes , Of course, there are many descriptions of the process that are not introduced , For example, process scheduling , priority , Memory management and so on , These will be separately introduced in detail in later articles . But all this information is stored in today's protagonists ——task_struct.