1. Basic overview of the process
Definition: A process is a running instance of an executable program that has been started.
/proc directory under the name of a directory, each directory represents a process, preserving the properties of the process information, each process PID is unique, even if the process exits, the other process will not occupy its PID
1.1 Components of the process
已分配内存的地址空间安全属性,包括所有权凭据和特权程序代码的一个或多个执行线程进程状态
1.2 Environment of the process
Local and global variables
Current Dispatch context
Assign system resources, such as file descriptions that conform to network ports
1.3 Generation of processes
The existing (parent) process replicates its own address space (fork) to create a new (child) process structure.
Each new process is assigned a unique process ID (PID) that meets the requirements for tracking and security. The PID and parent process ID (PPID) are elements of the new process environment.
Any process can create child processes. All processes are descendants of the first system process. On REHEL7, the first system process is systemd.
Through the fork routines, the child processes inherit security identities, past and current file descriptors, port and resource privileges, environment variables, and program code. The child process may then exec its own program code. Typically, the parent process is asleep while the child process is running, setting a request (wait) to signal when the child process is complete. On exit, the child process may and shuts down or discards its resources and environment, and the remainder is called a zombie. The parent process wakes up when the child process exits, cleans up the remaining structure, and then resumes execution of its own program code.
1.4 Classification of the process
Foreground process: terminal-related process, process initiated through terminal
Note: You can also send the foreground-initiated process to the background to run in daemon mode.
Daemon: Daemon, a process initiated during system boot that is unrelated to a terminal, such as a kernel
2. Status of the process
Excuting //运行态ready //就绪态,也可称作睡眠态 Uninterrutible sleep //不可中断的睡眠。不可随时会唤醒,只有当IO资源加载成功后才能唤醒 Interruptible sleep // 可中断的睡眠。可随时唤醒 Zombie //僵尸进程。正常运行结束了,但是不释放占据的内存资源 Stopped //停止态,暂停于内存中,但不会被调度,除非手动启动
Causes of process sleep
When an executing process needs to load additional IO resources, due to the slow speed of the IO device, it will go to sleep and hand over the CPU to other processes to avoid wasting the remaining execution time.
In a multitasking operating system, each CPU (or CPU core) processes a process at a point in time. As the process runs, its direct requirements for CPU time and resource allocation change. A process assignment has a state that changes as the environment requires it.
Linux process status
Flags the status and description of the kernel definition
R task_running: The process is executing on the CPU, or is waiting to run. While in a running (or operational) state, the process may be performing user routines or kernel routines (system calls), or is queued and ready
S task_interruptible: The process is asleep and is waiting for a condition: hardware request, system resource access, or signal. When the event or signal satisfies the condition, the process returns to run.
D task_uninterruptible: This process is also sleeping, but unlike the S state, it does not respond to signals that are passed. Used only under certain conditions, where a process may cause unexpected device state
K task_killable: The process is asleep and is the same as the non-interruptible D state, but has been modified. Allow a waiting task to be interrupted by a response signal (exit completely). Utility normally interruptible process status shown as D status
T task_stopped: The process has been stopped (paused) and is usually signaled by a user or other process. The process can return to the running state via another signal and continue execution (recovery)
Z Exit_zombie: The child process signals the parent process on exit. All resources except the process identity (PID) are freed.
X Exit_dead: When the parent process cleans up (gets) the remaining child process structure, the process is now completely freed. This state will never be seen in the process listing utility
< high-priority processes
N Low-priority processes
- Processes in the foreground process group
| Multi-line process
s session Process first process
3. Process priority 3.1Linux process scheduling and multitasking
Modern computers each have hundreds of CPUs, each with multiple cores on each CPU, and they can execute hundreds of instructions. But all of these systems often have one thing in common: the number of processes they need to run is always more than the number of cores actually.
The number of processes (and threads) that are actually capable of running through time slicing technology, Linux (and other operating systems) can exceed the number of actual processing units available. The operating system process scheduler will quickly switch between processes on a single core, giving the user an impression of running concurrently with multiple processes.
The part of the Linux kernel that performs this switchover is called the process scheduler.
3.2 Process Priority
Process priority range: 0-139, the smaller the number, the higher the priority
0-99 real-time priority, kernel tuning
100-139 static priority, user-controllable
Characteristics of process Priorities
Get more CPU Run time
Higher priority to get CPU run opportunities
要修改进程的优先级可以通过调整进程的nice值来实现,nice值越小,优先级越高 nice值的范围是(-20,19),-20对应100 ,19对应139
3.3 Relative priority
Because not every process is as important as other processes, you can tell the scheduler to use different scheduling policies for different processes. Most processes running on a regular system use a scheduling policy called SCHED_OTHER
(also known as SCHED_NORMAL
), but there are other policies that can be used for different purposes.
Because not all processes are created the same way, you can specify a SCHED_NORMAL
relative priority for the processes that are running with the policy. This priority is called the value of the process nice
. A process can have 40 different levels of nice
values.
These nice
levels range from 20 to 19. By default, a process inherits the level of its parent process nice
, typically 0
The higher the nice level, the lower the priority (which makes it easier for the process to get its CPU usage to other processes)
The lower the nice level, the higher the priority (the process is less inclined to give up the CPU)
If there is no resource contention (for example, when the number of active processes is less than the number of available CPU cores), even the nice-high processes will still use all available CPU resources that they can use. When the number of processes requesting CPU time exceeds the number of cores available, a process with a higher nice level will receive less CPU time than a process with a lower nice level.
3.4 nice级别与权限为很少占CPU资源的进程设置较低的nice级别可能会对同一系统上运行的其他进程的性能造成负面影响,所以紧允许root用户设置负nice级别以及降低现有进程的nice级别普通非特权用户仅允许是指正的nice级别,只能对现有进行级别提升nice级别,而不能降低nice级别。3.5 进程优先级调整 调整nice值 ``` //调整以及启动的进程的nice值 renice NI PID (例:renice 3 3704) //在启动时指定nice值(-20,19) nice -n NI (COMMAND) ``` 4.进程管理命令 Linux系统各进程的相关信息均保存在/proc/PID目录下的各文件中 4.1 ps(process state)命令用于列出当前的进程,可以显示详细的进程信息,包括: 用户标识符(UID),它确定进程的特权 唯一进程识别符(PID) CPU和已经花费的时间 进程在各种位置上分配的内存数量 进程的位置STDOUT,称为控制终端 当前的进程状态ps支持是三种选项格式: UNIX(POSIX)选项、可以分组但必须以连字符开头 BSD选项,可以分组但不可与连字符同用 GNU长选项,以双连字符开头ps(process state),显示进程信息,注意事项: 加了[ ]中括号的,表示内核进程,通常位于顶部 exiting或defunct表示僵尸进程
Linux Process Management