Solved

mmap gives segmentation fault - pls help

Posted on 2010-09-12
10
674 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

777 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