Pipelines for linux system programming (2): MPS queue read/write rules

Source: Internet
Author: User

I. MPS queue read/write rules

When no data is readable

  • O_NONBLOCK disable: The read call is blocked, that is, the process is paused until data arrives.
  • O_NONBLOCK enable: The read call returns-1, and the errno value is EAGAIN.

When the MPs queue is full

  • O_NONBLOCK disable: The write call is blocked until a process reads data.
  • O_NONBLOCK enable: The call returns-1, and the errno value is EAGAIN.

If the file descriptor corresponding to the write end of all pipelines is disabled, read returns 0

If the file descriptor corresponding to the read end of all pipelines is disabled, the write operation generates a signal SIGPIPE

When the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of writing.

When the data volume to be written is greater than PIPE_BUF, linux will no longer guarantee the atomicity of writing.

Ii. Verification example

Example 1: O_NONBLOCK disable: The read call is blocked, that is, the process is paused until data arrives.

#include <stdio.h><unistd.h><stdlib.h><fcntl.h>  main( fds[(pipe(fds) == -=(pid == -(pid == ]);        sleep(],,]);     buf[] = {],buf, 

Result:

<Unistd. h> <stdlib. h> <fcntl. h> main (fds [(pipe (fds) =-= (pid =-(pid =]); sleep (],]); buf [] = {flags = fcntl (fds [], F_GETFL); fcntl (fds [], F_SETFL, flags | O_NONBLOCK); = read (fds [], buf, (ret =-

Result:

<Unistd. h> <stdlib. h> <fcntl. h> main (fds [(pipe (fds) =-= (pid =-(pid =]); buf [] = {= read (fds [], buf,

Result:

<Unistd. h> <stdlib. h> <fcntl. h> <signal. h> sighandler (main (fds [(signal (SIGPIPE, sighandler) = (pipe (fds) =-= (pid =-(pid =]); sleep (); = write (fds [], (ret =-sighandler (

Result:

<Unistd. h> <stdlib. h> <fcntl. h> main (fds [(pipe (fds) =-count = (= write (fds [],); (ret =-++

Result:

<Unistd. h> <stdlib. h> <fcntl. h> main (fds [(pipe (fds) =-count = flags = fcntl (fds [], F_SETFL, flags | (= write (fds [],); (ret =-++

Result:

O_NONBLOCKFlag is set (see below ). different implementations have different limits for the pipe capacity. applications shocould not rely on a particle capacity: an application shocould be designed so that a reading process consumes data as soon as it is available, so that a writing process does not remain blocked. in Linux versions before 2.6.11, the capacity of a pipe was the same as the system page size (e.g ., 4096 bytes on i386 ). since Linux 2.6.11, the pipe capacity is 65536 bytes.3. Relationship between pipeline writing and PIPE_BUF

Man help description:
PIPE_BUF
POSIX.1-2001 says that write (2) s of lessPIPE_BUFBytes must be atomic: the output data is written to the pipe as a contiguous sequence. Writes of morePIPE_BUFBytes may be nonatomic: the kernel may interleave the data with data written by other processes. POSIX.1-2001 requiresPIPE_BUFTo be at least 512 bytes. (On Linux,PIPE_BUFIs 4096 bytes.) The precise semantics depend on whether the file descriptor is nonblocking (O_NONBLOCK), Whether there are multiple writers to the pipe, and onN, The number of bytes to be written:O_NONBLOCKDisabled,N<=PIPE_BUFAllNBytes are written atomically; write (2) may block if there is not roomNBytes to be written immediately blocking mode and n <PIPE_BUF: Write is atomic. If there is not enough space for all n bytes to write, it is blocked until there is enough space to write all n Bytes into the pipeline.O_NONBLOCKEnabled,N<=PIPE_BUFIf there is room to writeNBytes to the pipe, then write (2) succeeds immediately, writing allNBytes; otherwise write (2) fails,ErrnoSetEAGAIN. In non-blocking mode and n <PIPE_BUF: Write is atomic and all data is written successfully immediately. Otherwise, no data is written and an error is returned.O_NONBLOCKDisabled,N>PIPE_BUFThe write is nonatomic: the data given to write (2) may be interleaved with write (2) s by other process; the write (2) blocksNBytes have been written. In blocking mode, and n> PIPE_BUF: Is not Atomic. Other processes may write data in the middle until all n bytes are written. Otherwise, the data is blocked and waiting for writing.O_NONBLOCKEnabled,N>PIPE_BUFIf the pipe is full, then write (2) fails,ErrnoSetEAGAIN. Otherwise, from 1NBytes may be written (I. e ., a "partial write" may occur; the caller shocould check the return value from write (2) to see how many bytes were actually written ), and these bytes may be interleaved with writes by other processes.
In non-blocking mode, and N> PIPE_BUF: If the pipeline is full, the system immediately fails. If no data is written, an error is returned. If not, the number of bytes written is 1 ~ N, that is, partial write. Other processes may write data during write.
  • When the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of writing.
  • When the data volume to be written is greater than PIPE_BUF, linux will no longer guarantee the atomicity of writing.

Note: The pipeline capacity is not necessarily equal to PIPE_BUF

Example: When the write data is greater than PIPE_BUF

#include <stdio.h><stdlib.h><.h><unistd.h><sys/types.h><errno.h><fcntl.h> ERR_EXIT(m) \        ( TEST_SIZE 68*1024 main(, , ,  pipefd[ ret = (ret == -= (pid == )= write(pipefd[], a, = (pid == )= write(pipefd[], b, = (pid == )= write(pipefd[], c,  fd = open(, O_WRONLY | O_CREAT | O_TRUNC,  buf[*] = { n =  (= read(pipefd[], buf,  (ret == , n++, getpid(), ret, buf[ 

Result:

It can be seen that the sub-processes are interspersed with each other, and there is no guarantee of atomic writing, and the parent process is read while writing the sub-process.

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.