0

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

2 Answers 2

1

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.

2
  • I thought If i was blocked in the child I was stuck in the child. How do you go about blocking to wait on a pipe? Mar 1, 2014 at 23:38
  • @MatUtter, the parent & child are two separate processes after the fork. The child blocks on a pipe simply by trying to read from it (in blocking mode) while there is no data to read. When the parent has something for the child to do it writes it the pipe. The child unblocks when there is something to read, does whatever it is suppose to do, and goes back to blocking on the pipe. This kind of mechanism solves the parent/child communication issue as well.
    – Duck
    Mar 2, 2014 at 0:02
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.