int main(int argc, char** argv) {
int i = 0;
while (i < 2) {
fork();
system("ps -o pid,ppid,comm,stat");
i++;
}
return (EXIT_SUCCESS);
}
Can anyone tell me how many times ps command is executed with an explanation?
I believe the answer is 6.
in the first iteration,
fork()is called, splitting the process in 2, thus calling ps twice.in the second iteration, fork is called again in each process, so you now have 4 processes running ps.
total calls to ps: 2+4=6.