Search code examples
c++forkposixinterprocess

asynchronous c++ inter-process control


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();
        }
    }
}

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.