#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include<string.h>
int main( ) {
int pipefd1[ 2 ] ; //msg from parent to child
int pipefd2[ 2 ] ; //msg from child to child1
char write_msg1[ 100 ] ;
char write_msg2[ 100 ] ;
char read_msg1[ 100 ] ;
char read_msg2[ 100 ] ;
if ( pipe
( pipefd1
) ==- 1 ) { printf ( "error" ) ; return 1 ; } if ( pipe
( pipefd2
) ==- 1 ) { printf ( "error" ) ; return 1 ; }
printf ( "Enter the msg sent from parent to child:" ) ; printf ( "Enter the msg sent from child to child1:" ) ;
pid_t pid_child;
pid_child = fork( ) ;
if ( pid_child
< 0 ) { printf ( "ERROR IN CHILD\n " ) ; return 1 ; } else if ( pid_child== 0 ) {
close( pipefd1[ 1 ] ) ; // close writing
read( pipefd1[ 0 ] , read_msg1, sizeof ( read_msg1) ) ;
printf ( "THE MSG FROM PARENT IS :%s \n " , read_msg1
) ; close( pipefd1[ 0 ] ) ;
close( pipefd2[ 0 ] ) ;
write
( pipefd2
[ 1 ] , write_msg2
, strlen ( write_msg2
) + 1 ) ; printf ( "MSG SENT TO CHILD1 IS :%s \n " , write_msg2
) ; close( pipefd2[ 1 ] ) ;
pid_t pid_child1;
pid_child1 = fork( ) ;
if ( pid_child1
< 0 ) { printf ( "ERROR IN CHILD1\n " ) ; return 1 ; } else if ( pid_child1== 0 ) {
close( pipefd2[ 1 ] ) ; // close writing
read( pipefd2[ 0 ] , read_msg2, sizeof ( read_msg2) ) ;
printf ( "THE MSG FROM CHILD IS :%s \n " , read_msg2
) ; close( pipefd2[ 0 ] ) ;
} else { wait( NULL) ; }
} else {
close( pipefd1[ 0 ] ) ;
write
( pipefd1
[ 1 ] , write_msg1
, strlen ( write_msg1
) + 1 ) ; printf ( "MSG SENT TO CHILD IS :%s \n " , write_msg1
) ; close( pipefd1[ 1 ] ) ;
wait( NULL) ;
}
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx1bm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