Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Processes II<br />
9. PROCESSES (II)<br />
FORK<br />
! /* fork.c */<br />
#include <br />
main()<br />
{<br />
int pid;<br />
printf("original process <strong>with</strong> PID %d and PPID %d\n", getpid(), getppid());<br />
pid = fork(); /* duplicate process */<br />
}<br />
if (pid != 0) {/* parent */<br />
printf("parent process <strong>with</strong> PID %d and PPID %d\n",<br />
getpid(), getppid());<br />
printf("child's PID is %d\n", pid);<br />
}<br />
else{ /* child */<br />
printf("child process <strong>with</strong> PID %d and PPID %d\n",<br />
getpid(), getppid());<br />
}<br />
printf("PID %d terminates\n", getpid());<br />
original process <strong>with</strong> PID 134 and PPID 120<br />
parent process <strong>with</strong> PID 134 and PPID 120<br />
child's PID is 135<br />
child process <strong>with</strong> PID 135 and PPID 134<br />
PID 135 terminates<br />
PID 134 terminates<br />
! /* orphan.c */<br />
#include <br />
main()<br />
{<br />
int pid;<br />
printf("original process <strong>with</strong> PID %d and PPID %d\n", getpid(), getppid());<br />
}<br />
pid = fork(); /* duplicate process */<br />
if (pid != 0){ /* parent */<br />
printf("parent process <strong>with</strong> PID %d and PPID %d\n",<br />
getpid(), getppid());<br />
printf("child's PID is %d\n", pid);<br />
}<br />
else { /* child */<br />
sleep(5); /* terminate parent first */<br />
printf("child process <strong>with</strong> PID %d and PPID %d\n",<br />
getpid(), getppid());<br />
}<br />
printf("PID %d terminates\n", getpid());<br />
original process <strong>with</strong> PID 154 and PPID 140<br />
parent process <strong>with</strong> PID 154 and PPID 140<br />
child's PID is 155<br />
PID 154 terminates<br />
parent dies<br />
child process <strong>with</strong> PID 155 and PPID 1 init adopts child<br />
PID 155 terminates<br />
122