THIS IS xv6
Iām working on an assignment where we are instructed on testing an implementation of the method ps. ps prints all of the active processes, their process id, parent id, state, and name. For the first test, we are to assign 8 children to a process via the fork() function and I have done that.
The code for that test is:
int main(void) {
int ppid = getpid();
int pid = fork();
if(pid > 0){
for(int x = 0; x < 7; x++){
pid = fork();
if(pid == 0){
sleep(20);
exit();
}
}
}
if(getpid() == ppid){
ps();
}
while(wait() != -1);
exit();
}
The output is this (ignore shit and init)
1 1 init
2 1 S shit
3 2 R pstest2
4 3 Z pstest2
5 3 S pstest2
6 3 S pstest2
7 3 S pstest2
8 3 S pstest2
9 3 S pstest2
10 3 S pstest2
11 3 S pstest2
In this test I made one process with 8 direct children. For the next process we have to make it so the parent forks a single child, which forks a single child, and so on until there are 8 processes. Looks like this
O
\
O
\
O...
I have no clue how to actually to use fork() to make a current child product a child as it only I'm only able to add children to the current parent. Note: fork returns 0 when successfully forked new process. Psuedo code would be great