Kill用法举例

/*
 * 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);
}


posted @ 2012-10-30 21:34  CodingMonkey  阅读(246)  评论(0编辑  收藏  举报