Unix Programming - Sharing a file through mmap

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > Sharing a file through mmap





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Sharing a file through mmap
Peter Nolan

2005-05-14, 1:23 pm

Hi,
I am new to mmap on unix....I have written some ETL software and
introduced memory mapped IO for my win2000+ version..I am now trying to
introduce memory mapped IO for my unix version.....

I have written code that will (I hope) load the data I want into memory
mapped IO...however, when writing the code to gain access to the map I
have a question....I have the statement below...


IF ((ptr_MemMapBuffer = (char *) mmap((caddr_t) 0, 0 ,
(PROT_READ|PROT_WRITE), MAP_SHARED, fp_MemMapBuffer, 0)) == MAP_FAILED)
THEN
BEGIN_
// some error and return
CALL_RETURNED_OK = False ;
return CALL_RETURNED_OK ;
END_

At this point in this program I do not know the size of the file
pointed to by fp_MemMapBuffer. In the win2000 equivalent call when you
ask for the mapping to be made if you specify 0 as a length it will
allow the program access to the entire file in memory. I've searched
in the documentation but I cannot see if mmap defaults to allowing
access to the entire file or if I need to run some function to find the
size of the entire file before I call mmap.....And if I do, does anyone
know the function to call against a file pointer opened with the 'open'
statement to get the size in bytes of the file?

Thanks

Peter Nolan
www.peternolan.com

Måns Rullgård

2005-05-14, 1:23 pm

"Peter Nolan" <peter@peternolan.com> writes:

> Hi,
> I am new to mmap on unix....I have written some ETL software and
> introduced memory mapped IO for my win2000+ version..I am now trying to
> introduce memory mapped IO for my unix version.....
>
> I have written code that will (I hope) load the data I want into memory
> mapped IO...however, when writing the code to gain access to the map I
> have a question....I have the statement below...
>
> IF ((ptr_MemMapBuffer = (char *) mmap((caddr_t) 0, 0 ,
> (PROT_READ|PROT_WRITE), MAP_SHARED, fp_MemMapBuffer, 0)) == MAP_FAILED)
> THEN
> BEGIN_
> // some error and return
> CALL_RETURNED_OK = False ;
> return CALL_RETURNED_OK ;
> END_


What language is that?

> At this point in this program I do not know the size of the file
> pointed to by fp_MemMapBuffer. In the win2000 equivalent call when you
> ask for the mapping to be made if you specify 0 as a length it will
> allow the program access to the entire file in memory. I've searched
> in the documentation but I cannot see if mmap defaults to allowing
> access to the entire file or if I need to run some function to find the
> size of the entire file before I call mmap.....And if I do, does anyone
> know the function to call against a file pointer opened with the 'open'
> statement to get the size in bytes of the file?


fstat

--
Måns Rullgård
mru@inprovide.com
Michael B Allen

2005-05-15, 8:35 am

On Sat, 14 May 2005 10:19:32 -0700, Peter Nolan wrote:

> pointed to by fp_MemMapBuffer. In the win2000 equivalent call when you
> ask for the mapping to be made if you specify 0 as a length it will
> allow the program access to the entire file in memory. I've searched
> in the documentation but I cannot see if mmap defaults to allowing
> access to the entire file or if I need to run some function to find the
> size of the entire file before I call mmap.


You need to use stat or fstat to determine the size first. Or if you're
creating the file you need to manually grow the file with ftruncate.

Mike

Måns Rullgård

2005-05-15, 8:35 am

Michael B Allen <mba2000@ioplex.com> writes:

> On Sat, 14 May 2005 10:19:32 -0700, Peter Nolan wrote:
>
>
> You need to use stat or fstat to determine the size first.


Using fstat is preferred. Someone might rename the file between calls
to open and stat.

--
Måns Rullgård
mru@inprovide.com
Peter Nolan

2005-05-15, 5:49 pm

Hi Mans,
thanks for that.....I've been using C++ classes for file access and
I've forgotten the older C ones.....and I have never used mmap
before....just learning that bit....

The language is C++....however, I apply a large number of pre-processor
statements to make the source code read like PL/I which was my first
language......I can read/write PL/I many times faster than I can
read/write C++....many people have told me I should just learn C++
better but heck, it works for me... :-).....and I could convert it to
C++ just by doing change alls of the source code if I wanted to.....

Best Regards
Peter Nolan
www.peternolan.com

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com