0

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
  }
}

```
Pedro Lopes
  • 33
  • 2
  • 5
  • Have you tried stepping through the code to see where the child dies? Maybe the problem is not about forking at all. You could also look into multi-threaded execution instead of fork (easier to debug) – Refugnic Eternium Oct 20 '22 at 20:14
  • Generally (though I may be mistaken), forking does not save your child from terminating when the parent process dies. There are ways to prevent the death by SIGHUP, but I don't see them in this code snippet. Of course I see the 'wait', but I do not know how long `wait` actually waits. – Refugnic Eternium Oct 20 '22 at 20:17
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 20 '22 at 20:25
  • @RefugnicEternium Yhea but the debbug doesn't work very good with fork processes, also it doesn't raise any exception when I put a try block on it. Wait function waits for the child process to die so that the parent process can resume – Pedro Lopes Oct 21 '22 at 08:34
  • @PedroLopes, have you tried running the child code directly in your debugger. Without the forking? Y'know, don't run the parent, run the child? – Refugnic Eternium Oct 21 '22 at 09:16
  • @RefugnicEternium I don't know how I can run the child process on my debugger instead of the parent process – Pedro Lopes Oct 21 '22 at 12:16
  • Have you considered copying your child code into a separate project file and run that in your debugger? – Refugnic Eternium Oct 21 '22 at 12:19
  • @RefugnicEternium Ok I see what you mean, I have done something similar to that and I have gotten a Segmentation Fault when running in debugg. Basicly I changed the Api call from the child to the parent and I got a segmentation fault – Pedro Lopes Oct 21 '22 at 12:27
  • @PedroLopes, well, there's your answer. That's why your child exits right after launching. Now you need to figure out what is causing the segmentation fault. Figure out which line causes it and start working from there. Please open a new question if the new problem finds you stumped. – Refugnic Eternium Oct 21 '22 at 12:58

1 Answers1

0

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.

  • That is what the "std::exit(EXIT_SUCCESS);" does, it exists the child process when it reaches this statement, or at least it should do that. – Pedro Lopes Oct 21 '22 at 08:39