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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:35:25+00:00 2026-05-13T22:35:25+00:00

Is there any way to differentiate the child processes created by different fork() functions

  • 0

Is there any way to differentiate the child processes created by different fork() functions within a program.

global variable i;

SIGCHLD handler function()
{
  i--;
}

handle()
{
  fork() --> FORK2
}

main()
{
  while(1)
  {
     if(i<5)
     {
        i++;
        if( (fpid=fork())==0) --> FORK1
           handle()
        else (fpid>0)
           .....
     }
  }
}

Is there any way I can differentiate between child processes created by FORK1 and FORK2 ?? because I am trying to decrement the value of global variable ‘i’ in SIGCHLD handler function and it should be decremented only for the processes created by FORK1 ..

I tried to use an array and save the process id of the child processes created by FORK1 and this is done by the parent process. I will decrement the value of ‘i’ only if the process id of dead child is within the array …

But I faced a problem with the following scenario

child1, parent1, child1 killed, child2, child2 killed, parent2

Incase of child1 since it is killed after parent1 the array is updated properly.

But what in the case of child2 which gets killed before its pid value get updated by parent2 in the array? Inside SIGCHLD signal handler function since child2 PID value is not in the array the ‘i’ value is not getting decremented accordingly ..

So is there any better solution for this problem ??

  • 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-05-13T22:35:26+00:00Added an answer on May 13, 2026 at 10:35 pm

    Since you’re already saving a list of PIDs for which you want to decrement the value of i in the SIGCHLD handler, the part that may be tripping you up is receiving an additional SIGCHLD while updating the list.

    You can mask/block additional SIGCHLD signals during execution of the signal handler using sigprocmask(). This should provide you sufficient time to update your array/list of process IDs without worrying about receiving another SIGCHLD. The blocked mask must be returned to its original value when the signal handling routine has finished.

    Try adding something similar to the following to your SIGCHLD handler:

    sigset_t mask;
    sigset_t orig_mask;
    
    sigemptyset (&mask);
    sigaddset (&mask, SIGCHLD);
    
    /* temporarily mask/block SIGCHLD signals and save original mask */
    if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
        perror ("sigprocmask - %s",  strerror(errno));
    }
    
    /* process SIGCHLD via waitpid() and update PID list as necessary */
    ...
    
    /* restore original mask */ 
    if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) {
        perror ("sigprocmask - %s", strerror(errno));
    }
    

    Update

    After re-examining your question, I see that you have a race conditon in which a child process terminates before the parent process can add its PID to the list.

    One possible solution is still to use sigprocmask() to block the SIGCHLD signal during the critical section. In this case, you will need to block the SIGCHLD signal before your call to fork() and unblock it in the parent code after the PID has been added to the list. If the child dies, the signal handler will be called after you unblock the signal and this should guarantee that the PID is in the list.

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

Sidebar

Related Questions

No related questions found

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.