linux进程在等待I/O,它的状态是S+不是D?

linux process waiting for I/O, its status is S+ not D?

我预计等待 I/O 资源的 linux 进程应该处于 "D" 状态,所以我测试了:

$cat testD.cpp
#include<assert.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
char hello[]="hello";
int main(){
    int fd[2];
    assert(pipe(fd)==0);
    pid_t pid=fork();
    if(pid==0){//child
        sleep(10);
        getchar();
    }else{//father
        char buf[1024];
        read(fd[0],buf,sizeof(buf));
        printf("read%s\n",buf);
        int status;
        waitpid(pid,&status,0);
    }
    return 0;
}

编译并运行它:

$g++ testD.cpp && ./a.out

我发现它的状态是S+,但不是我预期的D。

$ps -a -umyself -o pid,ppid,stat,command|grep a.out
 29798  47236 S+   ./a.out
 29799  29798 S+   ./a.out
 29953  59678 S+   grep --color a.out

我的问题是如何让自己的进程处于D状态?只是想模拟一下这个场景

至少,正常的 read() 系统调用是可中断的,这解释了为什么您的进程处于可中断睡眠 S+,而不是处于不可中断睡眠。这是一个 link: What is an uninterruptable process?