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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:42:12+00:00 2026-05-31T07:42:12+00:00

I want to create multiple children using fork() , but I am facing problems.

  • 0

I want to create multiple children using fork(), but I am facing problems.
LIKE:

///////////////////////// these are some class objects creation and calling
//  There are some functions calling from another class. about 5 functions I invoked.
// and some variables like.

    pid_t PID;
    int status = 0;
    char *x = new char[10];

///////////////////////// This for loop give me multiple children
// now one parent have multiple children in this case 10


    for( int i=0; i<10; i++ )
    {
        mNo++;
        rNo++;
        PID = fork();

        if( PID == FORK_ERROR )         // fork ERROR!
        {
            fprintf(stderr, "ERROR! In fork() [FORCE EXITING]... %d\n", errno);
            exit(EXIT_FAILURE);
        }
        else if( PID == CHILD )         // Do Child Stuff Here...
        {
            exit(status);
        }
        else            // Do parent Stuff Here...
        {

        }
    }
    PID = wait(&status);

////////////////////////////////////////////

This was my code which I implemented, but there are some problems.

I want to ask that when I create a child in memory, it creates the duplicate memory . Also the objects which I declared; are they reinvoked by calling fork() so does that mean that for every child there are objects in memory which are reinvoked on class?

I want that if I fork, and the previous functions calling and variables should be same for every child but not non-idempotent functions. I hope you get it. I know this is a very simple problem, but the project I am working on is very critical and it has to implemented with fork(), not in pipes or threads.

Moreover, I am repeating my question that if I fork(), what happens to objects and variables. Before calling fork, are they just reallocated or they again reinvoked for every child? But what I am seeing is that they are reinvoked for every child, which I don’t want. So what do I have to do?

  • 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-31T07:42:13+00:00Added an answer on May 31, 2026 at 7:42 am

    The behaviour of fork() will not change to suit your wishes.

    When the process forks, there are two almost identical copies of the same program. The differences are listed in the POSIX manual page (link above). The memory is the same. Objects in C++ will not be re-constructed, for example. In each of the processes, all the singletons will still be singletons, for example.

    Could you give me an example?

    Here’s the complete main program from something I wrote back in 1991. I’ve only updated it to use a prototype for main() and I’ve included the typedef for Pipe and the definition of id:

    static char id[] = "\n\n\nSQLMONITOR version 3.5 \n";
    typedef int Pipe[2];
    
    int main(int argc, char **argv)
    {
        int     pid;
        Pipe    to_sqlexec;
        Pipe    from_sqlexec;
    
        setarg0(argv[0]);
        write(STDERR, id, sizeof(id)-1);
        if (pipe(to_sqlexec) < 0 || pipe(from_sqlexec) < 0)
            error("unable to create pipes");
        else if ((pid = fork()) < 0)
            error("unable to fork");
        else if (pid == 0)
            be_childish(argv, to_sqlexec, from_sqlexec);
        else
            be_parental(to_sqlexec, from_sqlexec);
        return(0);
    }
    

    The be_childish() function went on to do some plumbing (duplicating and closing appropriate parts of the pipes, before executing another program. The be_parental() process went on to read messages from its standard input, log them to a file, and write the messages to the standard input of its child down the write-side of the to_sqlexec pipe; it would then read a response back from the child on the read-side of the from_sqlexec pipe, and log that message too, and then write it to its standard output. Thus, it would sit in the middle of two processes, recording everything that went from one to the other. The communication protocol between the processes was synchronous, which greatly simplified life. On the other hand, it was moderately hard to work out when the data from one process was complete. The code predated the widespread availability of select() and poll(); nowadays, it would not need to do as much analysis of the messages.

    The key thing to note, though, is that the two pipes are the same in both processes after the fork(); indeed, the only variable that’s different is pid which captures the return value from fork(). Everything else is exactly as it was before the fork().

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

Sidebar

Related Questions

I want to create multiple instances of Zend_Auth class as I have two modules
I want to create multiple thumbnails using GD library in php, and I already
I am using PHP5 and I want to create multiple file uploader of type
all! I want to create multiple threads in my application. I' using following code
I have a class,which implements Runnable interface. I want to create multiple threads for
I want to create multiple bots with all their own unique id. But how
I want to create the app modular and would like to create multiple EDMX,
I want to create person objects in my rails app using a jquery ajax
I want to create multiple versions of print-friendly pages from a single page. I
I`v come across a need where I want to create multiple list items from

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.