Monday, 15 August 2011

c++ - Child and Parent pid with fork(); -



c++ - Child and Parent pid with fork(); -

i'm trying create programme create 9 kid process, utilize fork 9 times if father, this:

for (int = 0; < 9; i++) { // creo 9 hijos. if (child_pid > 0) { child_pid = fork(); childs[i] = child_pid; } if (child_pid < 0) printf("error...\n"); }

now, have print on each children children is, starting 0, thinking this:

printf("this kid #%d\n", getpid() - getppid());

but i'm not sure, work?, if while parent creating childrens operating scheme creates process?, number of children discontinued?. , finally, if reply yes, how can create #n children knows children number n?.

you can utilize i variable tell kid in, logic of loop incorrect. should go this:

for (int = 0; < 9; ++i) { child_pid = fork(); if (child_pid == 0) { // child. value of variable tell one. // if == 0 first child, == 1 , second, , on. printf("we kid #%d\n", i); exit(exit_success); } if (child_pid < 0) { // forking failed. perror("fork()"); exit(exit_failure); } // otherwise parent , forking successful; go on loop. }

the operating scheme not required assign process ids in sequential order. if process using next one, skipped on in sequential assignment method, os assign random number pid long not in use.

c++ c fork

No comments:

Post a Comment