c - Fork() call process? -


suppose have code:

int main () {      int i, r;     = 5;     printf("%d\n", i);     r = fork();     if (r > 0) {         = 6;     }     else if (r == 0) {         = 4;     }     printf("%d\n", i);     } 

i wondering forked child process start executing either beginning or called. reason ask because on own system output 5,6,4 means starts called typing in http://ideone.com/rhppmp 5,6,5,4?

one process calls fork, 2 processes return (errors notwithstanding). that's how works. child starts next "line" (technically starts assignment r).

what you're seeing here has buffering. in online case, you'll find it's using full buffering standard output, meaning initial 5 hasn't yet been flushed output device.

hence, @ fork, both parent , child have , both flush @ point.

for line buffered case, parent flushes on newline line no longer in buffer @ fork.

the rules explicit. standard output set line buffered if output device known terminal. in cases redirect file, or catch output in online environment can sanitise browser output, it'll buffered.

hence why you're seeing difference.

it's idea flush output handles (with fflush) before forking.


Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -