-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.c
42 lines (34 loc) · 1.03 KB
/
q1.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
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
void q1_tree(int right_depth){
int parent_pid = (int) getpid(); //gets parent ID to show in output
int rc = fork();
if(rc<0){
exit(1);
}
else if(rc==0){ //first child created with fork system call is the left one
printf("Left child (id: %d) of %d\n", (int) getpid(), parent_pid);
}
else if(rc>0 && right_depth!=0){
int rc2 = fork(); //creates right child
if(rc2<0){
exit(1);
}
else if(rc2==0){ // right child
printf("Right child (id: %d) of %d\n", (int) getpid(), parent_pid);
q1_tree(--right_depth); //used recursive function to create every left and right child
}
else{ //parent waits for child to terminate
int wc = wait(NULL);
}
}
}
int main(){
int right_depth;
printf("Print the right depth of the tree: ");
scanf("%d", &right_depth);
printf("Root: %d\n", getpid());
q1_tree(right_depth);
}