From a52543acd2b6713f679f91f8f92bc0744b303f94 Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Sat, 17 Dec 2016 14:05:57 +0100 Subject: [PATCH] Improve noux fork test (grand child, waitpid) The test now forks twice - the parent forks a child and the child itself forks a grand child. Both, parent and child, wait for termination of their forked process with waitpid(). Additionally, the run script now checks for exit of the parent (not any noux process). --- repos/ports/run/noux_fork.run | 12 +++++--- repos/ports/src/test/noux_fork/test.cc | 39 +++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/repos/ports/run/noux_fork.run b/repos/ports/run/noux_fork.run index d831b8e2dd..3f0667ae1f 100644 --- a/repos/ports/run/noux_fork.run +++ b/repos/ports/run/noux_fork.run @@ -30,9 +30,12 @@ install_config { - - - + + + + + + @@ -46,4 +49,5 @@ build_boot_image { append qemu_args " -nographic " -run_genode_until "child.*exited with exit value 0.*\n" 20 +run_genode_until "--- parent done ---.*\n" 20 +run_genode_until "child.*exited.*\n" 5 [output_spawn_id] diff --git a/repos/ports/src/test/noux_fork/test.cc b/repos/ports/src/test/noux_fork/test.cc index ecebd64474..5d35f07038 100644 --- a/repos/ports/src/test/noux_fork/test.cc +++ b/repos/ports/src/test/noux_fork/test.cc @@ -7,6 +7,7 @@ #include #include #include +#include enum { MAX_COUNT = 1000 }; @@ -22,11 +23,38 @@ int main(int, char **) printf("pid %d: fork returned %d\n", getpid(), fork_ret); + /* child */ if (fork_ret == 0) { printf("pid %d: child says hello\n", getpid()); - for (int j = 0; j < MAX_COUNT; j++) { - printf("pid %d: child j = %d\n", getpid(), j); + + pid_t fork_ret = fork(); + if (fork_ret < 0) { + printf("Error: fork returned %d, errno=%d\n", fork_ret, errno); + return -1; } + + printf("pid %d: fork returned %d\n", getpid(), fork_ret); + + /* grand child */ + if (fork_ret == 0) { + printf("pid %d: grand child says hello\n", getpid()); + + for (int k = 0; k < MAX_COUNT; k++) { + printf("pid %d: grand child k = %d\n", getpid(), k); + } + + return 0; + }; + + for (int j = 0; j < MAX_COUNT; j++) { + printf("pid %d: child j = %d\n", getpid(), j); + } + + if (fork_ret != 0) { + printf("pid %d: child waits for grand-child exit\n", getpid()); + waitpid(fork_ret, nullptr, 0); + } + return 0; } @@ -34,9 +62,12 @@ int main(int, char **) getpid(), fork_ret); for (int i = 0; i < MAX_COUNT; ) { - printf("pid %d: parent i = %d\n", getpid(), i++); + printf("pid %d: parent i = %d\n", getpid(), i++); } - pause(); + printf("pid %d: parent waits for child exit\n", getpid()); + waitpid(fork_ret, nullptr, 0); + + printf("--- parent done ---\n"); return 0; }