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 ?
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.