zombie to exist after the termination of main program..

main()
{
pid_t child;
child=fork();

if(child > 0)

{sleep(60);
}

else
{exit(0);
}

}

the above code will create zombie process,which will be adopted by init as soon as parent process will dies.Can any one gimme a code or an alogrithm to keep this zombie proocess alive even after its parent dies...i.e to stop init from adopting it after the termination of main programme

Fork again. Keep the child process running. When the original process exits, the child process will keep the grandchild process in a zombie state when it exits.

Otherwise no, there's no way to keep an orphan process from being adopted by init.

thanx for ur reply sir , i had already done my assignment with this logic....:slight_smile:
but i think the child process was supposed to stay as zombie after the termination of main program not its grandchild......:confused:
this is possible through kerenl level programming....

Why on Earth would anyone want a zombie to persist like this? The zombie's only purpose is perserve the exit code of the process so that the parent can obtain it. Once the parent is dead, there is no purpose left for the zombie. If you disable the reaping of zombies on a systemwide basis, the process table will fill and you will be unable to fork new processes. If that is your goal, you can easily fill the process table with living processes rather than zombies. Just write a "while(1) fork();" program and run it as root. Fair warning: it will be rather hard to recover from that and you may need to cycle power to the system. If you must fill the process table with zombies you will need to rewrite init to not reap zombies. There will be no way to recover from that. You will need to recycle power each time the process table fills. If you modify init to not reap only a few certain zombies, you will be able to use the system for a longer period before the process table fills.

I would really like to hear an explanation as why someone wants unreapable zombies. Is any benefit expected from an unreapable zombie?

thanx for ur explanations.....!!
i m not interested in making zombies unreapable..all i wanted to hav an idea that whether its possible or not to do it...

If u look at the code above in first post,i hav made parent process to sleep for 60secs
If i run this code(i.e ./a.out) n then execute "ps -eaf" on the terminal within 60secs... i will see
1)./a.out
2)[a.out <defunct>]

If i again execute "ps -eaf" after 60secs i wont see those two output....
bcoz its no longer exist ,after 60secs the main program has terminated and [a.out <defunct>] has been adopted by init .

i want these two output to remain there even after the parent process has terminated i.e if i run "ps -eaf " after 60secs the same output should be seen ..

actually this is my lab assignment questions which goes as follow

"Write a Unix program that will create a zombie process that exists after
the main program terminates."
so in order to solve this i was seeking help in different forums..

This is an example why Homework is not allowed on the Forums. Apparently nobody here could figure out why any sane person would do that.

Do not post homework questions.

Do not post homework questions. Closing the thread.