Linux programming-Read and Write the FIFO (chapter 13th)

Source: Internet
Author: User

4. Read and write the FIFO Use of the O_nonblock mode affects the read and write calls to the FIFO.
To an empty, blocked FIFO
(That is, it is not opened with the O_nonblock flag) The read call will wait until there is data that can be read to continue execution. On the contrary, A read call to an empty, non-blocking FIFO will immediately return 0 bytes.
Write calls to a fully blocked FIFO will wait until the data can be written to continue execution
. if the non-blocking FIFO cannot receive all of the written data, it will execute according to the following rules.
If the requested data length is less than or equal to Pipe_buf bytes, the call fails and the data cannot be written.
If the requested data length is greater than pipe_buf bytes, part of the data will be written, returning the actual number of bytes written, and the return value may be 0.
The length of the FIFO is a very important factor to consider,The system has a limit on the length of data that can exist in a FIFO at any one time, as defined by the # define PIPE_BUF statement, which can usually be found in the header file limits.h. In Linux and many other Unix-like systems, its value is typically 4096 bytes, In some systems, however, it may be as small as 512 bytes. The system specifies that in a FIFO opened in o_wronly mode (that is, blocking mode), if the data written is less than or equal to pipe_buf, either all bytes are written, or none of the bytes are written.
Although, this limitation is not very important for a simple case where there is only one FIFO write process and one FIFO read process, However, it is common to use only one FIFO and allow multiple different programs to send requests to a FIFO read process .If several different programs try to write data to the FIFO at the same time, it is critical to ensure that the databases from different programs are not interleaved with each other. That is to say, each write operation must be "atomized". How do you do that?
If you can guarantee that all write requests are destined for a blocking FIFO, and that the data length of each request is less than or equal to Pipe_buf bytes, the system can determine that the data blocks will not be interleaved together. It is often a good idea to limit the length of data passed through the FIFO to pipe_buf bytes every time. Unless only one write process and read process are used.
Using FIFO for interprocess communication
To demonstrate how unrelated processes communicate using named pipes, you need two separate programs fifo3.c and fifo4.c
/************************************************************************* > File name:fifo3.c > Description:  The FIFO3.C program is a producer program that creates pipelines when needed, and then writes data to the pipeline as much as possible > author:liubingbing > Created time:2015 July 14 Tuesday 21:28 28 seconds > other:fifo3.c ************************************************************************/#include <stdio.h># Include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include < limits.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" #define Buffer_ SIZE pipe_buf#define Ten_meg (1024x768 * 1024x768) int main () {int pipe_fd;int res;int Open_mode = O_wronly;int bytes_sent = 0; Char Buffer[buffer_size + 1];/* checks to see if the Fifo_name file exists and creates it if it does not exist */if (Access (fifo_name, F_OK) = =-1) {/* Mkfifo create a named pipe (that is, a special type of text FIFO) */res = Mkfifo (Fifo_name, 0777), if (res! = 0) {fprintf (stderr, "Could not create FIFO%s\n", fifo_name); Exit (Exit_f ailure);}} printf ("Process%d opening Fifo_owroNly\n ", Getpid ());/* The Open function opens the FIFO file o_wronly, if successful pipe_fd points to the Open file */PIPE_FD = open (Fifo_name, Open_mode);p rintf (" Process%d result%d\n ", Getpid (), PIPE_FD), if (pipe_fd! =-1) {/* */while (Bytes_sent < Ten_meg) {/* Write function is pointing from buffer Write Buffer_size bytes into the pipe_fd file * If successful returns the actual number of bytes written */res = write (pipe_fd, buffer, buffer_size); if (res = =-1) {fprintf (Stder R, "Write error on pipe\n"); exit (exit_failure);} Bytes_sent + = res;} (void) Close (PIPE_FD);} else {exit (exit_failure);} printf ("Process%d finished\n", getpid ()); exit (exit_success);}
The first program is the producer program, which creates the pipeline when needed, and then writes the data to the pipeline as much as possible
/************************************************************************* > File name:fifo4.c > Description:        The FIFO4.C program is a consumer program that reads data from a FIFO and discards them > author:liubingbing > Created time:2015 July 14 Tuesday 22:07 34 seconds > Other: fifo4.c ************************************************************************/#include <stdio.h># Include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include < limits.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" #define Buffer_ SIZE Pipe_bufint Main () {int pipe_fd;int res;int Open_mode = O_rdonly;char buffer[buffer_size + 1];int bytes_read = 0;memse T (buffer, ' fifo_name ', sizeof (buffer));p rintf ("Process%d opening FIFO o_rdonly\n", Getpid ());/* The Open function opens the Open_ file to Mode mode (i.e. o_rdonly) * If successful, returns the file descriptor */pipe_fd = open (Fifo_name, Open_mode);p rintf ("Process%d result%d\n", getpid (), Pipe _FD); if (pipe_fd! =-1) {do {* * Read function reads from PIPE_FD point to file buffer_size bytes of data to buffer pointing to the memory * If successful, returns the actual number of bytes read into the data */res = Read (pipe_fd, buffer, buffer_size); Bytes_read + = res;} while (res > 0);(void) Close (PIPE_FD);} else {exit (exit_failure);} printf ("Process%d finished,%d bytes read\n", Getpid (), bytes_read); exit (exit_success);}
The second program is the consumer program, its code is much simpler, it reads the data from the FIFO and discards them.
While the two programs are running, the read process is timed with the time command, and the output is as follows:
$./fifo3 &
$ time./fifo4

Two programs use the FIFO of the blocking mode, first start Fifo3 (write process/producer), it will block to wait for the read process to open after the Fifo.fifo4 (consumer) startup, the write process unblocked and began to write data to the pipeline. Also, the read process begins to read data from the pipeline .
Linux Schedules The schedules between the two processes so that they run when they can run and block when they are not running. Therefore, the write process will block when the pipeline is full, and the read process will block when the pipeline is empty.
The output of the time command shows that The read process only runs for less than 0.1 seconds, but reads 10MB of data, which shows that pipelines (at least in today's Linux system implementations) are efficient at passing data between programs.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux programming-Read and Write the FIFO (chapter 13th)

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.