linux - In C++, fork and kill not killing all the child processes -
i have below code executes binary in child process, wait 1 sec , if it's not done kill it.
pid_t pid; pid=fork(); if (pid == 0) { //in child execl("/bin/sh", "sh", "-c", "/opt/qcom/bin/version.out > /tmp/version", (char *)null); exit(0); } else { // in parent, wait 1 second sleep(1); int status; if (waitpid(pid, &status, wnohang) != pid) { //kill(pid, sigterm); //--> tried kill(pid, sigkill); } fsmverdir("/tmp/version"); system("rm /tmp/version"); }
but it's not killing completely, see below ps output after running program 3 times (it created 3 version.out's), , "sh" appear zombie...
# ps | grep "version.out\|sh" 2012 root 0 z [sh] 2013 root 13236 s /opt/qcom/bin/version.out 2058 root 0 z [sh] 2059 root 13236 s /opt/qcom/bin/version.out 2092 root 0 z [sh] 2093 root 13236 s /opt/qcom/bin/version.out 2100 root 2360 s grep version.out\|sh #
or, there way run command timeout in busybox linux?
like:
execlp("timeout","timeout","1","sh","-c","/opt/qcom/bin/version.out > /tmp/version",null);
there no timeout in busybox version, there alternative?
you creating shell (the child) in turn runs "version.out" (the grandchild).
you kill child thereby making zombie , orphaning grandchild. can collect child calling wait
second time (it error-ed out first time or never have called kill
) still won't accomplish goal of killing grandchild parent.
Comments
Post a Comment