Sign in to follow this  

C Posix exec success fail?

This topic is 1440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites

This topic is 1440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this