1/* 2 * Copyright 2007, Axel D��rfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7#include <errno.h> 8#include <stdio.h> 9#include <string.h> 10#include <sys/wait.h> 11#include <unistd.h> 12 13 14/*! 15 waitpid() should wait only once. 16*/ 17 18 19int 20child2() 21{ 22 printf("child 2 1. parent id = %ld\n", getppid()); 23 sleep(2); 24 printf("child 2 2. parent id = %ld\n", getppid()); 25 return 2; 26} 27 28 29//! exits before child 2 30int 31child1() 32{ 33 printf("child 1 process group: %ld\n", getpgrp()); 34 35 pid_t child = fork(); 36 if (child == 0) 37 return child2(); 38 39 sleep(1); 40 return 1; 41} 42 43 44int 45main() 46{ 47 printf("main process group: %ld\n", getpgrp()); 48 pid_t child = fork(); 49 if (child == 0) 50 return child1(); 51 52 pid_t pid; 53 do { 54 int childStatus = -1; 55 pid = waitpid(0, &childStatus, 0); 56 printf("waitpid() returned %ld (%s), child status %d\n", pid, strerror(errno), childStatus); 57 } while (pid >= 0); 58 59 return 0; 60} 61 62