linux pipe文件系统(pipefs)

Linux的pipe和FIFO基于pipefs,提供进程间通信。pipe只能在创建进程及其后代间使用,FIFO则不受此限。本文深入探讨pipefs的数据结构,包括pipe_buffer、pipe_inode_info,以及pipe_buf_operations等,解析pipe读写操作的内部机制,如缓存管理、信号通知和异步I/O。
摘要由CSDN通过智能技术生成

linux的pipe,FIFO(named pipe)都是基于pipe文件系统(pipefs)的。
linux的pipe和FIFO都是半双工的,即数据流向只能是一个方向;
pipe只能在pipe的创建进程及其后代进程(后代进程fork/exec时,通过继承父进程的打开文件描述符表)之间使用,来实现通信;
FIFO是有名称的pipe,即可以通过名称查找到pipe,所以没有“只能在pipe的创建进程及其后代进程之间使用”的限制;通过名称找到pipe文件,创建相应的pipe,就可以实现进程间的通信。

I.数据结构
i.pipe_buffer
管道缓存,用于暂存写入管道的数据;写进程通过管道写入端将数据写入管道缓存中,读进程通过管道读出端将数据从管道缓存中读出

/* include/linux/pipe_fs_i.h */
  8 #define PIPE_BUF_FLAG_LRU       0x01    /* page is on the LRU */
  9 #define PIPE_BUF_FLAG_ATOMIC    0x02    /* was atomically mapped */
 10 #define PIPE_BUF_FLAG_GIFT      0x04    /* page is a gift */
 11 
 12 /**
 13  *      struct pipe_buffer - a linux kernel pipe buffer
 14  *      @page: the page containing the data for the pipe buffer
 15  *      @offset: offset of data inside the @page
 16  *      @len: length of data inside the @page
 17  *      @ops: operations associated with this buffer. See @pipe_buf_operations.
 18  *      @flags: pipe buffer flags. See above.
 19  *      @private: private data owned by the ops.
 20  **/
 21 struct pipe_buffer {
 22         struct page *page;
 23         unsigned int offset, len;
 24         const struct pipe_buf_operations *ops;
 25         unsigned int flags;
 26         unsigned long private;
 27 };

page:页帧,用于存储pipe数据;pipe缓存与页帧是一对一的关系
offset:页内偏移,用于记录有效数据在页帧的超始地址(只能用偏移,而不能用地址,因为高内存页帧在内核空间中没有虚拟地址与之对应)
len:有效数据长度
ops:缓存操作集
flags:缓存标识
private:缓存操作私有数据

 

ii. pipe_buf_operations
用于存储管道缓存操作集

/* include/linux/pipe_fs_i.h */
 60 /*
 61  * Note on the nesting of these functions:
 62  *
 63  * ->confirm()
 64  *      ->steal()
 65  *      ...
 66  *      ->map()
 67  *      ...
 68  *      ->unmap()
 69  *
 70  * That is, ->map() must be called on a confirmed buffer,
 71  * same goes for ->steal(). See below for the meaning of each
 72  * operation. Also see kerneldoc in fs/pipe.c for the pipe
 73  * and generic variants of these hooks.
 74  */
 75 struct pipe_buf_operations {
 76         /*
 77          * This is set to 1, if the generic pipe read/write may coalesce
 78          * data into an existing buffer. If this is set to 0, a new pipe
 79          * page segment is always used for new data.
 80          */
 81         int can_merge;
 82 
 83         /*
 84          * ->map() returns a virtual address mapping of the pipe buffer.
 85          * The last integer flag reflects whether this should be an atomic
 86          * mapping or not. The atomic map is faster, however you can't take
 87          * page faults before calling ->unmap() again. So if you need to eg
 88          * access user data through copy_to/from_user(), then you must get
 89          * a non-atomic map. ->map() uses the KM_USER0 atomic slot for
 90          * atomic maps, so you can't map more than one pipe_buffer at once
 91          * and you have to be careful if mapping another page as source
 92          * or destination for a copy (IOW, it has to use something else
 93          * than KM_USER0).
 94          */
 95         void * (*map)(struct pipe_inode_info *, struct pipe_buffer *, int);
 96 
 97         /*
 98          * Undoes ->map(), finishes the virtual mapping of the pipe buffer.
 99          */
100         void (*unmap)(struct pipe_inode_info *, struct pipe_buffer *, void *);
101 
102         /*
103          * ->confirm() verifies that the data in the pipe buffer is there
104          * and that the contents are good. If the pages in the pipe belong
105          * to a file system, we may need to wait for IO completion in this
106          * hook. Returns 0 for good, or a negative error value in case of
107          * error.
108          */
109         int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
110 
111         /*
112          * When the contents of this pipe buffer has been completely
113          * consumed by a reader, ->release() is called.
114          */
115         void (*release)(struct pipe_inode_info *, struct pipe_buffer *);
116 
117         /*
118          * Attempt to take ownership of the pipe buffer and its contents.
119          * ->steal() returns 0 for success, in which case the contents
120          * of the pipe (the buf->page) is locked and now completely owned
121          * by the caller. The page may then be transferred to a different
122          * mapping, the most often used case is insertion into different
123          * file address space cache.
124          */
125         int (*steal)(struct pipe_inode_info *, struct pipe_buffer *);
126 
127         /*
128          * Get a reference to the pipe buffer.
129          */
130         void (*get)(struct pipe_inode_info *, struct pipe_buffer *);
131 };

can_merge:合并标识;如果pipe_buffer中有空闲空间,有数据写入时,如果can_merge置位,会先写pipe_buffer的空闲空间;否则重新分配一个pipe_buffer来存储写入数据
map:由于pipe_buffer的page可能是高内存页帧,由于内核空间页表没有相应的页表项,所以内核不能直接访问page;只有通过map将page映射到内核地址空间后,内核才能访问
unmap:map的逆过程;因为内核地址空间有限,所以page访问完后释文地址映射
confirm:检验pipe_buffer中的数据
release:当pipe_buffer中的数据被读完后,用于释放pipe_buffer
get:增加pipe_buffer的引用计数器

 

iii.pipe_inode_info
管道描述符,用于表示一个管道,存储管道相应的信息

/* include/linux/pipe_fs_i.h */
 29 /**
 30  *      struct pipe_inode_info - a linux kernel pipe
 31  *      @wait: reader/writer wait point in case of empty/full pipe
 32  *      @nrbufs: the number of non-empty pipe buffers in this pipe
 33  *      @curbuf: the current pipe buffer entry
 34  *      @tmp_page: cached released page
 35  *      @readers: number of current readers of this pipe
 36  *      @writers: number of current writers of this pipe
 37  *      @waiting_writers: number of writers blocked waiting for room
 38  *      @r_counter: reader counter
 39  *      @w_counter: writer counter
 40  *      @fasync_readers: reader side fasync
 41  *      @fasync_writers: writer side fasync
 42  *      @inode: inode this pipe is attached to
 43  *      @bufs: the circular array of pipe buffers
 44  **/
 45 struct pipe_inode_info {
 46         wait_queue_head_t wait;
 47         unsigned int nrbufs, curbuf;
 48         struct page *tmp_page;
 49         unsigned int readers;
 50         unsigned int writers;
 51         unsigned int waiting_writers;
 52         unsigned int r_counter;
 53         unsigned int w_counter;
 54         struct fasync_struct *fasync_readers;
 55         struct fasync_struct *fasync_writers;
 56         struct inode *inode;
 57         struct pipe_buffer bufs[PIPE_BUFFERS];
 58 };

wait:读/写/poll等待队列;由于读/写不可能同时出现在等待的情况,所以可以共用等待队列;poll读与读,poll写与写可以共存出现在等待队列中
nrbufs:非空的pipe_buffer数量
curbuf:数据的起始pipe_buffer
tmp_page:页缓存,可以加速页帧的分配过程;当释放页帧时将页帧记入tmp_page,当分配页帧时,先从tmp_page中获取,如果tmp_page为空才从伙伴系统中获取
readers:当前管道的读者个数;每次以读方式打开时,readers加1;关闭时readers减1
writers:当前管道的写者个数;每次以写方式打开时,writers加1;关闭时writers减1
waiting_writers:被阻塞的管道写者个数;写进程被阻塞时,waiting_writers加1;被唤醒时,waiting_writers
r_counter:管道读者记数器,每次以读方式打开管道时,r_counter加1;关闭是不变
w_counter:管道读者计数器;每次以写方式打开时,w_counter加1;关闭是不变
fasync_readers:读端异步描述符
fasync_writers:写端异步描述符
inode:pipe对应的inode
bufs:pipe_buffer回环数据

 

iv.以上结构关系图



II.pipe_buf_operations
pipe_buf_operations主要用于记录pipe缓存的操作集:

/* fs/pipe.c */
 319 static const struct pipe_buf_operations anon_pipe_buf_ops = {
 320         .can_merge = 1,
 321         .map = generic_pipe_buf_map,
 322      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值