Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:12:07+00:00 2026-06-11T23:12:07+00:00

My program is supposed to limit the number of child processes to 3. With

  • 0

My program is supposed to limit the number of child processes to 3.

With the code below, waitpid stalls my parent process so I can’t create more child processes after the first one. If I don’t use waitpid then I don’t know when a child process quits to decrease the number of alive processes.

int numProcs = 0;
while(1==1) {
    /* 
     * inserts code that waits for incoming input 
     */
    numProcs++;
    pid = fork();
    if (pid == 0) {
        doStuff(); // may exit anytime, based on user input
    } else {
        if (numProcs > 3) {
            wait(&status);
            numProcs--;
        } else {
            waitpid(pid, &status, 0); // PROBLEM!
            numProcs--;
        }
    }
}

I’ve been searching for this problem the whole day. Can somebody help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:12:08+00:00Added an answer on June 11, 2026 at 11:12 pm

    At the risk of being obvious, you basically want to just drop the else clause. The logic you’re looking for is something like:

    int max_active = 3; // or whatever
    int number_active = 0;
    bool done = false;
    
    for (; !done; ++number_active) {
      // wait for something to do;
      GetSomeWork();
      // wait for something to finish, if necessary.
      for (; number_active >= max_active; --number_active)
        wait(&status);
      pid = fork();
      if (pid < 0)
        ReportErrorAndDie();
      if (pid == 0)
        DoTheWorkAndExit();
    }
    

    This actually lets you change the value of max_active without restarting, which is the only justification for the for loop around the wait() call.

    The obvious complaint is that number_active in my version doesn’t actually tell you how many processes are active, which is true. It tells you how many processes you haven’t wait()’ed for, which means that you might keep some zombies (but the number is limited). If you’re constantly running at or close to the maximum number of tasks, this doesn’t matter, and unless your maximum is huge, it doesn’t matter anyway, since the only Design Requirement was that you don’t use more than the maximum number of tasks, and consequently you only have to know that the number active is not more than the maximum.

    If this really bothers you and you want to clean the tasks up, you can put:

    for (; waitpid(-1, &status, WNOHANG) > 0; --number_active) {}
    

    before the other for loop, which will reap the zombies before checking if you need to block. (I can’t remember if waitpid(-1, &status WNOHANG) returns an error if there are no processes at all, but in any event there’s no point continuing the loop on an error.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program is supposed to create a button that the user can press to
This program is supposed to calculate the number of degrees below 60 on a
The program is supposed to ask the user for a number, and loops until
The program is supposed to create x amount of threads based on the arguments
This program is supposed to take in a three digit number and change it
The program is supposed to calculate the number of arguments, iterate over the list
My program is supposed to create a folder for the uploaded images on the
Basically the program is supposed to create a round table of executives, with a
The program is supposed to take a file, say data.dat, filled with a list
My program is supposed to read a text document with a bunch of numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.