Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'EAGAIN' or 'EWOULDBLOCK'

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. ?

like image 728
ppandey Avatar asked Mar 01 '18 12:03

ppandey


People also ask

What does Ewouldblock mean?

EAGAIN/EWOULDBLOCK means resources temporarily unavailable. The call might work if you try later. An example is the case of non-blocking IO operation that will block.

When the kernel will return an error Ewouldblock?

The Linux man page for write(2) states that EWOULDBLOCK would only be returned for a file descriptor that refers to a socket. The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the write would block.

Is socket send blocking?

In case of blocking socket: The send() will block if the kernel buffer is not free enough to intake the data provided to send() call. Non blocking sockets: send() will not block, but would fail and returns -1 or it may return number of bytes copied partially(depending on the buffer space available).

How do I make a non blocking socket?

To mark a socket as non-blocking, we use the fcntl system call. Here's an example: int flags = guard(fcntl(socket_fd, F_GETFL), "could not get file flags"); guard(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK), "could not set file flags"); Here's a complete example.


1 Answers

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.

like image 136
cegfault Avatar answered Oct 11 '22 14:10

cegfault