mmap fails with errno EBADF
In my program I am opening a file as follows:
the open succeeds, and gives me a valid file number.
std::string filename = "somefile";
struct stat st;
int fd = open(filename.c_str(), O_RDONLY);
I then attempt to memory-map the file using mmap() as follows:
I then check the return value of mmap() and find that it is equal to MAP_FAILED. I print the return value of strerror(errno) to stdout and it says "Bad file descriptor."
unsigned char* buf = (unsigned char*)mmap(NULL, st.st_size, PROT_READ, fd, MAP_PRIVATE | MAP_POPULATE, 0);
the file is exactly 51200000 bytes long (an integer multiple of the 4096 byte page size), and I'm not asking it to map to a specific or non-aligned memory address.
not sure what I'm doing wrong here, but I just can't seem to make this work.
system is OpenSuse 10.3 x86 kernel version 18.104.22.168-31-default
2GB of system memory + 4GB swap
GCC version 4.2.1