鍍金池/ 問答/C  Linux  網(wǎng)絡(luò)安全/ 管道為什么是半雙工的呢?

管道為什么是半雙工的呢?

從源碼的角度來看,為什么管道不得不是半雙工的呢?

回答
編輯回答
夏夕

Linux的管道實(shí)現(xiàn)是個(gè)環(huán)形緩沖區(qū):

/**
 *    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 current writers of this pipe
 *    @files: number of struct file referring 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
 *    @user: the user who created this pipe
 **/
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;
    struct user_struct *user;
};

curbuf是當(dāng)前緩存區(qū)的下標(biāo),每個(gè)緩沖區(qū)里有offset和len記錄數(shù)據(jù)寫到的位置,讀寫的時(shí)候是要修改這些信息的。

如果兩個(gè)進(jìn)程同時(shí)進(jìn)行讀或者同時(shí)進(jìn)行寫,必要會(huì)導(dǎo)致數(shù)據(jù)沖突,所以內(nèi)核會(huì)對(duì)管道上鎖(pipe_inode_info里的mutex),所以是半雙工的。

比較簡單的實(shí)現(xiàn)可以看xv6的實(shí)現(xiàn)

struct pipe {
  struct spinlock lock;
  char data[PIPESIZE];
  uint nread;     // number of bytes read
  uint nwrite;    // number of bytes written
  int readopen;   // read fd is still open
  int writeopen;  // write fd is still open
};

直接一塊連續(xù)的內(nèi)存data,用兩個(gè)數(shù)字nread、nwrite記錄讀寫數(shù),通過PIPESIZE取模得到在data上的讀寫位置,用自旋鎖保護(hù)。如果沒有鎖,兩個(gè)進(jìn)程就能同時(shí)寫數(shù)據(jù)到data和修改nwrite,數(shù)據(jù)也就沖突了,同時(shí)讀也同理。

2017年6月27日 20:31