From 471fc6d274b01a4ae2a72831309e1c9a29c89114 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 14 Apr 2026 19:12:10 +0200 Subject: [PATCH] pipe.c --- AnimalProcessing/Fundamentals/pipe.c | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) 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();}