c - Why does this code using fork() work? -
i've code executes code depending of if active process parent or child process in infinite loop:
pid_t childpid; childpid=fork(); while (1) { if (childpid >=0) { if (childpid==0) { [do child process stuff] } else { [do parent process stuff] } } else { printf("\n fork failed, quitting!!!!!\n"); return 1; } }
code simple there's 1 big thing on me don't understand how happens although have guess:
if not taking consideration we're creating 2 processes looks childpid being reasigned don't think makes sense.
so guess, fork creates childpid each process, returning 0 parent process , pid child process, though syntax makes me think should return 1 result , assign chilpid.
is guess correct or there other thing involved?
thank you.
so guess, fork creates childpid each process, returning 0 parent process , pid child process, though syntax makes me think should return 1 result , assign chilpid.
exactly that. fork reference manual:
return value: on success, pid of child process returned in parent, , 0 returned in child. on failure, -1 returned in parent, no child process created, , errno set appropriately.
so
is guess
why guess if precisely defined in posix specification?
Comments
Post a Comment