int main(void)
{
pid_t pid1, pid2, pid3, child;
int val1 = 100, val2 = 200;
int status;
switch (pid1 = fork()) { /* child one */
case -1 : err_sys("Child1: fork");
case 0 : val1 = val1 + 1;
printf("Child1 (%d): val1 = %d\n", getpid(), val1);
status = status / 0;
exit(0);
}
switch (pid2 = vfork()) { /* child two */
case -1 : err_sys("vfork");
case 0 : val2 = val2 + 1;
printf("Child2 (%d): val2 = %d\n", getpid(), val2);
execl("/bin/ps", "ps", "-gux", (char *) 0);
err_sys("Child2: exec");
}
while ((child = waitpid(pid2, &status, WNOHANG)) == 0)
sleep(1);
if (child == -1)
err_sys("Parent: waitpid");
else
exit_report(child, status);
if ((child = wait(&status)) == -1)
err_sys("Parent: waitpid");
else
exit_report(child, status);
printf("Parent: val1 = %d val2 = %d\n", val1, val2);
switch (pid3 = vfork()) { /* child three */
case -1 : err_sys("Child3: fork");
case 0 : printf("Child3 (%d): what have I done?!\n", getpid());
exit(20);
}
printf("Parent: I'm done\n");
exit(0);
}
[1090] csb:Ex_vfork % vfork.bin
Child1 (19351): val1 = 101
Child2 (19352): val2 = 201
USER PID %CPU %MEM SZ RSS TT STAT START TIME COMMAND
park 19352 7.7 0.6 256 568 q6 R 16:32 0:00 ps -gux
park 19350 7.7 0.3 40 272 q6 S 16:32 0:00 vfork.bin
park 14528 0.0 0.5 384 504 q6 S May 23 0:54 -tcsh (tcsh)
park 19351 0.0 0.0 0 0 q6 Z Apr 8 0:00
normal termination: process 19352 status 0
abnormal termination: process 19351 signal 8
Parent: val1 = 100 val2 = 201
Child3 (19354): what have I done?!
[1091] csb:Ex_vfork %