Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Processes II<br />
A process that terminates cannot leave <strong>the</strong> system until its parent accept code. If its parent is already dead, it is<br />
adopted by <strong>the</strong> "init" process<br />
If a process's parent is alive but never executes a wait() <strong>the</strong> process's will never be accepted and <strong>the</strong> process will<br />
remain a zombie.<br />
/* zombie.c */<br />
#include <br />
main()<br />
{<br />
int pid;<br />
pid = fork(); /* duplicate process */<br />
if (pid != 0) /* parent lives */<br />
{<br />
while (1)<br />
sleep(1000); /* child dies */<br />
}<br />
else {<br />
exit(2);<br />
}<br />
}<br />
ps<br />
PID TT STAT TIME COMMAND<br />
160 p1 S 0:00 -ksh<br />
170 p1 S 0:00 zombie # parent process<br />
171 p1 Z 0:00 <br />
180 p1 R 0:00 ps<br />
kill 170<br />
[1] Terminated<br />
ps<br />
PID TT STAT TIME COMMAND<br />
160 p1 S 0:00 -ksh<br />
190 p1 R 0:00 ps<br />
/* wait.c */<br />
#include <br />
main()<br />
{<br />
int pid, status, childpid;<br />
printf("original process <strong>with</strong> PID %d\n", getpid());<br />
pid = fork(); /* duplicate process */<br />
if (pid != 0)<br />
{ /* parent */<br />
printf("parent process <strong>with</strong> PID %d and PPID %d\n", getpid(), getppid());<br />
childpid = wait(&status); /* wait for child */<br />
printf("child PID %d terminated <strong>with</strong> exit code %d\n", childpid, status>>8);<br />
} else { /* child */<br />
printf("child process <strong>with</strong> PID %d and PPID %d\n", getpid(), getppid());<br />
exit(2);<br />
}<br />
printf("PID %d terminates\n", getpid());<br />
}<br />
original process <strong>with</strong> PID 190<br />
child process <strong>with</strong> PID 191 and PPID 190<br />
parent process <strong>with</strong> PID 190 and PPID 188<br />
child PID 191 terminated <strong>with</strong> exit code 2<br />
PID 191 terminates<br />
123