-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_test.c
More file actions
79 lines (63 loc) · 1.24 KB
/
ml_test.c
File metadata and controls
79 lines (63 loc) · 1.24 KB
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
68
69
70
71
72
73
74
75
76
77
78
#include "types.h"
#include "stat.h"
#include "user.h"
#define NUM_LOOP 20
#define NUM_YIELD 50000
#define NUM_SLEEP 10
#define NUM_THREAD 4
int parent;
int fork_children()
{
int i, p;
for (i = 0; i < NUM_THREAD; i++)
if ((p = fork()) == 0)
{
sleep(10);
return getpid();
}
return parent;
}
void exit_children()
{
if (getpid() != parent)
exit();
while (wait() != -1);
}
int main(int argc, char *argv[])
{
int i, pid;
parent = getpid();
printf(1, "Multilevel test start\n");
printf(1, "[Test 1] without yield / sleep\n");
pid = fork_children();
if (pid != parent)
{
for (i = 0; i < NUM_LOOP; i++)
printf(1, "Process %d\n", pid);
}
exit_children();
printf(1, "[Test 1] finished\n");
printf(1, "[Test 2] with yield\n");
pid = fork_children();
if (pid != parent)
{
for (i = 0; i < NUM_YIELD; i++)
yield();
printf(1, "Process %d finished\n", pid);
}
exit_children();
printf(1, "[Test 2] finished\n");
printf(1, "[Test 3] with sleep\n");
pid = fork_children();
if (pid != parent)
{
for (i = 0; i < NUM_SLEEP; i++)
{
printf(1, "Process %d\n", pid);
sleep(10);
}
}
exit_children();
printf(1, "[Test 3] finished\n");
exit();
}