C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-10-2009, 09:27 AM   #1
Registered User
 
Join Date: Oct 2006
Posts: 263
mmap fails with errno EBADF

In my program I am opening a file as follows:

Code:
std::string filename = "somefile";
struct stat st;
stat(filename.c_str(), &st);
int fd = open(filename.c_str(), O_RDONLY);
the open succeeds, and gives me a valid file number.
I then attempt to memory-map the file using mmap() as follows:

Code:
unsigned char* buf = (unsigned char*)mmap(NULL, st.st_size, PROT_READ, fd, MAP_PRIVATE | MAP_POPULATE, 0);
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."

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 2.6.22.5-31-default
2GB of system memory + 4GB swap
GCC version 4.2.1

Last edited by Elkvis; 09-10-2009 at 09:30 AM. Reason: add more info
Elkvis is offline   Reply With Quote
Old 09-10-2009, 09:51 AM   #2
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
You got your parameters to mmap() mixed up.

gg
Codeplug is offline   Reply With Quote
Old 09-10-2009, 09:58 AM   #3
Registered User
 
Join Date: Oct 2006
Posts: 263
thanks! it would have taken me many hours to spot that, being inexperienced with mmap and its friends.
Elkvis is offline   Reply With Quote
Old 09-10-2009, 10:03 AM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
This is all I've ever done with mmap(), but it worked:

seeking for exercises

Looking at that, it appears I used a file opened O_RDWR|O_CREAT, not O_RDONLY, but since your mmap call is PROT_READ I guess that should be okay. The other difference is in the flags at the end...which it looks to me like you have the flags parameter and the fd parameter backward!
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
errno function, error TLS/non TLS fran.b C Programming 13 03-25-2009 08:06 AM
BSD mmap for shared memory - am I right? sean Linux Programming 21 03-09-2009 01:57 PM
using mmap for copying large files rohan_ak1 C Programming 6 05-13-2008 08:12 AM
Function argument assignment between types "unsigned int*" and "unsigned long*" nadeer78 C Programming 8 03-10-2008 11:57 AM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM


All times are GMT -6. The time now is 01:24 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22