linux pipe 非阻塞,阻塞和非阻塞打开命名管道有什么区别吗?

rt

|

When attempting to read from an empty pipe or FIFO:

* If no process has the pipe open for writing, read() shall return 0 to indicate end-of-file.

* If some process has the pipe open for writing and O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].

* If some process has the pipe open for writing and O_NONBLOCK is clear, read() shall block the calling thread until some data is

written or the pipe is closed by all processes that had the pipe open for writing.

When attempting to read from an empty pipe or FIFO:

* If no process has the pipe open for writing, read() shall return 0 to indicate end-of-file.

* If some process has the pipe open for writing and O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].

* If some process has the pipe open for writing and O_NONBLOCK is clear, read() shall block the calling thread until some data is

written or the pipe is closed by all processes that had the pipe open for writing.

|

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

* 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  pro‐

cesses, 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. Otherwise,  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 previ‐

ously 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].

上面贴重了,这个是写.

|

O_RDONLY, O_WEONLY, O_NONBLOCK四种组合,阻塞情况比较简单,非阻塞稍有不同,自己查下资料吧

|

阻塞:在读写数据的时候如果不能读或写的时候程序将一直等待在这里,直到读或些被执行或者设置的超时时间到达才会返回。

非阻塞:如果不能立即读写程序立即返回。

|

管道和FIFO的阻塞与非阻塞说明直接参考man open即可,很详细。

因为管道和FIFO和其他IPC可能有点区别,因为只要一次性写字节数小于PIPE_SIZE属于原子写,如果容量不足那么将会阻塞,如果大于PIPE_SIZE那么不属于原子写,有多少容量写多少,如果一点都写不了应该是阻塞住的。

上边是阻塞情况,非阻塞情况下,原子写如果容量不足直接返回错误,并EAGAIN。 非原子写有多少容量写多少,如果一点都写不了返回-1并且EAGAIN。

读方面的话,都是有多少返回多少,如果一点都没有,阻塞的就阻塞,非阻塞的就返回错误EAGAIN。

像我们编程,无论哪一种IPC,我们只要遵从一个原则永远也不会出错,就是非阻塞描述符,检查返回值和错误即可,因为非阻塞描述符:

1.返回>0,说明写/读了多少字节,你就可以知道有多少没写/读成功。

2,返回 File    # 写字符串到"File".

exec 3 File             # 打开"File"并且给它分配fd 3.

read -n 4 &3             # 写一个小数点.

exec 3>&-                 # 关闭fd 3.

cat File                  # ==> 1234.67890

|

楼上在说shell。。我们在说unix c

|

写和读是独立的,而且管道是FIRST IN FIRST OUT, 只要保证只有一个写端,一个读端,那么谁阻塞谁不阻塞没什么问题. 如果是多个写端,那得看数据写入量是否小于PIPE_SIZE,小于是原子写,大于的话多个写端需要同步.自己不懂多man一下,看不懂英语是不行的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值