// CS22B1067 Skanda S Bhat
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main( )
{
int pipe_fd[ 2 ] ;
char buffer[ 100 ] ;
if ( pipe( pipe_fd) == - 1 ) {
perror ( "Pipe creation failed" ) ; return 1 ;
}
int pid = fork( ) ;
if ( pid < 0 ) {
return 1 ;
}
if ( pid > 0 ) {
close( pipe_fd[ 0 ] ) ;
printf ( "Parent: Enter a message to send to the child: " ) ; fgets ( buffer
, sizeof ( buffer
) , stdin
) ;
write( pipe_fd[ 1 ] , buffer, sizeof ( buffer) ) ;
printf ( "Parent: Message sent to child.\n " ) ;
close( pipe_fd[ 1 ] ) ;
wait( NULL) ;
printf ( "Parent: Child process finished.\n " ) ; }
else {
close( pipe_fd[ 1 ] ) ;
read( pipe_fd[ 0 ] , buffer, sizeof ( buffer) ) ;
printf ( "Child: Message received from parent: %s" , buffer
) ;
close( pipe_fd[ 0 ] ) ;
}
return 0 ;
}
Ly8gQ1MyMkIxMDY3IFNrYW5kYSBTIEJoYXQKI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPHVuaXN0ZC5oPgojaW5jbHVkZTxzeXMvd2FpdC5oPgoKaW50IG1haW4oKQp7CiAgICBpbnQgcGlwZV9mZFsyXTsgIAogICAgY2hhciBidWZmZXJbMTAwXTsKICAgIAoKICAgIGlmIChwaXBlKHBpcGVfZmQpID09IC0xKSB7CiAgICAgICAgcGVycm9yKCJQaXBlIGNyZWF0aW9uIGZhaWxlZCIpOwogICAgICAgIHJldHVybiAxOwogICAgfQogICAgCiAgICBpbnQgcGlkID0gZm9yaygpOyAgCiAgICBpZiAocGlkIDwgMCkgewogICAgICAgIHBlcnJvcigiRm9yayBmYWlsZWQiKTsKICAgICAgICByZXR1cm4gMTsKICAgIH0KCgogICAgaWYgKHBpZCA+IDApIHsKICAgICAgICBjbG9zZShwaXBlX2ZkWzBdKTsgCgogICAgICAgCiAgICAgIC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