I have to run my processes in strong order, child then parent, what if child gets blocked, how to stop parent?
if ((pid = fork()) == -1) { /* create a child process */
perror("fork() failed\n");
exit(1);
}
if (pid == 0) {
/* child process */
func1(NULL);
fflush(stdout);
return 0;
} else {
/* parent process */
wait(NULL);
func2(NULL);
fflush(stdout);
}
Aaand as an alternative to an active cpu-busy loop with a timeout doing waitpid()
with WNOHANG
in a loop, you should prefer to implement it in such a way that cpu is free.
Do not call wait
from parent, instead enable SIGCHLD
and setup timeout with a timer and call pause()
and wait for and dispatch events from signals with pause()
. An example how to do it is even in man 3 wait
.
Also on Linux you can use pidfd_open(pid, 0)
and call poll()
on it for POLLIN
with a timeout.