进程状态
理念上的状态
新建
子面意思
运行
task_struct在运行队列中排队,就叫做运行态
阻塞
等待非CPU资源就绪
挂起
当内存不足的时候,OS通过适当的置换进程的代码和数据到磁盘,进程的状态就叫做挂起
退出
子面意思
实际上的状态
R:对应运行态
代码:
#include <stdio.h>
#include <unistd.h> int main()
{ while (1) { } return 0;
}
进程信息
S:对应阻塞状态,是可中断睡眠
代码
#include <stdio.h>
#include <unistd.h> int main()
{ while (1) { printf("hello world\\n"); } return 0;
}
进程信息
进程等待屏幕资源就绪的时间要远大于在CPU中运行的时间,所以最后是S状态
D:
磁盘睡眠,深度睡眠,不可被中断的睡眠
T:暂停状态
给S或R状态的进程发送一个19号信号使其暂停
t:暂停状态,调试的时候程序运行至断点处
gdb运行至断点处时,给进程发送了一个暂停信号
Z:
是什么
一个进程已经退出,但是还不允许被OS释放,处于一个被检测的状态
为什么
维持该状态,为了让子进程和父进程来回收
代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>int main()
{pid_t id = fork();if (id < 0){perror("fork");return 1;}else if (id == 0){// child processwhile (1){printf("I am child, pid: %d, ppid: %d\\n", getpid(), getppid());sleep(5);exit(1);}}else {// father processwhile (1){printf("I am father, pid: %d, ppid: %d\\n", getpid(), getppid());sleep(1);}}return 0;
}
进程信息
X:终止状态,瞬时性非常强