I have a parent process that needs to create multiple child processes. The best way I've found is to use fork + execl . But then the parent process needs to know whether execl will succeed in a particular child or not, and I don't know how to implement it.
int pid = fork(); if (pid < 0) { std::cout << "ERROR on fork." << std::endl; } if (pid == 0) { execl("/my/program/full/path", (char *)NULL); exit(1); } else { if () { std::cout << "it failed" << std::endl } else { std::cout << "child born" << std::endl } }
I think this idea is not very good:
int status(0); sleep(100); int res = waitpid(pid, &status, WNOHANG); if (res < 0 && errno == 10) { std::cout << "it failed" << std::endl } else { std::cout << "child born" << std::endl }
because itβs not good to hope that the child process will die in 100 milliseconds, I want to know this for sure as soon as this happens.
I also believe that creating a shared_memory or special channel connection for such a check is Cannon vs. Bees.
There must be a simple solution for this that I have not yet found.
What is the best way to achieve this?
source share