Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% CPU usage during wait #7

Open
raypulver opened this issue Dec 16, 2014 · 2 comments
Open

100% CPU usage during wait #7

raypulver opened this issue Dec 16, 2014 · 2 comments

Comments

@raypulver
Copy link

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);
@raypulver
Copy link
Author

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant