/*
* Killing other processes
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
int main()
{
pid_t child;
int status, retval;
if((child = fork()) < 0)
{
perror ("fork");
exit (EXIT_FAILURE);
}
if(child == 0)
{
sleep (1000);
exit (EXIT_SUCCESS);
}
else
{
if((waitpid(child, &status, WNOHANG)) == 0)
{
retval = kill (child, SIGKILL);
if (retval)
{
puts ("kill failed\n");
perror ("kill");
waitpid (child, &status, 0);
}
else
printf ("%d killed\n", child);
}
}
exit (EXIT_SUCCESS);
}Kill用法举例
最新推荐文章于 2020-06-08 11:25:01 发布
本文提供了一个使用C语言通过子进程发送SIGKILL信号来终止指定进程的示例代码。该程序首先创建一个子进程,子进程休眠1000秒后自行退出,父进程则尝试使用`kill`函数向子进程发送SIGKILL信号来强制结束子进程。
956

被折叠的 条评论
为什么被折叠?



