Hi,
I am trying my hands with unix socket programming in C. But while reading I am getting Err No as 4. I am unable to find out the description of this error code. Does anybody have any idea?
Hi,
I am trying my hands with unix socket programming in C. But while reading I am getting Err No as 4. I am unable to find out the description of this error code. Does anybody have any idea?
If you would start with looking at ultimate source of Unix error code names (/usr/include/errno.h
) you'll arrive at the file which contains your error code as
#define EINTR 4 /* Interrupted system call */
(Which is this file is left for you to find out, as an exercise ;))
The errno
values can be different for different systems (even different Unix-like systems), so the symbolic constants should be used in code.
The perror
function will print out (to stderr ) a descriptive string of the last errno
value along with a string you provide.
man 3 perror
The strerror
function simply returns a const char *
to the string that perror
prints.
If 4 is EINTR
on your system then you received a signal during your call to read. There are ways to keep this from interrupting your system calls, but often you just need to:
while (1) {
ssize_t x = read(file, buf, len);
if (x < 0) {
if (errno == EINTR) {
errno = 0;
continue;
} else {
// it's a real error
If you're getting EINTR
, it probably means you've installed a signal handler improperly. Good unices will default to restartable system calls when you simply call signal
, but for safety, you should either use the bsd_signal
function if it's available, or call sigaction
with the restartable flag to avoid the headaches of EINTR
.