You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have found waitpid very useful for my project but have recently discovered that it uses the entire CPU while it waits. Is there any change that could be made to the code so that it blocks execution without consuming resources during the wait? The problem is with this loop
do {
r = waitpid(child, &status, WNOHANG);
} while (r != -1);
This seems to work. WNOHANG makes it so the call is non-blocking. If you pass no flags to waitpid instead, it blocks without need for the do while loop. I wonder why WNOHANG was passed in though?
I had problems with my original solution, finding out that you definitely have to loop over waitpid to use it correctly. I eventually ended up using the loop
while (waitpid(child, &status, 0) == - 1) {
if (errno != EINTR) {
perror("waitpid");
exit(1);
}
}
This seems to work perfectly, without the 100% CPU problem.
Activity
raypulver commentedon Dec 16, 2014
I took out the loop and replaced it with
waitpid(child, &status, 0);
This seems to work. WNOHANG makes it so the call is non-blocking. If you pass no flags to waitpid instead, it blocks without need for the do while loop. I wonder why WNOHANG was passed in though?
raypulver commentedon Jan 1, 2015
I had problems with my original solution, finding out that you definitely have to loop over waitpid to use it correctly. I eventually ended up using the loop
This seems to work perfectly, without the 100% CPU problem.