Skip to content

100% CPU usage during wait #7

Open

Description

@raypulver

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);

Activity

  1. raypulver commented on Dec 16, 2014

    @raypulver
    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?

  2. raypulver commented on Jan 1, 2015

    @raypulver
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions