00001 /* 00002 waitpid example 00003 AUP2, Sec. 5.08 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include "statuspr.h" 00023 00024 static bool wait_and_display(void); 00025 00026 /*[waitpid]*/ 00027 int main(void) 00028 { 00029 pid_t pid; 00030 00031 /* Case 1: Explicit call to _exit */ 00032 if (fork() == 0) /* child */ 00033 _exit(123); 00034 /* parent */ 00035 ec_false( wait_and_display() ) 00036 00037 /* Case 2: Termination by kernel */ 00038 if (fork() == 0) { /* child */ 00039 int a, b = 0; 00040 00041 a = 1 / b; 00042 _exit(EXIT_SUCCESS); 00043 } 00044 /* parent */ 00045 ec_false( wait_and_display() ) 00046 00047 /* Case 3: External signal */ 00048 if ((pid = fork()) == 0) { /* child */ 00049 sleep(100); 00050 _exit(EXIT_SUCCESS); 00051 } 00052 /* parent */ 00053 ec_neg1( kill(pid, SIGHUP) ) 00054 ec_false( wait_and_display() ) 00055 00056 exit(EXIT_SUCCESS); 00057 00058 EC_CLEANUP_BGN 00059 exit(EXIT_FAILURE); 00060 EC_CLEANUP_END 00061 } 00062 00063 static bool wait_and_display(void) 00064 { 00065 pid_t wpid; 00066 int status; 00067 00068 ec_neg1( wpid = waitpid(-1, &status, 0) ) 00069 display_status(wpid, status); 00070 return true; 00071 00072 EC_CLEANUP_BGN 00073 return false; 00074 EC_CLEANUP_END 00075 } 00076 /*[]*/