D - Uninterruptible Sleep Process

认识 Linux ps 命令的状态列:

$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 169156 12824 ?        Ss   Nov03   6:05 /lib/systemd/systemd 
root           2  0.0  0.0      0     0 ?        S    Nov03   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   Nov03   0:00 [rcu_gp]
root          14  0.0  0.0      0     0 ?        I    Nov03  10:34 [rcu_sched]
root          68  0.0  0.0      0     0 ?        SN   Nov03   0:00 [ksmd]
root         868  0.0  0.0   2812  1048 ?        Ss   Nov03   0:00 /usr/sbin/acpid
root         875  0.0  0.0 484180 15888 ?        Ssl  Nov03   5:39 /usr/sbin/NetworkManager
root         894  0.0  0.1 917300 28164 pts/0    Ssl+ Nov03   1:52 /usr/local/qualys/cloud-agent 
rtkit       1415  0.0  0.0 155920  2584 ?        SNsl Nov03   0:08 /usr/libexec/rtkit-daemon
supra       2445  0.0  0.0  41472  3876 ?        S<sl Nov03   0:00 /usr/bin/pipewire
supra    2176300  0.0  0.0  15472  4596 pts/2    R+   16:58   0:00 ps aux

从上面的输出可以看到 STAT (state) 列有各种字母组合的状态, 分别代表什么意思呢?

State 含义

https://man7.org/linux/man-pages/man1/ps.1.html

    the state of a process:
               D    uninterruptible sleep (usually IO)
               I    Idle kernel thread
               R    running or runnable (on run queue)
               S    interruptible sleep (waiting for an event to
                    complete)
               T    stopped by job control signal
               t    stopped by debugger during the tracing
               W    paging (not valid since the 2.6.xx kernel)
               X    dead (should never be seen)
               Z    defunct ("zombie") process, terminated but not
                    reaped by its parent

       For BSD formats and when the stat keyword is used, additional
       characters may be displayed:

               <    high-priority (not nice to other users)
               N    low-priority (nice to other users)
               L    has pages locked into memory (for real-time and
                    custom IO)
               s    is a session leader
               l    is multi-threaded (using CLONE_THREAD, like NPTL
                    pthreads do)
               +    is in the foreground process group

虽然上面的各种状态比较详细, 简化一下, 其实主要有: R, S, D, T, Z.
LinuxProcessStateChange.png

关于 T & t Stopped 的状态

从运行或可运行状态,我们可以使用SIGSTOP或SIGTSTP信号将进程置于停止状态(T)。两种信号之间的区别在于,我们使用编程方式发送SIGSTOP,例如运行 kill -STOP {pid} 命令。进程不能忽略此信号,并将进入停止状态。当我们使用键盘CTRL + Z发送SIGTSTP信号, 也可以把它置于T状态。与SIGSTOP不同,进程可以选择忽略此信号并在接收SIGTSTP后继续执行。

在此状态下,我们可以通过发送SIGCONT信号将进程带回运行或可运行状态。

关于 D Uninterruptible 的状态

An uninterruptible sleep state is a sleep state that will not handle a signal right away. It will wake only as a result of a waited-upon resource becoming available or after a time-out occurs during that wait (if specified when put to sleep). It is mostly used by device drivers waiting for disk or network IO (input/output).

When the process is sleeping uninterruptibly, signals accumulated during the sleep will be noticed when the process returns from the system call or trap.

You cannot kill "D" state processes, even with SIGKILL or kill -9. As the name implies, they are uninterruptible. You can only clear them by rebooting the server or waiting for the I/O to respond.

为什么需要 Uninterruptible 状态

对于某些必须在系统可能出现故障或意外断电的情况下确保数据完整性和一致性的系统进程。 例如,当文件系统正在将数据写入磁盘时,如果系统接收到信号或其他事件,则它不能简单地在操作中途停止。 数据必须完整写入,以确保不丢失或损坏。

可以使用不间断睡眠的进程的其他示例包括一些设备驱动程序、备份进程和虚拟内存操作。

简而言之,不间断睡眠用于确保关键系统进程能够完成其任务,而不会出现数据损坏或丢失的风险,即使在发生断电或系统崩溃等意外事件时也是如此。

TASK_KILLABLE state.

这是可中断睡眠进程和不可中断睡眠进程之间的折衷方案。 处于 TASK_KILLABLE 状态的进程仍然不能被通常意义上的中断(即不能强制系统调用返回 EINTR); 然而,处于这种状态的进程可以被终止。 这意味着,例如,通过 NFS 执行 I/O 的进程如果进入楔入状态,则可能会被终止。 并非所有系统调用都实现这种状态,因此某些系统调用仍然有可能卡住不可杀死的进程,但这肯定比以前的情况有所改进。

Cgroup freezer subsystem -> D

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/sec-freezer
this subsystem suspends or resumes tasks in a cgroup.
什么情况下需要 cgroup freezer 子系统

  1. 暂停出问题的进程/线程, 查看它内存状态
  2. 搬家 checkpoint
  3. 暂停消耗资源(CPU/内存/网络)的进程/线程

使用cgroup 挂起进程的例子: https://www.kernel.org/doc/Documentation/cgroup-v1/freezer-subsystem.txt
与 SIGSTOP/SIGCONT 的区别:

  1. 进入的状态不同, SIGSTOP/SIGCONT 进入 stopped (T)状态, freezer 进入 Unineteruptiable sleep D.
  2. 被 SIGSTOP/SIGCONT 的进程能感应到信号, 被 freezer 的进程感应不到自己收到这个信号.

cgroup freezer 子系统和 SIGSTOP/SIGCONT 的区别?

进程的生命周期(from wikipidia):

pLifeCycle.webp

标签: none

添加新评论