forked from SeanBarber/homework7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro_processes.c
47 lines (43 loc) · 939 Bytes
/
macro_processes.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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#define NUMTASKS 25000
#define NANOSECONDS_PER_SECOND 1E9
#define FIBNUM 26
int fib(int x) {
if (x == 0)
return 0;
else if (x == 1)
return 1;
return fib(x - 1) + fib(x - 2);
}
int main(){
int wpid, status, retval;
for (int i = 0; i < NUMTASKS; i++) {
retval = fork();
if (retval == -1){
// over our process limit: wait for at least one process to finish and try again
wpid = wait(&status);
if (wpid < 0){
printf("waited when no children to wait for\n");
exit(1);
}
// try this iteration of the loop over again
i--;
continue;
}
if (retval == 0){
fib(FIBNUM);
exit(0);
}
else{
continue;
}
}
// wait for all worker processes to finish
while ((wpid = wait(&status)) > 0);
return 0;
}