Quick question about processes in C/C++. When I use fork() to create two processes in my main, on my child process I am calling an extern API to get a vector of bools and send it through a pipe to the parent, but when I call the API he kills right away the child process without sending the vector through the pipe first. Why do you guys think this might be happening? The code is something like this :
//main.cpp
#include "main.hpp"
int main(){
pid_t c_pid = fork();
if(c_pid == 0) {
std::cout << "Code From here implements Child process"<< std::endl;
API::Api_Get_Method(); // Child dies here
std::cout << "Finish Child process " << std::endl;
std::exit(EXIT_SUCCESS); // But should die here
}
else{
wait(nullptr)
std::cout << "Code From here implements Parent process Id : " << std::endl;
std::cout << "Finish Parent process " << std::endl;
}
}
//main.hpp
namespace API{
void Api_Get_Method(){
// Do stuff
// Print the result of the Stuff
}
}
```
with your else statement, it runs "wait(nullptr)" for all pids that are not zero, which is not necessarily only the parent. Also, if you do wait(nullptr) in the parent, it will wait until all child processes have terminated before continuing (which is good because they're not left orphanated).
Children from fork() ideally should be terminated because if they aren't, they're stuck as allocated space not being referenced by a parent in the processes tree. This eats RAM and slows the thread that it's on. Given a lot of these, one might have to restart their system. In short, it's just memory and thread resource allocations.