Discussion and view of the capacity of pipe under Linux

Source: Internet
Author: User
Tags mutex

1, Pipe capacity

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/84/15/wKiom1eE5D7hkmXVAACpNBUbJIA249.png "title=" screen Shot 2016-07-12 at 20.14.24.png "alt=" Wkiom1ee5d7hkmxvaacpnbubjia249.png "/>

2.6 Standard version of the Linux kernel, the pipe buffer is 64KB, although the command ulimit-a see the pipe size 8 blocks, the size of the buffer is not 4 K, because the kernel dynamically allocates a maximum of 16 "buffer entries", multiply K. These restrictions are hard-coded


2. How to see how much pipe you have on your PC

1) through ulimit-a see pipe size One atom write as: 512bytes*8=4096bytes

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/84/14/wKioL1eE3hGS_jaGAACB789kNz8179.png "title=" 2016-07-12 19-45-54 screen. png "alt=" Wkiol1ee3hgs_jagaacb789knz8179.png "/>

To view the number of buffer entries: cat/usr/src/kernels/3.10.0-327.el7.x86_64/include/linux/pipe_fs_i.h file

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/84/15/wKiom1eE5RfBPJF9AAAeEWYxh-g988.png "title=" 2016-07-12 20-39-15 screen. png "alt=" Wkiom1ee5rfbpjf9aaaeewyxh-g988.png "/>

So my PC's pipe buffer size is: 16*4096=65536bytes

It also verifies the pipe capacity under Man 7 pipe.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/84/14/wKioL1eE5rmTsnvUAABb0VgHReg024.png "title=" 2016-07-12 19-47-30 screen. png "alt=" Wkiol1ee5rmtsnvuaabb0vghreg024.png "/>


3, pipe of internal organization way

In Linux, the implementation of pipelines does not use a dedicated data structure, but instead uses the file structure of the filesystem and the index node inode of the VFS. By pointing two file structures to the same temporary VFS index node, the VFS index node points to a physical page.

There are two file data structures, but they define file operation routines where the address is different, one is a routine address that writes data to the pipeline, and the other is a routine address that reads data from the pipeline. In this way, the system call of the user program is still the usual file operation, but the kernel uses this abstraction mechanism to realize the special operation of the pipeline.

cat/usr/src/kernels/3.10.0-327.el7.x86_64/include/linux/pipe_fs_i.h file

#ifndef  _linux_pipe_fs_i_h#define _linux_pipe_fs_i_h#define pipe_def_buffers16#define pipe _buf_flag_lru0x01/* page is on the lru */#define  pipe_buf_flag_atomic0x02/*  was atomically mapped */#define  pipe_buf_flag_gift0x04/* page is a  gift */#define  pipe_buf_flag_packet0x08/* read ()  as a packet *//**  *struct pipe_buffer - a linux kernel pipe buffer * @page:  the page containing the data for the pipe buffer * @offset:  offset of data inside the  @page  * @len:  length of data inside  the  @page  * @ops:  operations associated with this buffer. see   @pipe_buf_operations.  * @flags:  pipe buffer flags. see above. *@ Private: private data owned by the ops. **/struct pipe_buffer {struct page *page; Unsigned int offset, len;const struct pipe_buf_operations *ops;unsigned int  flags;unsigned long private;};/ ** *struct pipe_inode_info - a linux kernel pipe * @mutex:  mutex  protecting the whole thing * @wait:  reader/writer wait point in  case of empty/full pipe * @nrbufs: the number of non-empty  pipe buffers in this pipe * @buffers:  total number of buffers   (should be a power of 2)  * @curbuf: the current pipe  buffer entry * @tmp_page:  cached released page * @readers:  number of  current readers of this pipe * @writers:  NUMBER OF&Nbsp;current writers of this pipe * @files:  number of struct file  refering this pipe  (protected by ->i_lock)  * @waiting_writers:  number of writers blocked waiting for room * @r_counter: reader  counter * @w_counter:  writer counter * @fasync_readers:  reader side fasync  * @fasync_writers:  writer side fasync * @bufs: the circular array  Of pipe buffers **/struct pipe_inode_info {struct mutex mutex;wait_queue_ head_t wait;unsigned int nrbufs, curbuf, buffers;unsigned int readers; unsigned int writers;unsigned int files;unsigned int waiting_writers;unsigned  int r_counter;unsigned int w_counter;struct page *tmp_page;struct fasync_ Struct *fasync_readers;struct fasync_struct *fasync_writers;struct pipe_buffer *bufs;};/ * * note on the nesting of these functions: * * -> Confirm ()  *->steal ()  *... *->map ()  *... *->unmap ()  * * that  is, ->map ()  must be called on a confirmed buffer, *  same goes for ->steal ().  see below for the meaning of  each * operation. also see kerneldoc in fs/pipe.c for the  pipe * and generic variants of these hooks. */struct pipe_ buf_operations {/* * this is set to 1, if the generic  Pipe read/write may coalesce * data into an existing buffer.  if this is set to 0, a&nBsp;new pipe * page segment is always used for new data.  */int can_merge;/* * ->map ()  returns a virtual address  Mapping of the pipe buffer. * the last integer flag reflects  whether this should be an atomic * mapping or not. the  atomic map is faster, however you can ' t take * page  Faults before calling ->unmap ()  again. So if you need to  Eg * access user data through copy_to/from_user (),  then you must  get * a non-atomic map. ->map ()  uses the kmap_atomic  slot for * atomic maps, you have to be careful if  mapping another page as * source or destination for a copy. */void *  (*map) ( Struct pipe_inode_info *, struct pipe_buffer *, int);/* * Undoes  ->map (),  finishes the virtual mapping of the pipe buffer. */ void  (*unmap) (struct pipe_inode_info *, struct pipe_buffer *, void *) ;/* * ->confirm ()  verifies that the data in the pipe  buffer is there * and that the contents are good. if  The pages in the pipe belong * to a file system, we  may need to wait for io completion in this * hook.  returns 0 for good, or a negative error value in case  of * error. */int  (*confirm) (struct pipe_inode_info *, struct pipe_buffer *);/* *  when the contents of this pipe buffer has been completely  * consumed by a reader, ->release ()  is called. */void  (* Release) (struct pipe_inode_info *, struct pipe_buffer *);/* * attempt  to take ownership of the pipe buffer and its contents. *  ->steal ()  returns 0 for success, in which case the  contents * of the pipe  (the buf->page)  is locked and now  completely owned * by the caller. the page may then be  transferred to a different * mapping, the most often used  case is insertion into different * file address space cache. */int  (*steal ) (struct pipe_inode_info *, struct pipe_buffer *);/* * get a  reference to the pipe buffer. */void  (*get) (struct pipe_inode_info *,  struct pipe_buffer *);};/ * differs from pipe_buf in that pipe_size is the length of  the actual   memory allocation, whereas PIPE_BUF makes  atomicity guarantees.  */#define  PIPE_SIZEPAGE_SIZE/* Pipe lock and  Unlock operations */void pipe_lock (struct pipe_inode_info *); Void pipe_unlock ( struct pipe_inode_info *); Void pipe_double_lock (struct pipe_inode_info *, struct  pipe_inode_info *); extern unsigned int pipe_max_size, pipe_min_SIZE;INT PIPE_PROC_FN (struct ctl_table *, int, void __user *, size_t  *, loff_t *);/* drop the inode semaphore and wait for  A pipe event, atomically */void pipe_wait (Struct pipe_inode_info *pipe); Struct pipe_inode_info *alloc_pipe_info (void); Void free_pipe_info (Struct pipe_inode_info  *);/* generic pipe buffer ops functions */void *generic_pipe_buf_map ( Struct pipe_inode_info *, struct pipe_buffer *, int); void generic_pipe_buf_ Unmap (struct pipe_inode_info *, struct pipe_buffer *, void *);void  Generic_pipe_buf_get (struct pipe_inode_info *, struct pipe_buffer *); int generic _pipe_buf_confirm (struct pipe_inode_info *, struct pipe_buffer *); int generic_ Pipe_buf_steal (struct pipe_inode_info *, struct pipe_buffer *); Void generic_pipe_buf_release (struct  pipe_inode_info *, struct pipe_buffer *);/* for f_setpipe_sz and  f_getpipe_sz */long pipe_fcntl (struct file *, unsigned int, unsigned  LONG ARG); Struct pipe_inode_info *get_pipe_info (Struct file *file);int  Create_pipe_files (struct file **, int); #endif

Extract the above files for an important structure

Inode node Information structure struct inode {...    struct pipe_inode_info  *i_pipe ;...  };//number of pipeline buffers #define pipe_buffers  (16)//Pipe buffer object Structure struct pipe_buffer {     struct page *page; //descriptor address of the Pipe buffer page box     unsigned int  offset, len; //the current position of valid data in the page box, and the length of the valid data     struct pipe_buf_operations * ops; //Pipeline Buffer Method table address};//pipeline information structure struct pipe_inode_info {    wait_queue_head_t  wait; //Pipeline Waiting Queue     unsigned int nrbufs, curbuf;      //the number of buffers containing the data to be read and the index of the first buffer containing the data to be read     struct pipe_buffer bufs[pipe_ buffers]; //Pipe Buffer Descriptor Array     struct page *tmp_page; //cache area Page box pointer      unsigned int start;  //current pipeline buffer read Location     unsigned  int readers; //ReadFlag of process, or number     unsigned int writers; //write process flag, or number      unsigned int waiting_writers; //the number of write processes in the waiting queue for sleep     unsigned int r _counter; //is similar to readers, but when the process waiting to write to the FIFO is using     unsigned int w_counter; // Similar to writers, but used when waiting for a process to write to the FIFO     struct fasync_struct *fasync_readers; // asynchronous I/O notification via signal     struct fasync_struct *fasync_writers; //for asynchronous I/O notification via signal} ;


This article from "Momo is spicy moe" blog, please be sure to keep this source http://momo462.blog.51cto.com/10138434/1825852

Discussion and view of the capacity of pipe under Linux

Related Article

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.