-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpos-app2.c
80 lines (66 loc) · 1.61 KB
/
mpos-app2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "mpos-app.h"
#include "lib.h"
/*****************************************************************************
* mpos-app2
*
* This application as 1024 new processes and then waits for them to
* exit. All processes print messages to the screen.
*
*****************************************************************************/
#define EXTRA
volatile int counter;
void run_child(void);
void
start(void)
{
pid_t p;
int status;
counter = 0;
while (counter < 1025) {
int n_started = 0;
// Start as many processes as possible, until we fail to start
// a process or we have started 1025 processes total.
while (counter + n_started < 1025) {
p = sys_fork();
if (p == 0)
run_child();
else if (p > 0)
n_started++;
else
break;
}
// If we could not start any new processes, give up!
if (n_started == 0)
break;
// We started at least one process, but then could not start
// any more.
// That means we ran out of room to start processes.
// Retrieve old processes' exit status with sys_wait(),
// to make room for new processes.
for (p = 2; p < NPROCS; p++){
(void) sys_wait(p);
}
}
sys_exit(0);
}
void
run_child(void)
{
int input_counter = counter;
counter++; /* Note that all "processes" share an address
space, so this change to 'counter' will be
visible to all processes. */
#ifdef EXTRA
int pid = sys_getpid();
if(!(pid&1)){ //even number
int i;
for( i = 3; i< NPROCS; i+=2)
{
int result = sys_kill(i);
}
}
#endif
app_printf("Process %d lives, counter %d!\n",
sys_getpid(), input_counter);
sys_exit(input_counter);
}