diff --git a/AnimalProcessing/Fundamentals/pipe.c b/AnimalProcessing/Fundamentals/pipe.c index e69de29..5b6ba81 100644 --- a/AnimalProcessing/Fundamentals/pipe.c +++ b/AnimalProcessing/Fundamentals/pipe.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include + + +void piped(){ + + + int fd[2]; + if(pipe(fd) == -1){ + printf("an error occured\n"); + return ; + } + + + pid_t pid = fork(); + + + if(pid < 0){ + printf("an error occured\n"); + return ; + } + + // Child + if (pid == 0) { + + close(fd[0]); + + char *str = "Hello from your child !\n"; + + write(fd[1], str, strlen(str) + 1); + close(fd[1]); + + exit(0); + } + // Mommy + else { + close(fd[1]); + + char buffer[2048]; + read(fd[0], buffer, sizeof(buffer)); + + printf("My child sends me : %s\n", buffer); + close(fd[0]); + wait(NULL); + + return; + } +} + + +int main(){ piped();}