diff --git a/src/tests/system/kernel/Jamfile b/src/tests/system/kernel/Jamfile index a379b1c218..a8016225c7 100644 --- a/src/tests/system/kernel/Jamfile +++ b/src/tests/system/kernel/Jamfile @@ -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 ; diff --git a/src/tests/system/kernel/wait_test.c b/src/tests/system/kernel/wait_test_1.c similarity index 100% rename from src/tests/system/kernel/wait_test.c rename to src/tests/system/kernel/wait_test_1.c diff --git a/src/tests/system/kernel/wait_test_2.cpp b/src/tests/system/kernel/wait_test_2.cpp new file mode 100644 index 0000000000..6d12728deb --- /dev/null +++ b/src/tests/system/kernel/wait_test_2.cpp @@ -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 +#include +#include +#include +#include + + +/*! + 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; +} + diff --git a/src/tests/system/kernel/wait_test_3.cpp b/src/tests/system/kernel/wait_test_3.cpp new file mode 100644 index 0000000000..444a1b4a84 --- /dev/null +++ b/src/tests/system/kernel/wait_test_3.cpp @@ -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 +#include +#include +#include +#include + + +/*! + 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; +} + diff --git a/src/tests/system/kernel/wait_test_4.cpp b/src/tests/system/kernel/wait_test_4.cpp new file mode 100644 index 0000000000..bc852ddad9 --- /dev/null +++ b/src/tests/system/kernel/wait_test_4.cpp @@ -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 +#include +#include +#include +#include + + +/*! + 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; +} +