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

  • Home
  • SEARCH
  • 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 8853663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:42:28+00:00 2026-06-14T13:42:28+00:00

I’m doing some parallel programming (multiprocessing) and I need the parent to: Fork several

  • 0

I’m doing some parallel programming (multiprocessing) and I need the parent to:

  1. Fork several children

  2. AFTER all the children have been created, simply WAIT for all of them to terminate

  3. After all the children are terminated, do some other work.

This is what I tried:

 int main(int argc, char **argv[])
{

  int j, i;
  pid_t children[3];
  int pid;

  // Fork the processes
  for(j = 0; j < 3; j++){
    if((children[j] = fork()) == 0){
      // Child process
      for(i = 0; i < 2; i++){
        printf("child %d printing: %d\n", j, i);
      }
    }else {
        // In parent now
        while (pid = waitpid(-1, NULL, 0)) {
            if (errno == ECHILD) {
                break;
            }
        }
        
        printf("all children terminated. in parent now\n");
    }
  }

  return 0;
}

Doesn’t give the correct output. "all children terminated. in parent now" gets printed out several times, even before all the children are dead. Also, for each process, I should see only 2 outputs, but I see more.

  • 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-14T13:42:29+00:00Added an answer on June 14, 2026 at 1:42 pm

    Is this more what you are trying to achieve? I’ve just set a simple count for each child with a delay to increase the parallelisation visibility.

    #include <sys/wait.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    
    int main(int argc, char **argv[])
    {
      int i, j, k;
      int pid;
    
      // Fork the processes
      for(j = 0; j < 3; j++)
      {
        if (fork() == 0)
        {
            printf("starting child %d\n", j);
            // Child process - do your child stuff
            for (i = 0; i < 5; ++i)
            {
              for (k = 0; k < 10000000; ++k);
              printf("child %d printing: %d\n", j, i);
            }
             printf("child %d ending\n", j);
            // then just quit
            exit(0);
        }
      }
    
      j = 1;
      while (wait(NULL) > 0)
      {
        printf("%d child completed\n", j++);
      }
    
      // In parent now
      printf("all children terminated. in parent now\n");
    
      return 0;
    }
    

    The output I get is

    starting child 0
    starting child 1
    child 0 printing: 0
    starting child 2
    child 1 printing: 0
    child 0 printing: 1
    child 2 printing: 0
    child 1 printing: 1
    child 0 printing: 2
    child 0 printing: 3
    child 2 printing: 1
    child 1 printing: 2
    child 1 printing: 3
    child 0 printing: 4
    child 0 ending
    child 2 printing: 2
    1 child completed
    child 1 printing: 4
    child 1 ending
    2 child completed
    child 2 printing: 3
    child 2 printing: 4
    child 2 ending
    3 child completed
    all children terminated. in parent now
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

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.