Solved

mmap gives segmentation fault - pls help

Posted on 2010-09-12
10
665 Views
Last Modified: 2012-05-10
hi,

i tried  a big file and the program gave segmentation fault but when i  tried with a smaller file, mmap works fine. i thought that with mmap, the system only loads the required page. so, it is not necessary to have enough memory on the server.

but now, it looks like when i give the length of the file to mmap, it maps that number of bytes. is this true ?

so, if the system doesn't have enough memory, there would be seg fault ??

thanks a million
0
Comment
Question by:zizi21
  • 4
  • 3
  • 3
10 Comments
 
LVL 3

Accepted Solution

by:
cconstable_work earned 300 total points
ID: 33659478
0
 

Author Comment

by:zizi21
ID: 33659512
thanks for the link but it is confusing. some of the expert advised that the whole file will be loaded and some advised that it will not be loaded.
0
 
LVL 3

Expert Comment

by:cconstable_work
ID: 33659763
A seg fault is not caused by the system not having enough memory.
A seg fault would be from something like trying to write to a location in memory that is not allowed (read only) or not initialized.
perhaps you are unmapping too early?

Can you show the code where you are setting the length?
0
 

Author Comment

by:zizi21
ID: 33659791
i used gdb and i am getting seg fault at memcpy. the code is long, so i am putting parts of it
while(1)
{
      memcpy(&x,&a[x],sizeof(unsigned int));
}

size = s.st_size;
        a = (unsigned int*) mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);

0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33659805
how is the while loop terminating..

i assume.. you map the file first and used it in the loop.

If the address where file is mapped is 'a' and the size mapped is size, then only memory from a to (a+size - 1)  is valid. If you cross that , you will get seg fault. Ofcourse, the end of the mapped segment might be rounded up to a page boundary, but from the programs logic, it shouldnt cross a+size -1 while reading from the mmapped region...

so put a terminating condition on the loop.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 3

Expert Comment

by:cconstable_work
ID: 33659918
Ah I see you are trying to write to 0, this will cause a seq fault on certain operating systems that implement mmap_min_addr protection using LSM http://en.wikipedia.org/wiki/Linux_Security_Modules.
0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33659949
while(1)
{
      memcpy(&x,&a[x],sizeof(unsigned int));
}


also &a[x] dont look correct to me here.... if x is very large.. you will very easily get seg fault
0
 
LVL 5

Assisted Solution

by:shajithchandran
shajithchandran earned 200 total points
ID: 33659952
int pointer = 0;

size = s.st_size;
        a = (unsigned int*) mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, fp, 0);


while(pointer < size)
{
      memcpy(&x,&a[pointer],sizeof(unsigned int));
      pointer++;
      ... rest of the code....
}
0
 

Author Comment

by:zizi21
ID: 33660303
trying it  now..thakns a millon
0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33660342
Also put a check for the return value for mmap... make sure its not failing before proceeding further..
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now