Skip to content

Instantly share code, notes, and snippets.

@cho0h5
Created December 18, 2021 03:57
Show Gist options
  • Save cho0h5/1556fdabac35879dc2f87b82d56881d9 to your computer and use it in GitHub Desktop.
Save cho0h5/1556fdabac35879dc2f87b82d56881d9 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <stdio.h>
int main() {
char buffer[10] = { 0, };
int fd[2];
if(pipe(fd) == -1) printf("pipe failed");
printf("fd[0] == %d\n", fd[0]);
printf("fd[1] == %d\n", fd[1]);
pid_t pid = fork();
if(pid > 0) {
write(fd[1], "hi", 3);
// read(fd[0], buffer, 10);
// printf("%s\n", buffer);
} else if(pid == 0) {
read(fd[0], buffer, 10);
printf("%s\n", buffer);
// write(fd[1], "hi", 3);
}
return 0;
}
// fd[0] == 3
// fd[1] == 4
// hi
// pipe()를 통해 pipe 하나를 생성
// fd[1]에 write를 하고 fd[0]를 read하여 사용 가능
// pipe를 하나 가지고 양방향 데이터 전송을 하기는 쉽지 않다
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment