简体   繁体   中英

can't create zombie process in 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;
}

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM