900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Linux下EAGAIN宏的含义

Linux下EAGAIN宏的含义

时间:2019-03-19 10:36:42

相关推荐

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

EAGAINThe 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提示其再调用一次(也许下次就能成功)。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。