68 lines
1.5 KiB
C
68 lines
1.5 KiB
C
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <signal.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <string.h>
|
||
|
#include <fcntl.h>
|
||
|
|
||
|
void syserr( const char *msg ) {
|
||
|
perror(msg);
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
main()
|
||
|
{
|
||
|
int rfd, wfd;
|
||
|
char buff[64096];
|
||
|
int nb;
|
||
|
int ed;
|
||
|
|
||
|
remove("duck");
|
||
|
|
||
|
if( mkfifo( "duck", 0666 ) == -1 ) {
|
||
|
syserr( "mkfifo" );
|
||
|
}
|
||
|
|
||
|
switch( fork() ) {
|
||
|
case -1: syserr("fork" ); break;
|
||
|
case 0: // child
|
||
|
printf( "Child awakes\n" );
|
||
|
if( (rfd = open( "duck", O_RDWR )) == -1 ) {
|
||
|
syserr( "open for reading" );
|
||
|
}
|
||
|
printf("Child: open completes\n");
|
||
|
|
||
|
for( ; ; ) {
|
||
|
if( (nb = read( rfd, buff, 200 )) == -1 ) {
|
||
|
syserr( "read" );
|
||
|
}
|
||
|
if( nb == 0 ) {
|
||
|
printf("No more data\n");
|
||
|
printf("child terminates 0\n");
|
||
|
exit( 0 );
|
||
|
}
|
||
|
buff[nb] = '\0';
|
||
|
printf("nb = %d Buff = |%s|\n", nb, buff);
|
||
|
}
|
||
|
break;
|
||
|
default: // parent
|
||
|
printf( "Parent awake\n" );
|
||
|
if( (wfd = open( "duck", O_RDWR )) == -1 ) {
|
||
|
syserr( "open for writing" );
|
||
|
}
|
||
|
printf("Parent opened pipe to write\n");
|
||
|
|
||
|
for( ed = 0; ed < 4; ed++ ) {
|
||
|
sprintf( buff, "duckie %d", ed + 1 );
|
||
|
if(write( wfd, buff, strlen(buff)) != strlen(buff)) {
|
||
|
syserr( "write" );
|
||
|
}
|
||
|
}
|
||
|
printf("parent terminates\n");
|
||
|
close(wfd);
|
||
|
remove("duck");
|
||
|
sleep(1);
|
||
|
}
|
||
|
}
|