Skip to content

Instantly share code, notes, and snippets.

@qianjigui
Last active January 4, 2016 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qianjigui/8644808 to your computer and use it in GitHub Desktop.
Save qianjigui/8644808 to your computer and use it in GitHub Desktop.
CBestPractice_forkrun
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
pid_t child;
int childStatus;
if((child = fork()) < 0){
LOGE("fork children error : %s", strerror(errno));
} else if(0==child) {
//child
execlp(cmdfile,argument0,argument1,NULL);//Last Argument : NULL
//Only return if execlp run failed
LOGE("child execlp error : %s", strerror(errno));
exit(EXIT_FAILURE);
} else {
//parent
waitpid(child, &childStatus, 0);
if (WIFEXITED(childStatus)){
if (0==WEXITSTATUS(childStatus)) {
LOGI("Execute success");
//success
} else {
LOGI("Execute failed ");
//failed
}
} else {
LOGI("Execute failed unexpected ");
//failed unexpected
}
}
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
static bool forkRunShell(const char* fullname)
{
pid_t child;
int childStatus;
bool res = false;
if((child = fork()) < 0){
LOGE("fork children error : %s", strerror(errno));
} else if(0==child) {
//child
execlp(SHELL_PATH,SHELL_PATH,fullname,NULL);//Last Argument : NULL
//Only return if execlp run failed
LOGE("child execlp error : %s", strerror(errno));
exit(EXIT_FAILURE);
} else {
int time = 0;
int rc;
bool isWait=false;
//parent
while(time<EACH_SHELL_TIMEOUT_SECONDS){
isWait=false;
rc=waitpid(child, &childStatus, WNOHANG);
if(rc<0){
LOGE("waitpid failed: %s", strerror(errno));
} else if(rc>0){
if (WIFEXITED(childStatus)){
if (0==WEXITSTATUS(childStatus)) {
LOGI("Execute success");
res = true;
//success
} else {
LOGI("Execute failed ");
//failed
}
} else {
LOGI("Execute failed unexpected %d",WTERMSIG(childStatus));
//failed unexpected
}
} else {
time++;
LOGD("Parent sleep %ds to child(%d)",time,child);
sleep(1);
isWait=true;
}
if(!isWait)break;
}
if(isWait){
LOGW("Child Process timeout");
kill(child, SIGTERM);
}
}
return res;
}
@qianjigui
Copy link
Author

timeout support

static bool forkRunShell(const char* fullname)
{
    pid_t child;
    int childStatus;
    bool res = false;

    if((child = fork()) < 0){
        LOGE("fork children error : %s", strerror(errno));
    } else if(0==child) {
        //child
        execlp(SHELL_PATH,SHELL_PATH,fullname,NULL);//Last Argument : NULL
        //Only return if execlp run failed
        LOGE("child execlp error : %s", strerror(errno));
        exit(EXIT_FAILURE);
    } else {
        int time = 0;
        int rc;
        bool isWait=false;
        //parent
        while(time<EACH_SHELL_TIMEOUT_SECONDS){
            isWait=false;
            rc=waitpid(child, &childStatus, WNOHANG);
            if(rc<0){
                LOGE("waitpid failed: %s", strerror(errno));
            } else if(rc>0){
                if (WIFEXITED(childStatus)){
                    if (0==WEXITSTATUS(childStatus)) {
                        LOGI("Execute success");
                        res = true;
                        //success
                    } else {
                        LOGI("Execute failed ");
                        //failed
                    }
                } else {
                    LOGI("Execute failed unexpected %d",WTERMSIG(childStatus));
                    //failed unexpected
                }
            } else {
                time++;
                LOGD("Parent sleep %ds to child(%d)",time,child);
                sleep(1);
                isWait=true;
            }
            if(!isWait)break;
        }
        if(isWait){
            LOGW("Child Process timeout");
            kill(child, SIGTERM);
        }
    }
    return res;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment