Linux下EAGAIN宏的含义

在Linux环境下开发经常会碰到很多错误(设置errno),其中EAGAIN是其中比较常见的一个错误(比如用在非阻塞操作中)。在man手册关于read的解释如下:


RETURN VALUE
    On success, the number of bytes read is returned(zero indicates end of file), and the file position is
    advanced by this number. It is not an error if this number is smaller than the number of bytes requested;
    this may happen for example because fewer bytes are actually available right now (maybe because we
    were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read()
    was interrupted by a signal. See also NOTES.

ERRORS

    EAGAIN The file decriptor fd refers to a file other than a socket and has been marked nonblocking
        (O_NONBLOCK), and the read would block. See open(2) for futher details on the O_NONBLOCK
        flag.


   EAGAIN or EOWOULDBLOCK

    The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read
    would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these
    constants to have the same value, so a portable application should check for both possibilities.


从字面上来看,是提示再试一次。这个错误经常出现在当应用程序进行一些非阻塞(non-blocking)操作(对文件或socket)的时候。例如,以O_NONBLOCK的标志打开file/socket/FIFO,如果你连续做read操作而没有数据可读。此时程序不会阻塞起来等待数据准备就绪返回,read函数会返回一个错误EAGAIN,提示你的应用程序现在没有数据可读请稍后再试。

又例如,当一个系统调用(比如fork)因为没有足够的资源(比如虚拟内存)而执行失败,返回EAGAIN提示其再调用一次(也许下次就能成功)。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值