Added some more tests for wait() and waitpid(). Looks like my implementation is

broken (see bug #996), but less broken than Be's.
Also the Linux implementation is not perfect, as test 4 reveals: it waits for a
child with a certain process group, even if there is no child around anymore (because
the last child changed its process group. However, it will still return (and report
the correct error code) once this child exits.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20009 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2007-01-29 13:40:52 +00:00
parent 1beeb96080
commit 56a16fd0f9
5 changed files with 197 additions and 1 deletions

View File

@ -33,7 +33,10 @@ SimpleTest lock_node_test :
: be
;
SimpleTest wait_test : wait_test.c ;
SimpleTest wait_test_1 : wait_test_1.c ;
SimpleTest wait_test_2 : wait_test_2.cpp ;
SimpleTest wait_test_3 : wait_test_3.cpp ;
SimpleTest wait_test_4 : wait_test_4.cpp ;
SubInclude HAIKU_TOP src tests system kernel cache ;
#SubInclude HAIKU_TOP src tests system kernel disk_device_manager ;

View File

@ -0,0 +1,66 @@
/*
* Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#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();
pid_t pid;
do {
int childStatus = -1;
if (waitForGroup)
pid = waitpid(0, &childStatus, 0);
else
pid = wait(&childStatus);
printf("wait() returned %ld (%s), child status %d\n",
pid, strerror(errno), childStatus);
} while (pid >= 0);
return 0;
}

View File

@ -0,0 +1,61 @@
/*
* Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
/*!
waitpid() should wait only once.
*/
int
child2()
{
printf("child 2 1. parent id = %ld\n", getppid());
sleep(2);
printf("child 2 2. parent id = %ld\n", getppid());
return 2;
}
//! exits before child 2
int
child1()
{
printf("child 1 process group: %ld\n", getpgrp());
pid_t child = fork();
if (child == 0)
return child2();
sleep(1);
return 1;
}
int
main()
{
printf("main process group: %ld\n", getpgrp());
pid_t child = fork();
if (child == 0)
return child1();
pid_t pid;
do {
int childStatus = -1;
pid = waitpid(0, &childStatus, 0);
printf("waitpid() returned %ld (%s), child status %d\n", pid, strerror(errno), childStatus);
} while (pid >= 0);
return 0;
}

View File

@ -0,0 +1,66 @@
/*
* Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#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(3);
return 2;
}
//! exits before child 2
int
child1()
{
pid_t child = fork();
if (child == 0)
return child2();
sleep(1);
setpgrp();
// put us into a new process group
sleep(1);
return 1;
}
int
main(int argc, char** argv)
{
bool waitForGroup = argc > 1;
pid_t child = fork();
if (child == 0)
return child1();
pid_t pid;
do {
int childStatus = -1;
if (waitForGroup)
pid = waitpid(0, &childStatus, 0);
else
pid = wait(&childStatus);
printf("wait() returned %ld (%s), child status %d\n",
pid, strerror(errno), childStatus);
} while (pid >= 0);
return 0;
}