14

I need to understand the difference between both EAGAIN and EWOULDBLOCK as I have seen many source code are checking against EAGAIN only (may be both the codes represent same number, Correct me here.)

My part of knowledge: For blocking socket if sender buffer is full and receiver is not receiving any data,Sender will get hanged if call send(). This is because once data is read by the receiver the space it was using in the buffer is made available for new data. If your socket is in 'non blocking' mode then the 'send()' will fail with 'EAGAIN' or 'EWOULDBLOCK'.

Are they always the same number or is there any scenario where they need to be treated differently. ?

ppandey
  • 311
  • 2
  • 3
  • 9
  • 3
    The reasons for two symbolic constants is probably historic, and these days they should not be treated differently at all (and in most implementations of the IP-stack can't, since they are defined as the same numeric value). If you want to learn more, then putting your title into your favorite search engine should give you more than enough to read. – Some programmer dude Mar 01 '18 at 12:11
  • 1
    Possible duplicate of [Which systems define EAGAIN and EWOULDBLOCK as different values?](https://stackoverflow.com/questions/7003234/which-systems-define-eagain-and-ewouldblock-as-different-values) – Steffen Ullrich Mar 01 '18 at 12:54

3 Answers3

15

In short: they're almost always the same value, but for portability it's recommended to check for both values (and treat both values the same way).

For most systems, EAGAIN and EWOULDBLOCK will be the same. There are only a few systems in which they are different, and you can see the list of those systems in this answer.

Even the errno manpage mentions that they "may be the same [value]".

Historically, however, EWOULDBLOCK was defined for "operation would block" - that is, the operation would have blocked, but the descriptor was placed in non-blocking mode. EAGAIN originally indicated when a "temporary resource shortage made an operation impossible". The example used by the gnu documentation is when there are not enough resources to fork(). Because the resource shortage was expected to be temporary, a subsequent attempt to perform the action might succeed (hence the name "again").

Practically speaking, those types of temporary resource shortages are not that common (but pretty serious when they do occur).

Most systems define these values as the same, and the systems which don't will become more and more uncommon in the future. Nevertheless, for portability reasons you should check for both values, but you should also treat both errors in the same way. As the GNU documentation states:

Portability Note: In many older Unix systems ... [EWOULDBLOCK was] a distinct error code different from EAGAIN. To make your program portable, you should check for both codes and treat them the same.

cegfault
  • 6,442
  • 3
  • 27
  • 49
4

They are functionally the same. The reason for the two different names is historic going back to the 1980s. EWOULDBLOCK was used on BSD/Sun variants of Unix, and EAGAIN was the AT&T System V error code.

For a compiled binary on a particular system the codes should have the same value. The reason both names are defined in include files is for source code portability.

-1

They are the same.
Defined in the include/uapi/asm-generic/errno.h file:

#define EWOULDBLOCK EAGAIN  /* Operation would block */
FFng
  • 1
  • 1