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 8626891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:08:24+00:00 2026-06-12T08:08:24+00:00

I created three processes and I want to synchronize their work. Specifically, I want

  • 0

I created three processes and I want to synchronize their work. Specifically, I want the first process to wait on the second, second on third. Basically execute them in the reverse order they were created in.

Here is what i did so far.

for (; i < 3 ; i++){
        pids[i] = fork();
        if (pids[i] == 0)
           break;
}
if (pids[i] != 0){
    wait(); // Main thread has to wait..    
}
else{               
     if (i == 0){
        waitpid(pids[1], &status, 0);       
        printProcessInfo(0);        
     }
     else if (i == 1){
        waitpid(pids[2], &status, 0);       
        printProcessInfo(1);        
          }
     else if (i == 2){
        printProcessInfo(2);        
     }              
}

I am using i to check which child process I am in because I am assuming I get the correct i in the child process, so if i = 0, i am in fact in the fist child. Is this assumption true? In any case I don’t get the order I want, in fact the processes get executed in their natural order, bypassing the waitpid calls I placed. Any ideas?

  • 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-12T08:08:25+00:00Added an answer on June 12, 2026 at 8:08 am

    No, you are getting more then 3 process.

    Consider a simplified version (this is just removed wait… you do the same number of fork):

    for (i=0; i < 3 ; i++){
        pids[i] = fork();
    }
    

    Because you do the fork inside a for-loop, without checking the return value. Both parent and child will get its own child.

        [###############PARENT###############]
              |           |           |
             i=0         i=1         i=2
              |           |           |
            [ a0 ]      [ b0 ]      [ c0 ]
             / \          |
           i=1 i=2       i=2
           /     \        |
         [a1]   [a2]    [ b1 ]
          |
         i=2
          |
         [a3]
    

    You have to break the loop after checking the return value for fork.

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

Sidebar

Related Questions

I've created a three step order form and I need help with the third
Three erlang processes have been created in erlang shell and registered locally, named Pid,
Now I want to create three processes in my program and there are several
from the javadoc for java.lang.Process : The methods that create processes may not work
We have a process here at work where an input file is created by
Is there any way to differentiate the child processes created by different fork() functions
i have created three linear layouts of equal weights. Inside one of the linear
I have a convenience class for encoding URI's. In it I've created three methods
i am new to iPad developer, i have created two or three iPad application
I've created a simple scene in three.js, but it doesn't works with canvas renderer

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.