gpt4 book ai didi

c - 超过超时限制后未从 alarm() 触发信号处理程序

转载 作者:太空宇宙 更新时间:2023-11-04 06:48:53 27 4
gpt4 key购买 nike

如果子进程超过超时限制,我想终止它,超时限制在几秒钟内作为参数传递给程序。

在这个例子中,我将 3 作为我的超时限制。这里的程序是 /bin/cat 没有任何额外的参数,所以它应该挂起并且 SIGALRM 应该被触发,但由于某种原因它没有触发 killChild( ) 函数。

void killChild(int sig) {
printf("PID: %d\n", getpid());
kill(getpid(), SIGKILL);
}

int main(int argc, char** argv) {

// Parse timeout arg
int timeout = 0;
if (argv[1] != NULL) {
timeout = atoi(argv[1]);
}

char program[] = "/bin/cat";

// Create child process
pid_t child = fork();


if (child == 0) { // Child

signal(SIGALRM, killChild);
alarm(timeout);

printf("I'm the child %d, my parent is %d\n", getpid(), getppid());
char* av[] = { program, NULL };

execve(program, av, NULL);
} else { // Parent

printf("I'm the parent %d, my child is %d\n", getpid(), child);
wait(NULL);
alarm(0); // Reset alarm if program executes within timeout limit
}
return 0;
}

编辑:根据@alk 的建议,信号正在被替换,所以我唯一的选择是将其保留在父进程中,因此我修改了代码以包含 alarm()signal() 在子 block 外部调用。

现在正在调用 killChild() 处理程序,但是现在有一个问题是 killChild() 中的 getpid() 是引用父 PID - 如何将子 PID 传递给 killChild()

signal(SIGALRM, killChild);
alarm(timeout);

if (child == 0) { // Child

printf("I'm the child %d, my parent is %d\n", getpid(), getppid());
char* av[] = { program, NULL };

execve(program, av, NULL);
} else { // Parent

printf("I'm the parent %d, my child is %d\n", getpid(), child);
wait(NULL);
alarm(0); // Reset alarm if program executes within timeout limit
}

最佳答案

您为子进程安装信号处理程序,然后调用 execve(),它用 execed 程序完全替换当前程序。这样信号处理程序就消失了。

因为您无法控制exec 进程的行为,所以只有父进程可以杀死它的子进程。因此,您想为父级安装信号处理程序并让它向子级发送 SIGKILL

实现这个可能会很棘手,因为好像需要信号处理程序知道 child 的 PID。

有几种方法可以做到这一点。

让我们从复杂但便携的开始。这里的信号处理程序并没有杀死 child ,只是设置了一个标志,表明它被调用了:

#include <stdio.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>


volatile sig_atomic_t f = 0;

/* to be set as handler for SIGALRM */
void sig_alarm(int unused)
{
f = 1;
}

int main(void)
{
pid_t child_pid;

/* install signal handler here */
...

/* fork/exec and set child_pid here */
...

/* assuming to be in the parent from here */
...

/* set alarm here */
...

while (!f)
{
int status;
int result = waitpid(child_pid, &status, WNOHANG);
if (-1 == result)
{
if (errno != EINTR)
{
perror("waitpid() failed");
exit(EXIT_FAILURE);
}

continue;
}
else if (0 != result) /* child ended. */
{
/* Analyse status here to learn in detail if the child
ended abnormally or normally and if the latter which
exit code it returned (see W* marcos on man waitpid). */
break;
}

sleep(1); /* busy waiting is not nice so sleep a bit */
}

if (f) /* sig-alarm handler was called */
{
if (-1 == kill(child_pid, SIGKILL))
{
perror("kill() failed");
exit(EXIT_FAILURE);
}
}

exit(EXIT_SUCCESS);
}

可能无法在任何系统上运行的快速而肮脏的解决方案是全局定义 child_pid

volatile sig_atomic_t child_pid = 0;

并调用 sig-alarm 处理程序

  kill(child_pid, SIGKILL)

这可能不起作用,因为不清楚 pid_t 是否适合构建代码的平台上的 sig_atomic_t

也不能在信号处理程序中使用 printf() 和其他几个非异步信号保存函数。例如,调用 perror() 来指示失败是一个错误。

关于c - 超过超时限制后未从 alarm() 触发信号处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54293489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com