You can check it with select and poll . Select will return EBADF if the file descriptor is closed:
fd_set rfds; struct timeval tv = { 0, 1 }; int retval; FD_ZERO(&rfds); FD_SET(fd, &rfds); retval = select(1, &rfds, NULL, NULL, &tv); if (retval == -1 && errno == EBADF)
NOTE that I have not tested this code, but the documentation reports this.
In addition, this is not entirely accurate, since errno can be EBADF for various reasons.
EDIT: Another thing: if you open the file immediately after closing another, it will most likely have the same fd as the previous one, so this only works halfway if you want to check if all files were closed.
source share