Once mmap succeeded, it's no longer necessary to keep the underlying fd
open. Therefore just close them as an attempt to reduce file descriptors
opened by the process.
Thanks for spectral from Google for pointing out that fd could be closed.
singhsrb |
Restricted Project |
Once mmap succeeded, it's no longer necessary to keep the underlying fd
open. Therefore just close them as an attempt to reduce file descriptors
opened by the process.
Thanks for spectral from Google for pointing out that fd could be closed.
make clean local rt test-cstore.t test-*pack*.t
Automatic diff as part of commit; lint not applicable. |
Automatic diff as part of commit; unit tests not applicable. |
If the FD are closed, does the mmap still work if the file is deleted? Does that count against the open file limit?
Yes. I think the only problematic case is truncating the file, which also breaks the old code.
Does that count against the open file limit?
I think so. Let me do an experiment.
I tried the following and it works without hitting fd limit.
#include <string> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> using namespace std; char *dommap(const char *path) { int fd = open(path, O_RDONLY); char *p = (char *)mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0); close(fd); return p; } int main(int argc, char const *argv[]) { for (int i = 0; i < 40960; ++i) { printf("%d\n", i); auto dest = string("/tmp/true-") + to_string(i); system((string("cp /bin/true ") + dest).c_str()); char *p = dommap(dest.c_str()); if (strncmp(p + 1, "ELF", 3) != 0) { printf("p is not 'ELF' !\n"); abort(); } } return 0; }