Skip to content

Instantly share code, notes, and snippets.

@chris-marsh
Last active January 25, 2017 16:43
Show Gist options
  • Save chris-marsh/9b4c0172f5aa9abf4eb81d5c035f5665 to your computer and use it in GitHub Desktop.
Save chris-marsh/9b4c0172f5aa9abf4eb81d5c035f5665 to your computer and use it in GitHub Desktop.
ANSI std C89/99 to send kill signals using system
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
void send_sig(int pid, int sig)
{
char exec_str[128];
sprintf(exec_str, "kill -%d %d >/dev/null 2>&1", sig, pid);
int result = WEXITSTATUS(system(exec_str));
if (result == 127)
puts("Failed to send signal to process");
else if (result == 1)
puts("No such process");
else if (result == 0)
puts("Success");
}
void main(void)
{
int pid = getpid();
send_sig(pid, 9);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment