Practice--linux The well-known management FIFO for interprocess communication

Source: Internet
Author: User

To read data from the FIFO:

Convention: If a process blocks the open FIFO in order to read data from the FIFO, the read operation in the process is called a read operation with the blocking flag set.

    • If there is a process write open FIFO and there is no data in the current FIFO, it will be blocked for read operations that have a blocking flag set. For a read operation that does not have a blocking flag set, 1 is returned, and the current errno value is Eagain, reminding you to try again later.
    • For read operations that have a blocking flag set, there are two reasons for blocking: There is data in the current FIFO, but other processes are reading the data, and there is no data in the FIFO. The reason for blocking is that there are new data writes in the FIFO, regardless of the size of the volume of data written, and regardless of the amount of data requested by the read operation.
    • Read Open blocking flag only applies to the first read operation of this process, if there is more than one read sequence in the process, after the first read operation is awakened and the read operation is completed, the other read operation will no longer block, even when performing a read operation, no data in the FIFO is the same (at this point, the read operation returns 0).
    • If there is no process write open FIFO, read operations that have the blocking flag set are blocked.

Note: If there is data in the FIFO, the read operation that sets the blocking flag does not block because the number of bytes in the FIFO is less than the number of bytes requested, and the read operation returns the amount of data that is already in the FIFO.

To write data to the FIFO:

Convention: If a process blocks the open FIFO in order to write data to the FIFO, then the write operation in the process is called a write operation that sets the blocking flag.

For write operations that have a blocking flag set:

    • When the amount of data to be written is not greater than PIPE_BUF, Linux guarantees the atomicity of the write. If the pipe free buffer is not sufficient to hold the number of bytes to write at this time, it goes to sleep until a one-time write is started when the number of bytes to be written is accommodated in the buffer.
    • When the amount of data to be written is greater than Pipe_buf, Linux will no longer guarantee the atomicity of writes. Once the FIFO buffer has an idle area, the write process attempts to write data to the pipeline, and the write is returned after all the requested data has been written.

For write operations that do not have a blocking flag set:

    • When the amount of data to be written is greater than Pipe_buf, Linux will no longer guarantee the atomicity of writes. After all FIFO idle buffers have been written, the write operation returns.
    • When the amount of data to be written is not greater than PIPE_BUF, Linux guarantees the atomicity of the write. If the current FIFO free buffer can hold the number of bytes requested to be written, it returns successfully after completion, and if the current FIFO free buffer does not hold the number of bytes requested to write, the Eagain error is returned and the reminder is later written;

The code may be wrong because there is no output. :)

Wait for a practical use later,

The key now is to understand the idea.

Cleans up the atomicity of blocking and non-blocking reads and writes.

Therefore, there is no one model is the absolute standard, must integrate the business scenario requirements.

/***************** * * test.c* *******************/#include<unistd.h>#include<sys/types.h>#include<fcntl.h>#include<errno.h>#include<string.h>#include<stdio.h>#include<stdlib.h>#defineFifo_server "/tmp/fifoserver"Main (intargcChar**argv) {    intFD; Charw_buf[4096*2]; intReal_wnum; memset (W_buf,0,4096*2); if(Mkfifo (Fifo_server, o_creat| O_EXCL) <0) && (errno! =eexist)) {printf ("Cannot create Fifoserver"); }    if(FD = =-1)    {        if(errno = =Enxio) {printf ("Openerror; no reading process"); }} FD= Open (Fifo_server, o_wronly| O_nonblock,0); //fd = open (fifo_server, o_wronly, 0);Real_wnum = Write (FD, W_BUF,2048); if(Real_wnum = =-1)    {        if(errno = =Eagain) {printf ("write to FIFO error; try late"); }    }    Else{printf ("Real Write num is%d\n", Real_wnum); } real_wnum= Write (fd, W_BUF, the); //real_wnum = Write (FD, W_BUF, 4096);    if(Real_wnum = =-1)    {        if(errno = =Eagain) {printf ("try later\n"); }    }}

Practice--linux The well-known management FIFO for interprocess communication

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.