-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c(final)
59 lines (45 loc) · 1.19 KB
/
main.c(final)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void collatzConjecture(int number, int childNum) {
printf("From Child%d, pid=%d: number=%d\n", childNum, getpid(), number);
while(number != 1){
if (number % 2 == 0) {
number = number / 2;
} else {
number = 3 * number + 1;
}
printf("From Child%d: number=%d\n", childNum, number);
}
printf("From Child%d, pid=%d: I'm done!\n", childNum, getpid());
}
int main(){
int number = 0;
pid_t child1, child2;
printf("Please enter a number: ");
scanf("%d", &number);
if(number >= 40 || number <= 0){
printf("This is an invalid number.");
return 1;
}
printf("collatz %d\n", number);
printf("This is the Parent waiting!\n");
//this is the first child process
child1 = fork();
if(child1 == 0) {
collatzConjecture(number, 1);
exit(0);
}
wait(NULL);
//this is the second child process
child2 = fork();
if(child2 == 0) {
collatzConjecture(number + 6, 2);
exit(0);
}
wait(NULL);
printf("All my Children Complete\n");
return 0;
}