0
点赞
收藏
分享

微信扫一扫

软件测试学习(一)

看看 Linux 内核源代码怎么说

为了弄明白正在运行的进程是什么意思,我们需要知道进程的不同状态。一个进程可以有几个状态(在

Linux 内核里,进程有时候也叫做任务)。

下面的状态在 kernel 源代码里定义:

R 运行状态( running : 并不意味着进程一定在运行中,它表明进程要么是在运行中要么在运行队列

里。

S 睡眠状态( sleeping): 意味着进程在等待事件完成(这里的睡眠有时候也叫做可中断睡眠

interruptible sleep ))。

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

{

int ret = fork();

if(ret < 0){

perror("fork");

return 1;

}

else if(ret == 0){ //child

printf("I am child : %d!, ret: %d\n", getpid(), ret);

}else{ //father

printf("I am father : %d!, ret: %d\n", getpid(), ret);

}

sleep(1);

return 0;

}

/*

* The task state array is a strange "bitmap" of

* reasons to sleep. Thus "running" is zero, and

* you can test for combinations of others with

* simple bit tests.

*/

static const char * const task_state_array[] = {

"R (running)", /* 0 */

"S (sleeping)", /* 1 */

"D (disk sleep)", /* 2 */

"T (stopped)", /* 4 */

"t (tracing stop)", /* 8 */

"X (dead)", /* 16 */

"Z (zombie)", /* 32 */

};

D 磁盘休眠状态( Disk sleep )有时候也叫不可中断睡眠状态( uninterruptible sleep ),在这个状态的

进程通常会等待 IO 的结束。

T 停止状态( stopped ): 可以通过发送 SIGSTOP 信号给进程来停止( T )进程。这个被暂停的进程可

以通过发送 SIGCONT 信号让进程继续运行。

X 死亡状态( dead ):这个状态只是一个返回状态,你不会在任务列表里看到这个状态。

进程状态查看

Z(zombie)- 僵尸进程

僵死状态( Zombies )是一个比较特殊的状态。当进程退出并且父进程(使用 wait() 系统调用 , 后面讲)

没有读取到子进程退出的返回代码时就会产生僵死 ( ) 进程

僵死进程会以终止状态保持在进程表中,并且会一直在等待父进程读取退出状态代码。

所以,只要子进程退出,父进程还在运行,但父进程没有读取子进程状态,子进程进入 Z 状态

来一个创建维持 30 秒的僵死进程例子:

ps aux / ps axj 命令

#include <stdio.h>

#include <stdlib.h>

int main()

{

pid_t id = fork();

if(id < 0){

perror("fork");

return 1;

~over~

举报

相关推荐

0 条评论