Last active
January 4, 2016 15:59
-
-
Save qianjigui/8644808 to your computer and use it in GitHub Desktop.
CBestPractice_forkrun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
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
timeout check:
http://stackoverflow.com/questions/6383007/killing-a-child-process-if-it-takes-too-much-time