c++ - linux进程等待I/O,状态是S+不是D?

标签 c++ linux process io wait

我预计等待 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() 系统调用是可中断的,这解释了为什么您的进程处于可中断 sleep S+,而不是处于不可中断 sleep 。这是一个链接: What is an uninterruptable process?

关于c++ - linux进程等待I/O,状态是S+不是D?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42717150/

相关文章:

c++ - 取消引用空指针

c++ - 将 GMP 整数转换为以 N 为基数的整数

c++ - 数组键是否确定 C++ 中的数组大小?

c - 流程管理数据结构和流程控制

linux - 在 Go 中设置进程名称(如 `ps` 所示)

c++ - 如何选择功能模板特化?

linux - 运行 Contiki 教程时出现 "Not a git repository:"错误消息

php 到 shell 脚本到 php 通过无密码 ipv6 ssh 隧道获取值

node.js - 打印最后的终端内容

java - 如何在 Java 中使用 pid 获取进程信息?