forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceChild.c
35 lines (30 loc) · 917 Bytes
/
traceChild.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
#include "types.h"
#include "stat.h"
#include "user.h"
int main(int argc, char *argv[]) {
int i;
int num_children = 3;
if (argc > 1) {
num_children = atoi(argv[1]); // Accept the number of child processes
}
printf(1, "Parent pid: %d\n", getpid());
for (i = 0; i < num_children; i++) {
int pid = fork();
if (pid == 0) {
// Child process
printf(1, "Child pid: %d\n", getpid());
printf(1, "Child %d: Executing 'echo hello'\n", getpid());
exec("echo", (char *[]){"echo", "hello", 0});
printf(1, "Child %d: exec failed\n", getpid());
exit();
} else if (pid > 0) {
// Parent process
wait();
} else {
printf(2, "fork failed\n");
exit();
}
}
printf(1, "All children finished. Exiting parent process.\n");
exit();
}