[LinuxIPC] 管道的_PC_PIPE_BUFF语义和O_NONBLOCK语义的理解

Write requests to a pipe or FIFO shall be handled in the same way as a regular file  with  the  following  excep-
       tions:

        * There  is no file offset associated with a pipe, hence each write request shall append to the end of the pipe.

        * Write requests of {PIPE_BUF} bytes or less shall not be interleaved  with  data  from  other  processes  doing
          writes  on  the  same  pipe.  Writes  of greater than {PIPE_BUF} bytes may have data interleaved, on arbitrary
          boundaries, with writes by other processes, whether or not the O_NONBLOCK flag of the  file  status  flags  is
          set.

        * If  the  O_NONBLOCK  flag is clear, a write request may cause the thread to block, but on normal completion it
          shall return nbyte.

        * If the O_NONBLOCK flag is set, write() requests shall be handled differently, in the following ways:

           * The write() function shall not block the thread.

           * A write request for {PIPE_BUF} or fewer bytes shall have the following effect: if there is sufficient space
             available in the pipe, write() shall transfer all the data and return the number of bytes requested. Other-
             wise, write() shall transfer no data and return -1 with errno set to [EAGAIN].

           * A write request for more than {PIPE_BUF} bytes shall cause one of the following:

              * When at least one byte can be written, transfer what it can and return the number of bytes written. When
                all data previously written to the pipe is read, it shall transfer at least {PIPE_BUF} bytes.

              * When no data can be written, transfer no data, and return -1 with errno set to [EAGAIN].

我觉得这段说的比较清晰, 唯一特别的就是非阻塞情况下写大于PIPE_BUF的数据在管道空的情况下会保证至少写入PIPE_BUF, 其他的我认为只要你根据返回值和errno都可以通用化的解决写入量问题, 不用考虑那么复杂, 这种问题我很早就研究过了, 最后发现我基本不会多个进程/线程写PIPE/FIFO, 就算写, 通常都是<PIPE_BUF的通知1字节.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值