Add a test for wait4().

This commit is contained in:
Jérôme Duval
2017-06-19 18:33:15 +02:00
parent ed8fe1c762
commit cffce774dd
2 changed files with 67 additions and 0 deletions
+1
View File
@@ -3,5 +3,6 @@ SubDir HAIKU_TOP src tests libs bsd ;
UseHeaders [ FDirName $(HAIKU_TOP) headers compatibility bsd ] : true ;
SimpleTest forkpty_test : forkpty.c : libbsd.so ;
SimpleTest wait4_test : wait4_test.cpp : libbsd.so ;
+66
View File
@@ -0,0 +1,66 @@
/*
* Copyright 2007, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#define _BSD_SOURCE
#include <sys/wait.h>
#include <unistd.h>
/*!
wait() should wait only once. If any argument is given, waitpid() should return
an error (and errno to ECHILD), since there is no child with that process group ID.
*/
int
child2()
{
sleep(2);
return 2;
}
//! exits before child 2
int
child1()
{
setpgrp();
// put us into a new process group
pid_t child = fork();
if (child == 0)
return child2();
sleep(1);
return 1;
}
int
main(int argc, char** argv)
{
bool waitForGroup = argc > 1;
pid_t child = fork();
if (child == 0)
return child1();
struct rusage usage;
pid_t pid;
do {
int childStatus = -1;
pid = wait4(-1, &childStatus, 0, &usage);
printf("wait4() returned %ld (%s), child status %d\n",
pid, strerror(errno), childStatus);
} while (pid >= 0);
return 0;
}