Linux Process Management

Keywords: Linux Unix server

Process and thread definition

A process is a program in execution (the object code is stored on the media). A process includes not only the running program, but also some other resources, such as open files, suspended signals, internal kernel data, processor status, etc. An execution thread is projected in a section of memory, which also contains a data section for storing global variables;

A thread is an active object in a process (also known as an execution thread). Each thread has an independent program counter, process stack and a set of process registers.

The object of kernel scheduling is threads, not processes. In traditional Unix, a process contains only one process, but now the system contains multiple threaded programs.
Modern virtual mechanism:
1. Virtual processor;
2. Virtual memory;
The program itself is not a process, but the general name of the program and related resources during execution. Virtual memory and processor, resulting in two or more different processes executing the same program; The processes with two or more programs can also share open files, address space and other resources;

In Linux systems, this is usually the result of calling the fork() system, which creates a new process by copying an existing process.
The process calling fork() is called the parent process, and the process generated after the call is called the child process. At the end of the call, the return point is recovered from the parent process at the same location; The subprocess starts to execute;
The fork() system returns twice in the kernel:
1. Return to the parent process once;
2. Another return to the newly generated process;
Usually, a new process is created to immediately execute a new and different program, and then the address space can be created by calling the exec() function.

Note: another name for a process is task (trsk), which is also commonly called task in Linux.

Process descriptor and task structure

The kernel puts the process list in the two-way circular list of task list, and each type in the list is task_struct, which becomes the structure of the process descriptor. This structure is defined in the < Linux / sched. H > file. The process descriptor contains all process information.

1. Allocate process descriptor

Linux By distribution slab Distributor distribution task_sturck Structure, which can achieve object reuse and cache coloring(cachecoloring)Purpose.
stay x86 Up, struct thread_info In file<asm/thread_info.h>As defined in:
struct thread_info{
	struct task_struct		*task;
	struct exec_domain		*exec_damain;
	__u32					flags;
	__u32					status;
	int						cpu;
	mm_segment_t			addr_limit;
	struct	restart_block	restart_block;
	void					*sysenter_return;
	int						uaccess_err;
	};

Thread per task_ The info structure is assigned at its tail. The task field in the structure stores the actual task pointing to the task_ Pointer to sturct.

2. Storage of process descriptor

The kernel passes a unique process identifier or PID To identify each process. PID Is a number expressed as pid_t An implicit type is actually a int Type. PID Maximum value of 32768( short int The maximum value of the short integer of. The kernel puts each process's PID Put it in the descriptor of your own process.
**increase PID The upper limit of,/proc/sys/kernel/pid_max To raise the upper limit.**
In the kernel, access tasks usually need to obtain a pointer task_struct Pointer to. In fact, most of the process code in the kernel is directly through task_struct Yes.
adopt current The speed at which the macro can find the descriptor of the process symbol of the currently running process is very important. The implementation of the macro is different with different hardware architecture.
stay x86 On the system, current Mask the last 13 significant bits of the stack pointer and use the calculated thread_info Offset of.
The operation is through curren_thread_info()Function. The assembly code is as follows:
movl 	$-8192, %eax
andl	%esp, %eax

It is assumed that the stack size is 8kb. When 4KB stack is enabled, 4096 is used instead of 8192
Finally, current starts from thread_ Extract and return task from the task field of info_ Sturct address:

current_thread_info() ->task;

3. Status of the process

In the process descriptor state Domain describes the current state of the process. Every process in the system must be in the current state. Every process in the system must be in one of the five process states.
There are five states of the domain:
a,TASK_RUNNING(function)--The process is executable; It is either executing or waiting in the run queue. This is the only possibility for a process to execute in user space; This state can also be applied to processes executing in kernel space;
b,TASK_INTERRUPTIBLE(Interruptible)-----The process is sleeping(That means he's blocked),When certain conditions are met, the kernel will set the process state to run. The process in this state will also wake up in advance and be ready to be put into operation at any time due to receiving signal 2;
c,TASK_UNINTERRUPTIBLE(Non interruptible)---This state is the same as the interruptible state except that even if the signal is received, it will not be awakened or ready to be put into operation. This state usually occurs when the process must be undisturbed while waiting or when waiting events will occur. Tasks in this state do not respond to signals.
d,	__TASK_TRACED---Processes tracked by other processes, such as through ptrace Track the debugger.
e,	__TASK_STOPPED(stop it)---The process stops executing; The process is not put into operation, nor can it be put into operation, nor can it be put into operation. Typically, this state occurs when a message is received SIGSTOP,SIGTTIN,SIGTTOU Waiting for the signal. In addition, any signal received during debugging will make the process enter this state;

4. Set current process status

The kernel often needs to adjust the state of a process. It's best to use it at this time set_state(task,state)Function:
sat_task_state(task,state);			/*Set the status of task statk to stata*/

This function sets the specified process to the specified state. If necessary, it will set a memory barrier to force other processors to reorder (generally only in SMP systems). Otherwise, it will be equivalent to:

  task->state = state;

ste_current_state(state) and set_task(current,state) has the same meaning.
Refer to the description of the implementation of these related functions in < liunx / sched. H >.

5. Process context

Executable program code is an important part of a process. The code is loaded from an executable file into the process's spatial address for execution. General programs are executed in user space. General programs are executed in user space. When a program executes a system call or starts an exception, it will fall into kernel space. At this point, the kernel "executes on behalf of the process" and is in the process context. The current macro is valid in this context. Unless a higher priority process needs to be executed in this gap and acted by the scheduler. If not, the program will continue to execute in user space when the kernel exits and the program recovers.
System calls and exception handlers are clearly defined interfaces for the kernel. In other words, access to the kernel should be through these interfaces.

Posted by Scorptique on Tue, 02 Nov 2021 13:15:11 -0700