- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果子进程超过超时限制,我想终止它,超时限制在几秒钟内作为参数传递给程序。
在这个例子中,我将 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()
,它用 exec
ed 程序完全替换当前程序。这样信号处理程序就消失了。
因为您无法控制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/
我使用这个 cmd 应用程序 https://github.com/tokland/youtube-upload 上传 50 个视频后,我收到此错误: [RequestError] Server re
尝试将 docker 容器提交到镜像时出现错误: [root@dockerregistry /]# docker commit da4180ab1536 dockerregistry:5000/myi
我只是想知道这样做会更好吗: if((fd = open(filename, O_RDWR)) == -1) { fprintf(stderr, "open [ %s ]\n", strerror(e
我在我的开发机器(单个笔记本)中使用 Elasticsearch 1.4.4。一切都设置为默认值,因为我从未更改过任何设置。 当我启动它时,我通常会收到以下消息: [2015-10-27 09:38:
我收到错误 Lock wait timeout exceeded;尝试重新启动事务。这是什么原因,如何解决?仅供引用:MySQL 配置文件中的 innodb_lock_wait_timeout = 1
我对 Slack 中的 block 功能有疑问。有人设法构建 3 列而不是 2 列吗? 我凭直觉尝试了以下代码,但它不起作用: { "blocks": [ {
div 中的内容超过由 css 决定的固定大小。 #fixeddiv { position: fixed; margin: auto; max-height: 300px
我想将 EditText 字段限制为 150 个字符,我可以很容易地做到这一点。但是,当用户试图超过该限制时,我还需要通过键入(即第 151 个字符)或粘贴超过 150 个字符来提醒用户。 解决这个问
我目前正在使用此代码并排记录两个窗口: ffmpeg -f gdigrab -framerate 30 -i title="" -f gdigrab -framerate 30 -i title=""
我在几个包含长字符串的单元格上使用嵌套的 SUBSTITUE 函数,并定期更新 SUBSTITUE fx,这导致我将其复制并粘贴到所有需要它的单元格中。问题是,我的 SUBSTITUTE 列表会随着时
我创建了一个标题 div,如下所示:
Here is the demo link 您怎么看,页面中只有 8 个广告可见,但我有 14 个广告。我已阅读有关广告限制的信息 here但不明白是不是我的情况?有人可以给我确切的答案,为什么我看不
我的非常简单的算法 - C 中的快速排序有问题。它非常有效(随机化大约 0.1 秒并检查列表是否已排序)但是当我想要对超过 500k 的元素进行排序时它会崩溃。不幸的是,我需要对它们进行更多排序,因为
我成功解决了一个关于 Hackerrank 的问题,它通过了所有测试用例,但我得到了一个错误,超过了时间限制。我猜如果我优化我的代码它会工作,但我想不出任何方法来使我的代码更有效率。 问题是: 对大小
你会如何用 包围下面的文字3 个反引号 ```使用 tpope 的 Vim Surround . 我所能做的就是 1 个反引号 使用 S`在视觉模式下: 最佳答案 这不是你问的,但这可以在没有环绕的情
我目前有一个模拟账户。我正在尝试为我的雇主使用 SwifType 制作 POC。我们有一个非常大的数据库,每 1 小时索引一次,并创建一个 JSON 文件。我认为与 Elastic 的集成将非常容易,
我为一个大约有 100 到 120 名成员的小型组织维护网站。 每个月,我们都会发送一封电子邮件,通知我们的成员我们将在即将举行的 session 中讨论的主题。 我正在尝试使用我们的网站为我们提供一
这个问题已经有答案了: How to automatically input an array formula as string with more than 255 characters in l
我有一个在 JBoss 6.1 中运行的 JSF 应用程序,它使用内部Tomcat Servlet 容器。 我已经通过apache commons文件上传实现了上传。我想防止上传过大的文件并设置了属性
当我尝试在 PyMySQL 上执行查询时,出现以下错误: pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try rest
我是一名优秀的程序员,十分优秀!