Vernacular/graphic FIFO (famous Pipeline) Execution Process

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.