I'v some questions about zombie processes
thanks in advance
A zombie process is a process in its terminated state. This usually happens in a program that has parent-child functions. After a child function has finished execution, it sends an exit status to its parent function.
A zombie process is a process whose execution is completed but it still has an entry in the process table. Zombie processes usually occur for child processes, as the parent process still needs to read its child's exit status.
If the parent process is still active A strace command stores all system calls and signals made by a process. Additionally, you can also kill the zombie process by sending the SIGCHLD signal to the parent process to make the parent process exit cleanly with its zombie process.
An orphan process is formed when it's parent dies while the process continues to execute, while zombie process is a process which has terminated but it's entry is there in the system.
A zombie process is purely a pid and exit status value. The pid cannot be released because the resource (the pid) "belongs to" the parent. If it were released, another process might get assigned the same pid, and then the parent could end up sending signals to an unrelated process; even if the parent first waited to determine if the child had exited, there would be no way to avoid race conditions.
-- what the benefits from zombie process concept?
A zombie process is just a pid, an exit status, and some accounting information that stays around until a parent uses one of the wait
family of system calls to get its final status. Until a parent calls wait
the child's process ID must stay marked as used so that no other process can be assigned it. If another process were to get assigned a recycled pid it would be difficult to tell the difference between it and previous processes that had that same pid. Once wait
is called by the parent and returns a final exit status it can be assumed that no one will go looking for the child at that pid again, so the pid may now be reused.
(I think on Linux if a parent leaves SIGCHLD as SIG_IGN the kernel will not keep zombies around, but that re-registering SIGCHLD's disposition as SIG_IGN does not have the same effect)
-- know that the kernel keeps (PID,termination status, resource usage information) for zombie process what's the meaning of "resource usage information"
Some of this information is what running a program as:
time my_program
will report. These values are usually reported in the siginfo structure for SIGCHLD (which isn't exactly a call to wait
) but also available from a call to the waitid
form of systme call (on some systems). Look at man sigaction
for info about this structure.
-- how zombie's PPID() = 1 and it still zombie , (init reaps Zombies because it wait() by default)
A zombie whose ppid = 1 should not stay a zombie for very long because init should reap it pretty quickly. A process will remain a zombie from a point soon after it dies (either via exit
or by an unhanded signal that kills it) until its parent calls wait
and gets it's final status. This means that even if init does nothing but call init over and over there could be a small amount of time where a process may show up as a zombie. If processes show up as children of init (0=ppid) for long amounts of time (seconds) then something is probably wrong.
-- can any one write some C code to make a zombie it's parent is Init?
This isn't clear, but I think you want:
pid_t f = fork();
if (f > 0) {
exit(0); // this is the parent dying, so the child will be an orphan
// and get adopted by init
} else if (f == 0) {
sleep(100); // This is the child doing something that takes enough time for
// its parent to commit suicide (exit(0)) and then for you to
// observe that it has now been adopted by init
exit(0); // And now it dies as well, so init should reap its status, but
// it may be a zombie for a short amount of time first.
} else /* error condition would be handled here */
-- can zombies refusing to release some lock on memory??
Zombies can't hold onto much of anything. They lose all of their memory pages, open file handles, ...etc. Pretty much everything the operating system can figure out how to free up should get freed. It would be a bug not to, but remember that the OS has to know that it is something that is supposed to be freed. It is very easy to create resources in user space that should be freed when a program dies that the OS doesn't know are supposed to be freed.
If you are interested in seeing the zombie process within a list of running processes use this:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
pid_t p = fork(); // creates child process and stores the returned PID
if (p != 0) // executed by parent process
{ sleep(1); /* the child process terminates while the parent process sleeps,
the child then becomes a zombie process because the returned status
of the terminated child process isn't checked via a wait() */
system("ps -eo pid,ppid,stat,cmd"); // prints a list of processes in your terminal
}
else // executed by child process
{
exit(0); // the child process terminates immediately
}
return 0;
}
You can identify the zombie process by the Z+ in the list:
NOTE: you will have to modify the code if you are using windows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With