当前位置: 技术问答>linux和unix
父进程等待子进程n秒,如果子进程正常退出,父进程立即继续向下执行,如果子出现进程故障一直不退出,父进程等待n秒后继续向下执行,请问
来源: 互联网 发布时间:2015-12-08
本文导语: 父进程等待子进程n秒,如果子进程正常退出,父进程立即继续向下执行,如果子出现进程故障一直不退出,父进程等待n秒后继续向下执行,请问如何实现 int safe_system(const char *s) { /* XXX This function needs some optim...
父进程等待子进程n秒,如果子进程正常退出,父进程立即继续向下执行,如果子出现进程故障一直不退出,父进程等待n秒后继续向下执行,请问如何实现
int safe_system(const char *s)
{
/* XXX This function needs some optimization work XXX */
pid_t pid;
int x;
int res;
struct rusage rusage;
int status;
void (*prev_handler) = signal(SIGCHLD, null_sig_handler);
pid = fork();
if (pid == 0) {
/* Close file descriptors and launch system command */
for (x=STDERR_FILENO + 1; x 0) {
for(;;) {
// help help busy!!!!
}
} else {
fprintf(stderr, "Fork failed: %sn", strerror(errno));
res = -1;
}
signal(SIGCHLD, prev_handler);
return res;
}
int safe_system(const char *s)
{
/* XXX This function needs some optimization work XXX */
pid_t pid;
int x;
int res;
struct rusage rusage;
int status;
void (*prev_handler) = signal(SIGCHLD, null_sig_handler);
pid = fork();
if (pid == 0) {
/* Close file descriptors and launch system command */
for (x=STDERR_FILENO + 1; x 0) {
for(;;) {
// help help busy!!!!
}
} else {
fprintf(stderr, "Fork failed: %sn", strerror(errno));
res = -1;
}
signal(SIGCHLD, prev_handler);
return res;
}
|
man waitpid