pipe.c
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <err.h>
|
||||
|
||||
|
||||
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();}
|
||||
|
||||
Reference in New Issue
Block a user