Please, my doctor in the university is too lazy to solve this exam question, can anyone help me?
#include <stdio.h>
#include <unistd.h>
int main() {
int i;
for(i=0;i<3;i++)
if(fork()) i++;
while(1);
return 1;
}
The question is: how many processes does this program generate?
I drew the tree of processes and I think this program will not go to an end. So there are infinite number of processes but I'm not sure.
The while loops that you have are not infinite as they will eventually return a value and exit. However, I think that by process you mean a different thread that would run separately in the memory.
EDIT: In this case you will generate a thread by calling the
fork()
method for the first time, which will return true and the if statement will execute. At this point i will become 1 which would be incremented by additional one at the end of the for loop so the next iteration will have i = 2. At this point you will call fork once more and then the loop will exit, so the answer is 2 threads at the end.