Open two terminals, execute export o_read on the first terminal, create a famous pipeline, and wait for Input
Then, execute commit o_write on the second terminal. After that, commit o_write writes data to the pipeline. At this time, the output pipeline will have readable data, so it is awakened and read data from the pipeline, and displayed.
/* Export o_read.c */
# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <string. h>
# Include <fcntl. h>
# Include <errno. h>
# Include <sys/time. h>
# Include <unistd. h>
# Define upload o_file "/tmp/FIFO. tmp"
# Define maxlen 1024
Int main ()
{
Int FIFO, FD;
Char Buf [maxlen];
Int Len, I = 0;
Fd_set set;
Struct timeval TV;
Unlink (export o_file );
If (FIFO = mkfifo (writable o_file, o_rdwr) <0)
{
Perror ("mkfifo error \ n ");
Return 0;
}
If (FD = open (fifo_file, o_rdwr) <0)
{
Perror ("Open mkfifo error \ n ");
Return 0;
}
Fd_zero (& set );
// Fd_set (FD, & set );
TV. TV _sec = 5;
TV. TV _usec = 0;
While (1)
{
Fd_set (FD, & set );
If (I = select (FD + 1, & set, null, null, & TV)> 0)
{
Printf ("have data in pipe \ n ");
If (fd_isset (FD, & set ))
{
Len = read (FD, Buf, maxlen );
// Buf [Len] = '/0 ';
Printf ("Buf = % s \ n", Buf );
TV. TV _sec = 5;
TV. TV _usec = 0;
}
}
Else if (I = 0)
{
TV. TV _sec = 5;
TV. TV _usec = 0;
Printf ("time out \ n ");
}
}
Unlink (export o_file );
Return 0;
}
// # GCC export o_read.c-O export o_read
# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <string. h>
# Include <fcntl. h>
# Include <errno. h>
# Define upload o_file "/tmp/FIFO. tmp"
# Define maxlen 1024
Int main ()
{
Int FIFO, FD;
Char Buf [maxlen];
Int Len, I = 0;
Memset (BUF, 0, sizeof (BUF ));
Strcpy (BUF, "10 ");
If (FIFO = open (fifo_file, o_rdwr) <0)
{
Perror ("Open mkfifo error \ n ");
Return 0;
}
While (I <10)
{
Sprintf (BUF, "% d", I + 1 );
Len = write (FIFO, Buf, strlen (BUF ));
Printf ("Send Len = % d \ n", Len );
Sleep (1 );
I ++;
}
Return 0;
}
// # GCC export o_write.c-O export o_write