* Fixed fibo_load_image: it actually did not produce the fibonacci numbers.
* Implemented a fibo_fork which uses fork() instead. * Both tests can pretty easily let the kernel crash! git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19764 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -4,7 +4,7 @@ UsePrivateHeaders kernel ;
|
|||||||
UseHeaders $(TARGET_PRIVATE_KERNEL_HEADERS) : true ;
|
UseHeaders $(TARGET_PRIVATE_KERNEL_HEADERS) : true ;
|
||||||
|
|
||||||
SimpleTest fibo_load_image : fibo_load_image.cpp ;
|
SimpleTest fibo_load_image : fibo_load_image.cpp ;
|
||||||
#SimpleTest fibo_fork : fibo_fork.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_1 : port_close_test_1.cpp ;
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2007, Axel Dörfler, [email protected]. 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 <image.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
int gForked = 0;
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(char const *app)
|
||||||
|
{
|
||||||
|
printf("usage: %s ###\n", app);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
usage(argv[0]);
|
||||||
|
|
||||||
|
int num = atoi(argv[1]);
|
||||||
|
|
||||||
|
if (num < 2) {
|
||||||
|
result = num;
|
||||||
|
} else {
|
||||||
|
pid_t childA = fork();
|
||||||
|
if (childA == 0) {
|
||||||
|
// we're the child
|
||||||
|
char buffer[64];
|
||||||
|
char* args[]= { argv[0], buffer, NULL };
|
||||||
|
int argCount = 2;
|
||||||
|
gForked++;
|
||||||
|
|
||||||
|
snprintf(buffer, sizeof(buffer), "%d", num - 1);
|
||||||
|
return main(argCount, args);
|
||||||
|
} 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], buffer, NULL };
|
||||||
|
int argCount = 2;
|
||||||
|
gForked++;
|
||||||
|
|
||||||
|
snprintf(buffer, sizeof(buffer), "%d", num - 2);
|
||||||
|
return main(argCount, args);
|
||||||
|
} 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 (gForked) {
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
printf("%d\n", result);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (num < 2) {
|
if (num < 2) {
|
||||||
result = 1;
|
result = num;
|
||||||
} else {
|
} else {
|
||||||
char buffer[64];
|
char buffer[64];
|
||||||
char* args[]= { argv[0], "-s", buffer, NULL };
|
char* args[]= { argv[0], "-s", buffer, NULL };
|
||||||
|
|||||||
Reference in New Issue
Block a user