PDA

View Full Version : Forking in C



raac
April 19th, 2011, 08:10 PM
Hello guys, I have a question consider this code



int i;
pid_t child;
for (i = 0; i < 7; i++){
if((child =fork()) == -1){
perror("Error");
}
if(child == 0){
fprintf(stderr, "C is number one\n");
return 0;
}else{ /*parent code*/
while (r_waitpid(-1, NULL, WNOHANG) > 0) ; /* clean zombies */
}

}

fprintf(stderr, "In conclusion C rocks!!\n");
return 0;



My question is how can I guarantee that the string "In conclusion C rocks!!\n", will be that LAST ONE to be executed????

In other words I don't want something like this...
"C is number one\n"
"In conclusion C rocks!!\n"
"C is number one\n"


thanks

Arndt
April 19th, 2011, 08:56 PM
Hello guys, I have a question consider this code



int i;
pid_t child;
for (i = 0; i < 7; i++){
if((child =fork()) == -1){
perror("Error");
}
if(child == 0){
fprintf(stderr, "C is number one\n");
return 0;
}else{ /*parent code*/
while (r_waitpid(-1, NULL, WNOHANG) > 0) ; /* clean zombies */
}

}

fprintf(stderr, "In conclusion C rocks!!\n");
return 0;



My question is how can I guarantee that the string "In conclusion C rocks!!\n", will be that LAST ONE to be executed????

In other words I don't want something like this...
"C is number one\n"
"In conclusion C rocks!!\n"
"C is number one\n"


thanks

Your waitpid logic seems a little strange to me. Can you put in ordinary words what the code should do?

What is r_waitpid, by the way? I only find waitpid in the man pages.

raac
April 19th, 2011, 09:05 PM
pid_t r_waitpid(pid_t pid, int *stat_loc, int options) {
pid_t retval;
while (((retval = waitpid(pid, stat_loc, options)) == -1) &&
(errno == EINTR)) ;
return retval;
}

raac
April 20th, 2011, 01:50 AM
Any ideas guys???

slavik
April 20th, 2011, 02:10 AM
it has to do with buffering I believe ... try fflush(); after each print.