i was wondering why waitpid()
returns -1 while fopen()
exists.
FILE *fp = fopen ("abc.txt", "r");
fclose(fp);
pid_t pid = fork ();
if (pid == 0) { /* child process */
printf ("child %d\n", getpid());
}
else { /* parent process */
pid_t pid2 = waitpid (pid);
printf ("parent %d\n", pid2);
}
pid2
equals to -1 from the above example, but it becomes the same number as pid
(child process number) if i eliminate fopen()
.
thanks for clarifying!