trying to use mmap on PC with cygwin.

C

comp.unix.shell

I'm trying to get mmap to working, potentially large files.

Here's my data
[a0312850@LTA0312850 ~/tool_box]$ ls -l DATA/LaffAdd.1
-rwx------+ 1 a0312850 ???????? 255 Jul 24 2006 DATA/LaffAdd.1


Here's a snippett of my code
int main(int argc,char* argv[]) {
int token;
if (argc != 2) Help();
char* laff = argv[1];
fp = fopen(laff,"r");
struct stat st;
fstat(fileno(fp),&st);
buflen = st.st_size;
buffer = (char*)mmap(0,buflen,PROT_READ,MAP_SHARED,fileno(fp),0);
bufend = buffer + buflen;
long read = fread(buffer,1,buflen,fp); seg fault here

buflen is 255
Looking at buffer (in ddd) it appears to have mapped it into buffer

a bt shows
(gdb) bt
#0 0x61016545 in stack_info::walk () from /usr/bin/cygwin1.dll
#1 0x7c859dcc in OutputDebugStringA () from /cygdrive/c/WINDOWS/
system32/kernel32.dll
#2 0x40010006 in ?? ()
#3 0x00000000 in ?? ()
 
R

Richard Tobin

I'm trying to get mmap to working, potentially large files.

Well, this is not really a C problem, but:
buffer = (char*)mmap(0,buflen,PROT_READ,MAP_SHARED,fileno(fp),0);
long read = fread(buffer,1,buflen,fp); seg fault here

The idea of memory mapping is that you can access the files as memory,
and don't have to use i/o operations like fread(). buffer should already
contain the data from the file!

The segmentation fault is probably because the memory returned by
mmap() is read-only.

-- Richard
 
F

Flash Gordon

comp.unix.shell wrote, On 13/04/07 16:15:
I'm trying to get mmap to working, potentially large files.

mmap is off topic here. We only deal with standard C, system specific
functions are covered in the relevant system specific groups, such as
comp.unix.programmer for Unix and Unix like systems.
Here's my data
[a0312850@LTA0312850 ~/tool_box]$ ls -l DATA/LaffAdd.1
-rwx------+ 1 a0312850 ???????? 255 Jul 24 2006 DATA/LaffAdd.1


Here's a snippett of my code

Snippets are bad. If you don't know what the problem is how do you know
it is not in the code you have left out? How do we know? You should post
small COMPLETE compilable programs and they should be copied and pasted,
NOT retyped.
int main(int argc,char* argv[]) {
int token;
if (argc != 2) Help();

How can we tell that the Help function has not corrupted memory causing
the fault? We would know if you provided a complete program.
char* laff = argv[1];
fp = fopen(laff,"r");

Have you included stdio.h? We would know if you provided a complete program.

What happens if fopen fails?
struct stat st;

In C89, the most commonly implemented standard, you cannot intermix
statements and definitions. This was added in C99, but very few
compilers completely and correctly implement C99, for example GNU
document that gcc is not C99 compliant yet.
fstat(fileno(fp),&st);

Oh dear, you might have passed a null pointer to fileno, can it handle
it? Can fstat handle whatever fileno returns if it was passed a null
pointer?
buflen = st.st_size;
buffer = (char*)mmap(0,buflen,PROT_READ,MAP_SHARED,fileno(fp),0);

In C it is very rare that you need the cast. A very common reason we see
here for people adding casts is that they have made some other mistake,
such as failing to provide a prototype for a function returning a pointer.
bufend = buffer + buflen;
long read = fread(buffer,1,buflen,fp); seg fault here

buflen is 255
Looking at buffer (in ddd) it appears to have mapped it into buffer

a bt shows
(gdb) bt
#0 0x61016545 in stack_info::walk () from /usr/bin/cygwin1.dll
#1 0x7c859dcc in OutputDebugStringA () from /cygdrive/c/WINDOWS/
system32/kernel32.dll
#2 0x40010006 in ?? ()
#3 0x00000000 in ?? ()

Looks to me like you have clobbered your stack.

If comp.unix.programmer does not cover Cygwin they might still answer
questions about mmap, failing that there are mailing lists for Cygwin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top