无法在Linux中创建僵尸进程 [英] can't create zombie process in linux

查看:348
本文介绍了无法在Linux中创建僵尸进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有奇怪的问题。
我不能在我的项目中创建一个僵尸进程,但我可以在其他文件中做到这一点。
有简单的说明:

Well I have weird problem. I can't create a zombie process in my project, but I can do this in other file. There's simple instructions:

int main()
{
    if(fork()==0)
        printf("Some instructions\n");
    else
    {
        sleep(10);
        wait(0);
    }
    return 0;
}

这是简单的code 10秒创建一个僵尸进程。我检查它确实存在。

That simple code create a zombie process for 10 seconds. I'm checking and it actually exists.

但是,如果我这个code复制到我的程序(我自己的壳),一切都在执行类似之前,但僵尸进程根本不存在。我不知道有什么区别。这是相同的code。

But if I copy this code to my program (my own shell), everything executing like before BUT zombie process doesn't exist at all. I don't know what's the difference. It's same code.

有没有我应该知道的更多信息?
有没有其他的方式简单的方法来创建僵尸?

Is there a more information I should know about that? Is there a other way to create zombie in simple way?

推荐答案

试试这个python脚本:

Try this python script:

#!/usr/bin/python
# -*- coding: utf8 -*-

import subprocess
import time
import threading

# Create 100 subprocesses 

proc = {}
for i in xrange(0,1000):
        proc[i] = subprocess.Popen(['ls','-l'])

# create zombies from this processes, observe one minute zombies
time.sleep(60)

# Zombies dead
proc.communicate()

time.sleep(5)

此后检查僵尸:

# ps -A | grep defunc
14711 pts/49   00:00:00 ls <defunct>
14713 pts/49   00:00:00 ls <defunct>
14716 pts/49   00:00:00 ls <defunct>
.... 
14740 pts/49   00:00:00 ls <defunct>
14741 pts/49   00:00:00 ls <defunct>
14742 pts/49   00:00:00 ls <defunct>
14743 pts/49   00:00:00 ls <defunct>
14746 pts/49   00:00:00 ls <defunct>
14749 pts/49   00:00:00 ls <defunct>
....
14805 pts/49   00:00:00 ls <defunct>
14806 pts/49   00:00:00 ls <defunct>

或c:

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main ()
{ 
  pid_t child_pid;

  child_pid = fork ();
  if (child_pid > 0) {
    sleep (60);
  }
  else {
    exit (0);
  }
  return 0;
}

应该像一个魅力,像一个晶莹剔透的纯海天Vodou

这篇关于无法在Linux中创建僵尸进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆