# C Posix exec success fail?

## Recommended Posts

jeff8j    787

Im trying to read and write to a external process so im using a popen type of function that forks and calls execl that works great but theres no way for me to detect if it actually launched or not if I take something I know doesnt exist I should be able to tell that it couldnt be launched

my thought is to do something like

fork

execl

if it makes it past execl it failed I can set a variable to let the parent process know it failed

the problem with that is the parent process would only be able to know if it waits some period of time and checks that the variable isnt set

I would like to have it work without waiting because if something hangs up it will fail and theres got to be a better way

    if( pipe(this->pipefd) != 0 ){
return false;
}
processid = fork();
if( processid == -1 ){
processid = 0;
return false;
}
if (processid == 0){
// Child
dup2(this->pipefd[0], STDIN_FILENO);
dup2(this->pipefd[1], STDOUT_FILENO);
dup2(this->pipefd[2], STDERR_FILENO);
//execl("your/script", "/path/to/your/script", (char*) NULL);
execl(command->c_str(), "/path/to/your/script", (char*) NULL);
// Nothing below this line should be executed by child process. If so,
// it means that the execl function wasn't successfull, so lets exit:
exit(1);
}
// The code below will be executed only by parent. You can write and read
// from the child using pipefd descriptors, and you can send signals to
// the process using its pid by kill() function. If the child process will
// exit unexpectedly, the parent process will obtain SIGCHLD signal that
// can be handled (e.g. you can respawn the child process).
std::cout << "Process Success" << std::endl;
return true;

Edited by jeff8j