From 23bd6929dc88fe9def4d211d244812317ed2a2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 11 Jan 2007 17:00:30 +0000 Subject: [PATCH] Added the last of the companion fibo test apps (based on fork/exec - this is just another test that doesn't work on BeOS, dunno why, but who cares...). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19767 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/system/kernel/Jamfile | 2 +- src/tests/system/kernel/fibo_exec.cpp | 103 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/tests/system/kernel/fibo_exec.cpp diff --git a/src/tests/system/kernel/Jamfile b/src/tests/system/kernel/Jamfile index 86ab8f2397..94c98c715f 100644 --- a/src/tests/system/kernel/Jamfile +++ b/src/tests/system/kernel/Jamfile @@ -5,7 +5,7 @@ UseHeaders $(TARGET_PRIVATE_KERNEL_HEADERS) : true ; SimpleTest fibo_load_image : fibo_load_image.cpp ; SimpleTest fibo_fork : fibo_fork.cpp ; -#SimpleTest fibo_exec : fibo_exec.cpp ; +SimpleTest fibo_exec : fibo_exec.cpp ; SimpleTest port_close_test_1 : port_close_test_1.cpp ; SimpleTest port_close_test_2 : port_close_test_2.cpp ; diff --git a/src/tests/system/kernel/fibo_exec.cpp b/src/tests/system/kernel/fibo_exec.cpp new file mode 100644 index 0000000000..c39de62c61 --- /dev/null +++ b/src/tests/system/kernel/fibo_exec.cpp @@ -0,0 +1,103 @@ +/* + * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Copyright 2002, Manuel J. Petit. All rights reserved. + * Distributed under the terms of the NewOS License. + */ + + +#include + +#include +#include +#include +#include +#include + + +static void +usage(char const *app) +{ + printf("usage: %s [-s] ###\n", app); + exit(-1); +} + + +int +main(int argc, char *argv[]) +{ + bool silent = false; + int num = 0; + int result; + + switch (argc) { + case 2: + num = atoi(argv[1]); + break; + case 3: + if (!strcmp(argv[1], "-s")) { + num = atoi(argv[2]); + silent = true; + break; + } + // supposed to fall through + + default: + usage(argv[0]); + break; + } + + if (num < 2) { + result = num; + } else { + pid_t childA = fork(); + if (childA == 0) { + // we're the child + char buffer[64]; + char* args[]= { argv[0], "-s", buffer, NULL }; + + snprintf(buffer, sizeof(buffer), "%d", num - 1); + if (execv(args[0], args) < 0) { + fprintf(stderr, "Could not create exec A: %s\n", strerror(errno)); + return -1; + } + } else if (childA < 0) { + fprintf(stderr, "fork() failed for child A: %s\n", strerror(errno)); + return -1; + } + + pid_t childB = fork(); + if (childB == 0) { + // we're the child + char buffer[64]; + char* args[]= { argv[0], "-s", buffer, NULL }; + + snprintf(buffer, sizeof(buffer), "%d", num - 2); + if (execv(args[0], args) < 0) { + fprintf(stderr, "Could not create exec B: %s\n", strerror(errno)); + return -1; + } + } else if (childB < 0) { + fprintf(stderr, "fork() failed for child B: %s\n", strerror(errno)); + return -1; + } + + status_t status; + while (wait_for_thread(childA, &status) == B_INTERRUPTED) + ; + result = status; + + while (wait_for_thread(childB, &status) == B_INTERRUPTED) + ; + result += status; + } + + if (silent) { + return result; + } else { + printf("%d\n", result); + return 0; + } +} +