Skip to content
Advertisement

What is the difference between closing [close(3)] and clearing [fd_clr(3)] a file descriptor?

I am having problems with an automated software testing, which is blaming use of freed resource when I use fd_clr(3) after using close(3) in a fd. Is there something wrong with doing this?

for(i = 0; i < nfds; ++i) {
    if (FD_ISSET(i, &myFdSet)) {
        close(i);
        FD_CLR(i, &myFdSet);
    }
}

Advertisement

Answer

FD_CLR() only changes a local fd_set, which is a C data structure to store info about a list of file descriptors.

close() is a system call that closes the file descriptor.

The fd_set is used in the select() system call. With select() you get information about the state of the list of file descriptors that are stored in your fd_set struct.

The reason why you see the FD_CLR() just below the close(), is that there’s no longer need/purpose in asking the state if the closed file descriptor.

Advertisement