Reputation: 351
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?
Upvotes: 18
Views: 26390
Reputation: 1
They are the same.
Defined in the include/uapi/asm-generic/errno.h
file:
#define EWOULDBLOCK EAGAIN /* Operation would block */
Upvotes: -2
Reputation: 51
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.
Upvotes: 5
Reputation: 6632
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.
Upvotes: 17