can't create zombie process in linux

别来无恙 提交于 2020-01-02 08:09:31

问题


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;
}

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

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?


回答1:


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)

Thereafter check zombies:

# 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>

Or 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;
}

Should work like a charm, like a crystal clear pure Haitian Vodou.



来源:https://stackoverflow.com/questions/13647519/cant-create-zombie-process-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!