Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve instance of class from a fork?

I'm currently working on a project where I need to recreate a Pizzeria. My Pizzeria is composed of a reception (CLI), which give orders to kitchens (subprocess).

Each kitchen got several member functions that I want to use from the reception, in order to do that I have chosen to have an std::vector<Kitchen> to call the function I want in each of my kitchens in a loop.

My problem is the following, how can I keep track of all my instances in the vector ?

For now, I wanted to do something like this:

int Reception::openKitchen(void)
{
    int pid = fork();
    Kitchen newKitchen;

    if (pid == -1) {
        std::cerr << "Can't open a new kitchen" << std::endl;
        return -1;
    } else if (pid == 0) { // In the child process
        // Do something in the child
    } else { // In my parent
        this->myVector.push_back(newKitchen); // Try to keep track of the new instance of Kitchen
    }
    return 0;
}

But with this, I can't access the instance of my child process and call the function. Is there any way to do that ?

like image 335
Bibas Avatar asked Dec 30 '25 18:12

Bibas


2 Answers

A general rather a specific and concrete answer:

Operating system processes cannot communicate at the level of abstraction of class instances and their methods (at least - not the instances and methods you've devised in your application). And - separate processes have a separate memory space (unlike threads, by the way). So your Reception will not be able to actuallu work with any objects the kitchens have, directly. You would have to either use some kind of inter-process communications protocol (e.g. using pipes or messages), or if your OS supports it - utilize an inter-process shared memory buffer. Even that will not be exactly like having access to the Kitchens' memory space, so you'll need to be careful.

like image 88
einpoklum Avatar answered Jan 01 '26 11:01

einpoklum


It doesn't work that way.

You've forked off a new child process, which in turn creates an instance of newKitchen and appends it to myVector. Meanwhile, the parent process has done the same thing. Now you have two different processes. Or more abstractly - two identical pizzerias - each with its own kitchen - both operating independently.

I assume this is an academic exercise, but I suspect you need some sort of message passing or IPC mechanism by which the child process owns the new kitchen object, and the parent process sends commands or messages to the child process to simulate "orders" being sent to the kitchen. Google for Linux message queues. One result here

I'm assuming you're using fork for a reason (academic exercise?). Otherwise, the better and easier way to do this simulation is to use threads instead of processes. Or better yet, just keep the entire thing single threaded.

like image 41
selbie Avatar answered Jan 01 '26 10:01

selbie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!