分类: LINUX
2006-04-22 12:37:17
while : ; do ps -C $prog &> /dev/null if [ $? -ne 0 ]; then break; fi echo $prog is running! try it 5 minutes later! sleep 300 done |
#include #include #include #include #include #include #include static int sig_received = 0; void sig_handler(int signo) { sig_received = 1; } int main(int argc, char *argv[]) { pid_t ret; pid_t child; int i; if(argc != 2){ fprintf(stderr, "Usage: %s pid\n", argv[0]); exit(1); } signal(SIGCHLD, sig_handler); /* check the parameter */ for(i = 0; argv[1][i] >= '0' && argv[1][i] <= '9'; i ++) ; if(argv[1][i] != '\0'){ fprintf(stderr, "Usage: %s pid\n", argv[0]); exit(1); } child = atoi(argv[1]); if(ptrace(PTRACE_ATTACH, child, NULL, NULL) == -1){ perror("ptrace"); exit(1); } while(1){ if(sig_received){ /* check if the process exists */ ret = waitpid(child, NULL, WNOHANG); if(ret == -1){ if(errno == ECHILD){ /* the child has exit */ return 0; } perror("waitpid"); exit(1); } /* child is running, let it continue */ sig_received = 0; /* must before ptrace */ if(ptrace(PTRACE_CONT, child, NULL, SIGCHLD) == -1){ perror("ptrace"); exit(1); } } ret = wait(NULL); /* wait a signal */ if(ret == -1){ if(errno == EINTR) continue; perror("wait"); exit(1); }else{ continue; /* received signal from the child */ } } return 0; } |