Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Created November 4, 2018 04:27
Show Gist options
  • Save horitaku1124/32fb3e591c1437ee6dba5d6297f630e7 to your computer and use it in GitHub Desktop.
Save horitaku1124/32fb3e591c1437ee6dba5d6297f630e7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <signal.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#define BUFSIZE 128
static pid_t pid;
void intHandler(int dummy) {
printf("INT\n");
kill(pid, SIGINT);
}
int main(void) {
pid = fork();
if (pid == -1) {
return -1;
} else if (pid == 0) {
return 0;
}
printf("pid=%d\n", pid);
char *cmd = "ping 192.168.11.1 -c 10";
char buf[BUFSIZE];
FILE *fp;
if ((fp = popen(cmd, "r")) == NULL) {
printf("Error opening pipe!\n");
return -1;
}
signal(SIGINT, intHandler);
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("OUTPUT: %s", buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment