Linux下EAGAIN宏的含义

Posted 清水寺扫地僧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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提示其再调用一次(也许下次就能成功)。

以上是关于Linux下EAGAIN宏的含义的主要内容,如果未能解决你的问题,请参考以下文章

linux __user 宏的含义是啥?

linux非阻塞的socket EAGAIN的错误处理

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

linux C语言 EAGAIN(EWOULDBLOCK)标志位

linux C语言 EAGAIN(EWOULDBLOCK)标志位

Flask模板宏的概念和基本使用