![]() |
| | #1 |
| Registered User Join Date: Oct 2006
Posts: 263
| mmap fails with errno EBADF Code: std::string filename = "somefile"; struct stat st; stat(filename.c_str(), &st); int fd = open(filename.c_str(), O_RDONLY); 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); 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 | |
| | #2 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| You got your parameters to mmap() mixed up. gg |
| Codeplug is offline | |
| | #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 | |
| | #4 |
| subminimalist 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |