Last active
October 19, 2020 00:54
-
-
Save alyoshenka/25c0166f0bffc2ed204d4af283aca33e to your computer and use it in GitHub Desktop.
pipe output to/from Hubble process
This file contains 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
commandCenter | |
parent_example | |
child_example | |
lPipe | |
rPipe |
This file contains 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> | |
int main() | |
{ | |
fprintf(stderr, "child: Starting\n"); | |
char buf[1024]; | |
while(fgets(buf, sizeof(buf), stdin) != NULL) | |
{ | |
fprintf(stderr, "child: Got input: %s\n", buf); | |
} | |
printf("This is a message to the parent"); | |
fprintf(stderr, "child: Exiting\n"); | |
return 0; | |
} |
This file contains 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
// communicate with Hubble process via stdin/out | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <unistd.h> | |
#include <string> | |
using std::string; | |
const char* cmd = "ps -ef | grep -m1 Hubble | awk '{print $2}'"; | |
int getProcessId(){ | |
char buffer[128]; | |
FILE* p = popen(cmd, "r"); | |
if(!p){ | |
perror("popen failed"); | |
return -1; | |
} | |
// read until end of process | |
while(!feof(p)){ | |
if(fgets(buffer, 128, p) != NULL){ | |
int ret = atoi(buffer); | |
pclose(p); | |
return ret; | |
} | |
} | |
return 0; | |
} | |
void sendMessage(char* msg, int* p){ | |
printf("trying to send message %s %i\n", msg, sizeof(msg)); | |
char ms[] = "beep"; | |
write(p[1], ms, sizeof(ms)); | |
printf("sent message: %s\n", msg); | |
} | |
int main(){ | |
printf("opening command center\n"); | |
// stdin pipe | |
int in[2]; | |
if(pipe(in) == -1){ | |
perror("Failed to create stdin pipe"); | |
return 1; | |
} | |
// stdout pipe | |
int out[2]; | |
if(pipe(out) == -1){ | |
perror("Failed to create stdout pipe"); | |
return 1; | |
} | |
ssize_t pid = getProcessId(); | |
printf("process id: %d\n", pid); | |
// shouldn't read from stdin pipe | |
close(in[0]); | |
// shouldn't write to stdout pipe | |
close(out[1]); | |
char* m; | |
sendMessage(m, in); | |
char buf[1024]; | |
ssize_t bytes; | |
do{ | |
bytes = read(out[0], buf, sizeof(buf) - 1); | |
if(bytes > 0){ | |
buf[bytes] = '\0'; | |
printf("Hubble: %s\n", buf); | |
} | |
} while(bytes > 0); | |
printf("closing command center\n"); | |
return 0; | |
} |
This file contains 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
#!/bin/bash | |
g++ -o commandCenter commandCenter.cpp |
This file contains 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
// https://www.geeksforgeeks.org/named-pipe-fifo-example-c-program/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <signal.h> | |
int main(){ | |
// disable control-c | |
signal(SIGINT, SIG_IGN); | |
int fd; | |
char* myfifo = "/tmp/sharedPipe"; | |
// mkfifo(myfifo, 0666); | |
char arr1[80], arr2[80]; | |
// send initial message | |
fd = open(myfifo, O_WRONLY); | |
write(fd, "opening Command Center", 80); | |
close(fd); | |
while('0' != arr2[0]){ | |
// open for read only | |
fd = open(myfifo, O_RDONLY); | |
read(fd, arr1, sizeof(arr1)); | |
printf("Hubble: %s\n", arr1); | |
close(fd); | |
// open for write only | |
fd = open(myfifo, O_WRONLY); | |
// take input from user | |
fgets(arr2, 80, stdin); | |
// write onto fifo | |
write(fd, arr2, strlen(arr2)+1); | |
close(fd); | |
} | |
printf("Closing CC\n"); | |
// capture closing response | |
fd = open(myfifo, O_RDONLY); | |
read(fd, arr1, sizeof(arr1)); | |
printf("Hubble: %s\n", arr1); | |
close(fd); | |
printf("Closed command center\n"); | |
return 0; | |
} |
This file contains 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
This code was copied directly from u/alanwj on reddit | |
All code ending in _example.cpp is not my creation | |
https://www.reddit.com/r/learnprogramming/comments/5622he/automating_inputoutput_test_for_c_on_linux/d8fq3ck/ |
This file contains 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 <sys/types.h> | |
#include <sys/wait.h> | |
#include <unistd.h> | |
int main() | |
{ | |
printf("parent: Starting\n"); | |
// create a pipe to communicate with stdin | |
int in[2]; | |
if(pipe(in) == -1) | |
{ | |
perror("Failed to create stdin pipe"); | |
return 1; | |
} | |
// create a pipe to communicate with stdout | |
int out[2]; | |
if(pipe(out) == -1) | |
{ | |
perror("Failed to create stdout pipe"); | |
return 1; | |
} | |
pid_t pid = fork(); | |
printf("pid: %i\n", int(pid)); | |
if(pid == 0) | |
{ | |
// we are in the child process | |
// child shouldn't write to stdin pipe | |
close(in[1]); | |
// redirect stdin and stdout for teh child process | |
if(dup2(in[0], fileno(stdin)) == -1) | |
{ | |
perror("Failed to recirect stdin"); | |
return 1; | |
} | |
if(dup2(out[1], fileno(stdout)) == -1) | |
{ | |
perror("Failed to redirect stdout"); | |
return 1; | |
} | |
// start the child executable | |
execlp("./child_example", "child_example", NULL); | |
// we should never get here | |
perror("Failed to create child"); | |
return 1; | |
} | |
else | |
{ | |
// we are in the parent process | |
if(pid == -1){ | |
perror("Failed to create child"); | |
return 1; | |
} | |
// parent shoucln't read from stdin pipe | |
close(in[0]); | |
// parent shouldn't write to stdout pipe | |
close(out[1]); | |
// write some data for teh chold to read | |
char input[] = "This is a message to the child"; | |
write(in[1], input, sizeof(input)); | |
close(in[1]); | |
char buf[1024]; | |
ssize_t bytes; | |
do{ | |
bytes = read(out[0], &buf, sizeof(buf) - 1); | |
if(bytes > 0){ | |
buf[bytes] = '\0'; | |
printf("parent: Child wrote: %s\n", buf); | |
} | |
} while(bytes > 0); | |
printf("parent: Waiting for child process to end\n"); | |
waitpid(pid, NULL, 0); | |
printf("parent: Exiting\n"); | |
} | |
return 0; | |
} |
This file contains 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
// https://www.geeksforgeeks.org/named-pipe-fifo-example-c-program/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
int main(){ | |
int fd1; | |
char* myfifo = "/tmp/sharedPipe"; | |
mkfifo(myfifo, 0666); | |
char str1[80], str2[80]; | |
while(true){ | |
fd1 = open(myfifo, O_RDONLY); | |
read(fd1, str1, 80); | |
printf("User1: %s\n", str1); | |
close(fd1); | |
fd1 = open(myfifo, O_WRONLY); | |
fgets(str2, 80, stdin); | |
write(fd1, str2, strlen(str2)+1); | |
close(fd1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment