I am studying about system programming system calls. I have a code block in my assignment (given below). The question asks me to how many A,B or C will be printed. My question is what is the meaning of if(pid == 0)? I guess if(pid == 0) means false, so I analyse that 2 x A and 2 x B will be printed. Am I write or? Second question is does pid2 = fork() executes main again?
int main() { int pid,pid2; int i; pid = fork(); printf("A\n"); if (pid == 0) pid2=fork(); if (pid2) printf("B\n"); printf("C\n"); return 0; }
pid2initially has an indeterminate value and is never set in the original parent. The original parent may or may not print 'B'. Assuming it is you getABCABCCthough the order may be different depending on which processes gets processor time.AC!