2 * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Copyright 2002, Manuel J. Petit. All rights reserved.
6 * Distributed under the terms of the NewOS License.
20 usage(char const *app
)
22 printf("usage: %s [-s] ###\n", app
);
28 main(int argc
, char *argv
[])
39 if (!strcmp(argv
[1], "-s")) {
44 // supposed to fall through
54 pid_t childA
= fork();
58 char* args
[]= { argv
[0], "-s", buffer
, NULL
};
60 snprintf(buffer
, sizeof(buffer
), "%d", num
- 1);
61 if (execv(args
[0], args
) < 0) {
62 fprintf(stderr
, "Could not create exec A: %s\n", strerror(errno
));
65 } else if (childA
< 0) {
66 fprintf(stderr
, "fork() failed for child A: %s\n", strerror(errno
));
70 pid_t childB
= fork();
74 char* args
[]= { argv
[0], "-s", buffer
, NULL
};
76 snprintf(buffer
, sizeof(buffer
), "%d", num
- 2);
77 if (execv(args
[0], args
) < 0) {
78 fprintf(stderr
, "Could not create exec B: %s\n", strerror(errno
));
81 } else if (childB
< 0) {
82 fprintf(stderr
, "fork() failed for child B: %s\n", strerror(errno
));
86 status_t status
, returnValue
= 0;
88 status
= wait_for_thread(childA
, &returnValue
);
89 } while (status
== B_INTERRUPTED
);
94 fprintf(stderr
, "wait_for_thread(%ld) A failed: %s\n", childA
, strerror(status
));
97 status
= wait_for_thread(childB
, &returnValue
);
98 } while (status
== B_INTERRUPTED
);
101 result
+= returnValue
;
103 fprintf(stderr
, "wait_for_thread(%ld) B failed: %s\n", childB
, strerror(status
));
109 printf("%d\n", result
);