Created
May 29, 2015 11:03
-
-
Save bbqtd/c781ae43a642e1011180 to your computer and use it in GitHub Desktop.
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 <unistd.h> | |
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <sys/wait.h> | |
const int k_BufferSize = 255; | |
int main() | |
{ | |
int pair_soc[2]; | |
char buf[k_BufferSize]; | |
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair_soc) == -1) { | |
perror("socketpair"); | |
return 1; | |
} | |
puts("Full-duplex pipe"); | |
if (!fork()) { | |
while (1) { | |
read(pair_soc[1], &buf, k_BufferSize); | |
printf("[Process 2]: <<< %s", buf); | |
printf("[Process 2]: >>> "); | |
fgets(buf, k_BufferSize, stdin); | |
write(pair_soc[1], &buf, k_BufferSize); | |
puts(""); | |
} | |
} else { | |
while (1) { | |
printf("[Process 1]: >>> "); | |
fgets(buf, k_BufferSize, stdin); | |
write(pair_soc[0], buf, k_BufferSize); | |
puts(""); | |
read(pair_soc[0], &buf, k_BufferSize); | |
printf("[Process 1]: <<< %s", buf); | |
} | |
wait(NULL); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment