• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

mmap gives segmentation fault - pls help

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
zizi21
Asked:
zizi21
  • 4
  • 3
  • 3
2 Solutions
 
cconstable_workCommented:
0
 
zizi21Author Commented:
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
 
cconstable_workCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zizi21Author Commented:
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
 
shajithchandranCommented:
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
 
cconstable_workCommented:
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
 
shajithchandranCommented:
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
 
shajithchandranCommented:
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
 
zizi21Author Commented:
trying it  now..thakns a millon
0
 
shajithchandranCommented:
Also put a check for the return value for mmap... make sure its not failing before proceeding further..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now