if(fork) and if(!fork) – how do they work?

  Kiến thức lập trình

Let’s say we have the following:

pid_t pid;
pid = fork();
if(pid == 0) {
        if(fork()) printf("A");
        else printf("B");
        printf("C");
}
else {
        if(!fork()) printf("A");
        else if (fork()) printf("B");
        else printf("C");
}
    

How many A’s, B’s and C’s are going to be printed?

I’m a little bit confused with how fork() works inside “if” and whether I’m in parent or child process.

New contributor

RhoThanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT