Created
November 4, 2018 04:27
-
-
Save horitaku1124/32fb3e591c1437ee6dba5d6297f630e7 to your computer and use it in GitHub Desktop.
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 <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