[C++] 纯文本查看 复制代码
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
int main(int argc, char **argv)
{
int s, sig, pid;
if (argc != 3 || strcmp(argv[1], "--help") == 0) {
printf ("usage: %s sig-num pid\n", argv[0]);
exit (EXIT_FAILURE);
}
sig = atoi(argv[1]);
pid = atoi(argv[2]);
s = kill(pid, sig);
if (s != 0) {
if (s == -1) {
perror("kill");
exit(EXIT_FAILURE);
}
} else {
if (s == 0) {
printf ("Process exists and we can send it a signal\n");
} else {
if (errno == EPERM)
printf ("Process exists, but we don't have permission to send it a signal\n");
else if (errno == ESRCH)
printf ("Process does not exist\n");
else
perror ("kill");
}
}
exit (EXIT_SUCCESS);
}