Last active
January 25, 2017 16:43
-
-
Save chris-marsh/9b4c0172f5aa9abf4eb81d5c035f5665 to your computer and use it in GitHub Desktop.
ANSI std C89/99 to send kill signals using system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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