我预计等待 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/