process - Trouble Understanding Fork Logic -


can me understand happening in segment of code? having trouble understanding why output how is. output is:

0 1 2 3 4

3

2

1

0

int main() {    int i;     (i = 0; < 5 && !fork(); i++) {       fflush(stdout);       printf("%d ", i);    }    wait(null);     printf("\n");    return 0; } 

two things here:

first, fork() return 0 in child process while returns non 0 pid parent process.

second, short circuit of &&.

so in beginning of first process (p0), runs i < 5 && !fork(). i = 0 , process created (p1). p0, test !fork() fails, ends loop , waiting child end. p1, test succeeds, , print out 0, increment i 1, create process p2 , goes out loop p0 did.

because of short circuiting, when i equals 5, no more fork called.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -