-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrowd_fork_no_procs.c
67 lines (51 loc) · 1001 Bytes
/
crowd_fork_no_procs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "types.h"
#include "user.h"
#include "param.h"
#define TEST_NAME "crowd_fork_no_procs"
#include "318_test-tapish.h"
/* Authors: Bailey & Sam */
/* Ensures fork fails gracefully if there are no procs left */
#define FORKS (NPROC - 3)
void recursive_fork(int depth);
int test_fork(void);
int
main(void)
{
TEST_STRT(FORKS + 1);
if(test_fork() == 0) {
recursive_fork(1);
exit();
} else {
wait();
}
TEST_FINI("fork with no procs");
exit();
}
void
recursive_fork(int depth)
{
int pid;
TEST_FINI("fork %d", depth);
if(depth == FORKS) {
TEST_FAIL_IF(fork() >= 0, "fork %d succeeded, but shouldn't have", depth);
exit();
} else if(depth < FORKS) {
depth++;
TEST_FAIL_IF((pid = fork()) < 0, "fork %d failed", depth);
if(pid == 0) {
recursive_fork(depth);
exit();
} else {
wait();
exit();
}
}
}
int
test_fork(void)
{
int pid;
pid = fork();
TEST_EXIT_IF(pid < 0, "fork failed");
return pid;
}