Question

I'm writing this with c++ on Ubuntu 12.04 LTS and i am explicitly not using any BOOST libraries. What I want to do is have a parent process and two or more children run simultaneously.

The parent process acts like an event listener and task dispatcher. The children handle the tasks the parents tells them to; these tasks are system calls, and the parent doesn't care when they finish.

What I have done successfully so far is

  • parent will fork() some children and retain their pids
  • children are suspended (S) immediately after being fork() with pause()
  • parent resumes (R) and detects events in a standard event loop way

what I need to know next is how to resume a child and return to the parent while child is still running.

So my questions are, can a parent and child run simultaneously and safely in this manner? (where the parent doesn't care what the child is doing after its resumed)

What do I have to do to in general to resume a child process after a pause()?

below is the constructor function that forks the children

childManager(int number_of_children, pid_t parent) {
    int i=0;

    while(number_of_children--) {
        if(parent == getpid())
        {   
            this->parent = parent;
            children.push_back({++i,_IDLE,fork()});
            if(parent != getpid()) pause();
        }
    }
}
Was it helpful?

Solution

The only question I see here is

What do I have to do to in general to resume a child process after a pause()?

Send it a signal whose action is either to execute a signal-catching function or terminate the process. For example:

kill(children[i], SIGUSR);

That said, this is a less than ideal way to do this. You could just as easily have the children block on a pipe waiting for the parent to tell them to do something. Or about 30 similar things which don't involve signals.

OTHER TIPS

To answer your specific questions:

So my questions are, can a parent and child run simultaneously and safely in this manner? (where the parent doesn't care what the child is doing after its resumed)

The parent process will have to call wait or waitpid to harvest the child processes after they exit. If you do not do that you will be left with zombie processes. You can do this in a nonblocking way inside your event loop in the parent process.

What do I have to do to in general to resume a child process after a pause()?

You have to send a signal to the child process. You do this by calling kill in the parent process. You will need to install a signal handler in the child process to catch the signal - use sigaction for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top