c - How can I make a "binary process tree"? -
i have number of process create. every son has create 2 sons. used recursive solution, works number of process created aren't want.
this tried:
void generate_kid(int g, int res){ pid_t kid1, kid2; int status1, status2; if( res > 0 ){ if( kid1 = fork() ){ if( res > 0){ if( kid2 = fork() ){ } else { printf("i %d, father %d\n",getpid(),getppid()); generate_kid(g,res/2-1); } } } else { printf("i %d, father %d\n",getpid(),getppid()); generate_kid(g,res/2-1); } } waitpid(kid1,&status1,0); waitpid(kid2,&status2,0); }
try this:
void generate_kid(int res){ pid_t kid1, kid2; int status1, status2; if( res > 0 ){ if ((kid1 = fork()) == 0) { // child printf("i %d, father %d\n",getpid(),getppid()); // generate half remaining rounded odd processes generate_kid((res-1)/2); } else if (kid1 > 0) { // parent - create second child if( res > 1){ if ((kid2 = fork()) == 0) { // child 2 printf("i %d, father %d\n",getpid(),getppid()); // generate half remaining processes generate_kid((res-2)/2); } else if (kid2 > 0){ // parent 2 waitpid(kid2,&status2,0); } } waitpid(kid1,&status1,0); } } }
Comments
Post a Comment