gpt4 book ai didi

linux - 读/写阻塞系统调用是否将进程置于 TASK_UNINTERRUPTIBLE 或 TASK_INTERRUPTIBLE 状态?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:51 26 4
gpt4 key购买 nike

如 signal(7) 的手册页所述,

   Interruption of system calls and library functions by signal handlers
If a signal handler is invoked while a system call or library function call is blocked, then either:

* the call is automatically restarted after the signal handler returns; or

* the call fails with the error EINTR.

Which of these two behaviors occurs depends on the interface and whether or not the signal handler was established using the SA_RESTART flag (see sigaction(2)). The details vary across UNIX systems; below, the details for
Linux.

If a blocked call to one of the following interfaces is interrupted by a signal handler, then the call will be automatically restarted after the signal handler returns if the SA_RESTART flag was used; otherwise the call will
fail with the error EINTR:

* read(2), readv(2), write(2), writev(2), and ioctl(2) calls on "slow" devices. A "slow" device is one where the I/O call may block for an indefinite time, for example, a terminal, pipe, or socket. If an I/O call on a
slow device has already transferred some data by the time it is interrupted by a signal handler, then the call will return a success status (normally, the number of bytes transferred). Note that a (local) disk is not a
slow device according to this definition; I/O operations on disk devices are not interrupted by signals.

如前所述,对以下接口(interface)(读、写)之一的阻塞调用被信号处理程序中断,如果使用 SA_RESTART 标志,则调用将在信号处理程序返回后自动重新启动,这意味着在阻塞读/写系统调用的情况下,进程必须处于 TASK_INTERRUPTIBLE 状态。

但是当我试图找出将进程置于 TASK_UNINTERRUPTIBLE 状态的阻塞系统调用时,我发现了 https://unix.stackexchange.com/questions/62697/why-is-i-o-uninterruptibleWhy doing I/O in Linux is uninterruptible? ,并且在这两个地方都提到阻塞的 I/O 调用(读取、写入)会将进程置于 TASK_UNINTERRUPTIBLE 中。

这里也提到了:https://access.redhat.com/sites/default/files/attachments/processstates_20120831.pdf

The Uninterruptible state is mostly used by device drivers waiting for disk or network I/O. When the process
is sleeping uninterruptibly, signals accumulated during the sleep are noticed when the process returns from
the system call or trap. In Linux systems. the command ps -l uses the letter D in the state field (S) to
indicate that the process is in an Uninterruptible sleep state. In that case, the process state flag is set as
follows:
p->state = TASK_UNINTERRUPTABLE
LEARN MORE: Read more about D states in the Red Hat Knowledgebase:
https://access.redhat.com/knowledge/solutions/59989/

这有点令人困惑。

我还想知道其他阻塞的系统调用,它们可以将进程置于 TASK_UNINTERRUPTIBLE 状态。

最佳答案

对于 read(2)write(2) 系列系统调用, sleep 类型取决于正在访问的文件类型。在您引用的文档中,“慢速”设备是 read/write 将不间断休眠的设备,“快速”设备是不间断休眠的设备(不间断 sleep 状态被命名为 D 用于“磁盘等待”,因为最初在磁盘文件上读/写 是此类 sleep 的最常见原因)。

请注意,“阻塞”在技术上仅指可中断的 sleep 。

几乎任何系统调用都可以进入不间断 sleep ,因为当进程需要获取保护内部内核资源的锁时(除其他外)可能会发生这种情况。通常,这种不间断的 sleep 时间很短,您不会注意到。

关于linux - 读/写阻塞系统调用是否将进程置于 TASK_UNINTERRUPTIBLE 或 TASK_INTERRUPTIBLE 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56136082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com