Linux提高:僵尸进程

简介: Linux提高:僵尸进程

题目


代码实现一个僵尸进程


代码


实现僵尸进程,只需让子进程先于父进程结束,并且父进程不调用 wait/ waitpid 函数回收子进程的退出状态。在父进程没有退出的转态下使用 ps 命令即可查看存在的僵尸进程信息。


下图运行结果,红框里为僵尸子进程



/*************************************************************************
    > File Name: main.c
    > Author: 杨永利
    > Mail: 1795018360@qq.com 
    > Created Time: 2021年07月14日 星期三 21时46分58秒
 ************************************************************************/
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
  pid_t pid;
  // 创建进程
  pid = fork();
  // 创建失败
  if (pid < 0)
  {
    perror("fork error:");
    exit(1);
  }
  // 子进程
  else if (pid == 0)
  {
    printf("I am child process.I am exiting.\n");
    exit(0);
  }
  printf("I am father process.I will sleep two seconds\n");
  //等待子进程先退出
  sleep(2);
  //输出进程信息
  system("ps -o pid,ppid,state,tty,command");
  printf("father process is exiting.\n");
  return 0;
}


知识回顾


详细内容请移驾另一篇博客:


https://yangyongli.blog.csdn.net/article/details/118738314

相关文章
|
2天前
|
存储 Linux Shell
Linux:进程等待 & 进程替换
Linux:进程等待 & 进程替换
29 9
|
2天前
|
存储 Linux C语言
Linux:进程创建 & 进程终止
Linux:进程创建 & 进程终止
24 6
|
21小时前
|
Linux 数据库
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
|
21小时前
|
Unix Linux 调度
linux线程与进程的区别及线程的优势
linux线程与进程的区别及线程的优势
|
21小时前
|
Linux 调度 C语言
|
2天前
|
存储 安全 Linux
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
|
2天前
|
消息中间件 算法 Linux
【Linux】详解如何利用共享内存实现进程间通信
【Linux】详解如何利用共享内存实现进程间通信
|
2天前
|
Linux
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
|
2天前
|
Linux
【Linux】匿名管道实现简单进程池
【Linux】匿名管道实现简单进程池