1. What is a process?
A process is a program in the execution period and a general term for all its resources, including virtual processors, virtual spaces, registers, stacks, and global data segments.
In Linux, each process is assigned a data structure when it is created, called a process control block (PCB ). PCB contains a lot of important information for System Scheduling and process execution. The PCB of all processes is stored in the kernel space. The most important information in PCB is the process PID, which is used by the kernel to uniquely identify a process. The PID can be used cyclically. The maximum value is 32768. The pid of the init process is 1, and other processes are descendants of the INIT process.
In addition to the process control block (PCB), each process has an independent Kernel stack (8 K), a process descriptor structure, the data is stored in the kernel space as the control information of the process. The user space of the process mainly stores code and data.
2) Process Creation
A process creates a new process by calling: fork (),: vfork (), and: Clone. In the kernel, they are implemented by calling do_fork. The traditional fork function directly copies all resources of the parent process to the child process. In Linux: fork () is implemented by copying pages at write time. That is to say, the Parent and Child processes share the same resource copy. Only when the data changes, data will be copied. Generally, after a child process is created, it immediately calls exec () to avoid copying all resources of the parent process.
The differences are as follows:
: Fork (): All data structures of the parent process are copied to the child process (copy the page at write time ).
: Vfork (): Only copies task_struct and kernel stack, so only one thread of the parent process is generated (no independent user space ).
: Clone (): Powerful, with many parameters. : Clone () allows you to selectively inherit the resources of the parent process. You can share a virtual space with the parent process like: vfork, this allows you to create a thread, and you can not share it with the parent process. You can even choose to create a process and a parent process, instead of a parent-child relationship, but a sibling relationship.
3. Process Revocation
The process exits the execution by calling exit (). This function terminates the process and releases all resources. The parent process can use wait4 () to query whether the child process is terminated. After the process exits and runs, it is frozen until its parent process calls wait () or waitpid. When the parent process exits, the kernel will specify other processes in the thread group or the INIT process as the new parent process of its child processes. When a process receives a signal that cannot be processed or ignored, or when an unrecoverable CPU exception occurs in the kernel state and the kernel is running, the kernel can force the process to terminate.
4. Process Management
The kernel stores process information in a bidirectional cyclic linked list (kernel space) called a task list ). Each item in the Linked List is of the task_struct type and is called the process descriptor structure (process descriptor). It contains all information about a specific process, including the opened file and the address space of the process, pending signals, Process status, and so on.
In Linux, task_struct is allocated through the slab distributor, so that object reuse and cache coloring can be achieved (by pre-allocating and reusing task_struct, the resource consumption caused by Dynamic Allocation and release can be avoided ).
The kernel organizes all processes in the task_running state into a two-way cyclic queue that can run. The scheduling function scans the entire runable queue to get the most executable process to be executed. Avoid scanning all processes and improve scheduling efficiency.
5. Process Kernel stack
Linux allocates an 8 KB memory area for each process to store two different data structures of the process: thread_info and the kernel stack of the process.
When a process is in the kernel state, it uses a stack different from the user State stack. Therefore, 8 KB is sufficient for the stack and descriptor.