简体   繁体   中英

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

I expected that a linux process waiting for I/O resource should be in "D" state, so I tested:

$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;
}

compile and run it:

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

I found its status is S+, but not D as I expected.

$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

My question is how to make my own process at D state? Just wish to simulate this scenario

At least, normal read() syscall is interruptible, explaining why your process is in interruptible sleep S+, and not in uninterruptible sleep. Here is one link: What is an uninterruptable process?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM