can't create zombie process in linux

2019-05-10 16: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条回答
Animai°情兽
2楼-- · 2019-05-10 17:11

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.

查看更多
登录 后发表回答