As a system call to close the file descriptor
close()
Is provided.
Therefore, I will write about the pitfalls of the close system call, which is often overlooked.
#include <unistd.h>
int close(int fd);
For example, when calling a file I / O system call or library function
eintr
Is annoying, and there are cases where an error occurs when a function call is interrupted.
EINTR See function call interrupted (POSIX.1); signal (7).
Reference: Linux Programmer's Manual --ERRNO
In that case, even if it fails as follows, if it is EINTR
,
There is a workaround to provide a wrapper function to retry and prevent processing failure in EINTR
.
/* wrapper function of send () */
ssize_t send_wrap(int sockfd, const void *buf, size_t len, int flags)
{
int res = 0;
do {
res = send(sockfd, buf, len, flags);
} while ((res == -1) && (errno == EINTR));
return res;
}
There is also a failure due to ʻEINTR` in close (). However, retries on the ** close () system call are dangerous. ** **
So why is it dangerous to retry when close () fails? For example, in the case of multithreading, another thread may close the reused fd.
The Linux kernel releases the fd early in the process inside close (),
The processing that can cause an error is located in the latter half of close ()
.
In other words, the process is returned as an error, but fd is ** released **, so If it is multithreaded, ** another thread may be reusing the fd **, There is a risk that ** the fd will be closed ** if it is retried.
From the above, it can be said that it is better not to retry the close ()
system call when it fails.
http://man7.org/linux/man-pages/man2/close.2.html
Recommended Posts