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 <inttypes.h> 9#include <stdio.h> 10#include <string.h> 11#include <sys/resource.h> 12#include <sys/wait.h> 13#include <unistd.h> 14 15 16/*! 17 wait() should wait only once. If any argument is given, waitpid() should return 18 an error (and errno to ECHILD), since there is no child with that process group ID. 19*/ 20 21 22int 23child2() 24{ 25 sleep(2); 26 return 2; 27} 28 29 30//! exits before child 2 31int 32child1() 33{ 34 setpgrp(); 35 // put us into a new process group 36 37 pid_t child = fork(); 38 if (child == 0) 39 return child2(); 40 41 sleep(1); 42 return 1; 43} 44 45 46int 47main(int argc, char** argv) 48{ 49 bool waitForGroup = argc > 1; 50 51 pid_t child = fork(); 52 if (child == 0) 53 return child1(); 54 55 struct rusage usage; 56 pid_t pid; 57 do { 58 memset(&usage, 0, sizeof(usage)); 59 int childStatus = -1; 60 pid = wait4(-1, &childStatus, 0, &usage); 61 printf("wait4() returned %" PRId32 " (%s), child status %" PRId32 62 ", kernel: %ld.%06" PRId32 " user: %ld.%06" PRId32 "\n", 63 pid, strerror(errno), childStatus, usage.ru_stime.tv_sec, 64 usage.ru_stime.tv_usec, usage.ru_utime.tv_sec, 65 usage.ru_utime.tv_usec); 66 } while (pid >= 0); 67 68 return 0; 69} 70 71