-2

What would be the output? I'm confused, it's 2 or 3 but which one I'm not sure. Can you help?

main() 
{
    printf("hello\n");

    if(fork() == 0)
        printf("hello\n");
}
Xaqron
  • 29,931
  • 42
  • 140
  • 205

1 Answers1

2

if statement is gonna be evaluated after forking so each process would run it with it's own return value which is zero for child process and non-zero (PID) for parent so there would be 1 hello at this point.

Plus the first hello at the top of your code you get total of 2 hellos at terminal.

Xaqron
  • 29,931
  • 42
  • 140
  • 205