【发布时间】:2021-07-19 08:05:02
【问题描述】:
我想完全终止 Linux C89 上的进程。
流程是:检查它是否已经死亡,如果没有,则使用sigterm让它和平死亡,然后等待10秒直到它死亡。如果它还活着 - SIGKILL它。
int TerminateProcessIMP(pid_t process_to_kill)
{
assert(process_to_kill);
/*---------------------------------------------------------*/
/* check if process is already does not exist */
if (!IsProcessAliveIMP(process_to_kill))
{
return (SUCCESS);
}
/*---------------------------------------------------------*/
/* terminate process_to_kill */
if (kill(process_to_kill, SIGTERM))
{
fprintf(stderr, "%s\n", strerror(errno));
}
if (!IsProcessAliveIMP(process_to_kill))
{
return (SUCCESS);
}
/*---------------------------------------------------------*/
/* if its still alive, SIGKILL it */
if (kill(process_to_kill, SIGKILL))
{
fprintf(stderr, "%s\n", strerror(errno));
}
if (!IsProcessAliveIMP(process_to_kill))
{
return (SUCCESS);
}
/*---------------------------------------------------------*/
return (FAILURE);
}
/******************************************************************************/
int IsProcessAliveIMP(pid_t process_to_check)
{
time_t start_time = 0;
time_t end_time = 0;
time_t time_to_wait = 10; /* in seconds */
assert(process_to_check);
start_time = time(0);
end_time = start_time + time_to_wait;
/* give it time to be terminated because maybe it frees memory meanwhile */
while (0 != kill(process_to_check, 0) && time(0) < end_time)
{}
/* check if it still exists */
if (0 == kill(process_to_check, 0))
{
return (0);
}
/* the process is still alive */
return (1);
}
你怎么看?
现在,它不起作用,也不会终止进程。
它试图终止进程但没有这样做。我不知道为什么。
谢谢。
【问题讨论】:
-
哪个操作系统,如果有的话?看门狗是一个术语,指的是嵌入式系统中的一种硬件复位电路。
-
@Lundin 嘿,看门狗不是这里的主要话题。我一般在
how can I terminate a process上发言,为什么我的代码不起作用。看门狗不太相关,我会从帖子中删除它以免混淆其他人,谢谢。
标签: c process signals terminate kill-process