您的位置:首页 > 其它

进程之Process Control实验(二)

2018-02-27 22:15 197 查看

1. 实验

Write a program that creates a zombie, and then call system to execute the ps(1) command to verify that the process is a zombie.

2. 代码展示

#include <unistd.h>
#include <stdio.h>
#include <err.h>
#include <stdlib.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE);} while (0)

int main( int agrc, char ** argv)
{
pid_t pid;

if((pid = fork())<0)
handle_error("fork");
if(pid == 0)
{
exit(0);
}
else
{
if(system("ps -o pid,ppid,pgid,sid,comm") == -1)
handle_error("system");
}
}


  执行结果如下:

[root@localhost ~]# ./8_1
PID   PPID   PGID    SID COMMAND
1040   1039   1040   1040 bash
1319   1040   1319   1040 8_1
1320   1319   1319   1040 8_1 <defunct>
1321   1319   1319   1040 ps


  从结果可得
8_1
的子进程
1320
确实为僵尸进程,而
ps
命令的父进程为
8_1
,这点值得注意。

3. 题外话

  第8章的实验比较简单,虽然有其他的题目,但是笔者筛选了下,就做了这个。也没有过分深究一些现象,总是把握主要矛盾,之前深究的细节也随着时间又淡忘了,看来学习知识也讲究实用主义,并非面面俱到就是好的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: