【问题标题】:linux process waiting for I/O, its status is S+ not D?linux进程等待I/O,它的状态是S+而不是D?
【发布时间】:2017-03-10 11:20:00
【问题描述】:

我预计等待 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 状态下制作自己的流程?只是想模拟一下这个场景

【问题讨论】:

    标签: c++ linux process io wait


    【解决方案1】:

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

    【讨论】:

    • 有没有办法可以编程来模拟这个?谢谢!
    • ioctl() 调用应该使进程处于不间断状态,也许您可​​以使用手工制作的慢速ioctl 调用构建内核。
    猜你喜欢
    • 2011-12-21
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多