mmap & copying files
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > mmap & copying files




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    mmap & copying files  
Alex Vinokur


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 01:34 PM

I would like to copy files using mmap.

int fd_in = open("in_file", O_RDONLY);
int fd_in = open("out_file", O_WRONLY);

char* ptr = (char*)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_in, 0);
assert ((ptr == MAP_FAILED));

void* rc =  (char*)mmap(ptr, file_size, PROT_WRITE, MAP_SHARED, fd_out, 0);
assert ((ptr == MAP_FAILED));

Is the code above correct?

--
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html








[ Post a follow-up to this message ]



    Re: mmap & copying files  
Casper H.S. Dik


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 01:34 PM

"Alex Vinokur" <alexvn@big.foot.com> writes:

>I would like to copy files using mmap.

>int fd_in = open("in_file", O_RDONLY);
>int fd_in = open("out_file", O_WRONLY);

>char* ptr = (char*)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_in, 0);
>assert ((ptr == MAP_FAILED));

>void* rc =  (char*)mmap(ptr, file_size, PROT_WRITE, MAP_SHARED, fd_out, 0);
>assert ((ptr == MAP_FAILED));

>Is the code above correct?

No, apart from the "asserts" being incorrect and the (char *) casts
being wrong, the above doesn't do anything close to what you hope for.

What you will need to do is:


- mmap() the source file
- write() the destination file.

Using mmap() for write is very tricky and should be avoided.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.





[ Post a follow-up to this message ]



    Re: mmap & copying files  
Alex Vinokur


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 01:34 PM


"Casper H.S. Dik" <Casper.Dik@Sun.COM> wrote in message news:4083c471$0$561$
e4fe514c@news.xs4all.nl...
[snip]
> Using mmap() for write is very tricky and should be avoided.
[snip]

What does 'PROT_WRITE usage' mean in mmap?

--
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html










[ Post a follow-up to this message ]



    Re: mmap & copying files  
Casper H.S. Dik


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 02:34 PM

"Alex Vinokur" <alexvn@big.foot.com> writes:


>"Casper H.S. Dik" <Casper.Dik@Sun.COM> wrote in message news:4083c471$0$561
$e4fe514c@news.xs4all.nl...
>[snip] 
>[snip]

>What does 'PROT_WRITE usage' mean in mmap?

That you can write to the pages mapped; they are mapped from the
file descriptor you passed in; it does not mean that you are remapping
the pages mapped by "ptr" to be written to the file; in fact, mmap()
will ignore the "ptr" argument and create a new mapping for the file
to be written.
PROT_WRITE generally used for "PROT_READ|PROT_WRITE" mappings; that's becaus
e:

- you can't grow the size of a file using mmap()
(references beyond last page will result in a SIGSEGV)
- a "disk full" condition for writes to unallocated pages
will also be met with a "SIGSEGV".

Also, note that most systems implement PROT_WRITE as "PROT_READ|PROT_WRITE"
because "write only" memory is often nonsensical or not supported by the
hardware.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.





[ Post a follow-up to this message ]



    Re: mmap & copying files  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 02:34 PM

In article <c60f7a$6dc76$1@ID-79865.news.uni-berlin.de>,
"Alex Vinokur" <alexvn@big.foot.com> wrote:

> I would like to copy files using mmap.
>
> int fd_in = open("in_file", O_RDONLY);
> int fd_in = open("out_file", O_WRONLY);

That should be fd_out.

>
> char* ptr = (char*)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_in, 0);
> assert ((ptr == MAP_FAILED));

Shouldn't that be != ?

>
> void* rc =  (char*)mmap(ptr, file_size, PROT_WRITE, MAP_SHARED, fd_out, 0)
;
> assert ((ptr == MAP_FAILED));

Same thing.

>
> Is the code above correct?

You need to extend out_file to file_size bytes first.  Mmap() doesn't
allow you to map beyond the file's current length.

The more common technique is to mmap() the source file, and just write
the entire thing to the destination file:

write(fd_out, ptr, file_size);

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***





[ Post a follow-up to this message ]



    Re: mmap & copying files  
Alex Vinokur


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 02:34 PM


"Barry Margolin" <barmar@alum.mit.edu> wrote in message news:barmar-F2F24B.08541519042004@co
mcast.ash.giganews.com...
> In article <c60f7a$6dc76$1@ID-79865.news.uni-berlin.de>,
>  "Alex Vinokur" <alexvn@big.foot.com> wrote:
[snip] 
>
> That should be fd_out.
Of course, my mistake. Thanks.

> 
>
> Shouldn't that be != ?

assert ((ptr != MAP_FAILED));
See above "Of course, my mystake. Thanks."
> 
>
> Same thing.
The same thing.
[snip]

--
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html








[ Post a follow-up to this message ]



    Re: mmap & copying files  
Alex Vinokur


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 04:35 PM


"Barry Margolin" <barmar@alum.mit.edu> wrote in message news:barmar-F2F24B.08541519042004@co
mcast.ash.giganews.com...
> In article <c60f7a$6dc76$1@ID-79865.news.uni-berlin.de>,
>  "Alex Vinokur" <alexvn@big.foot.com> wrote:
> 

[snip]
[vbcol=seagreen]
> The more common technique is to mmap() the source file, and just write
> the entire thing to the destination file:
>
> write(fd_out, ptr, file_size);
>


Thanks.

The testsuite has been added to C/C++ Performance Tests
(Comparative performance measurement : Copying files)
http://article.gmane.org/gmane.comp...+.perfometer/43
http://article.gmane.org/gmane.comp...+.perfometer/42

--
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html







[ Post a follow-up to this message ]



    Re: mmap & copying files  
Andre Majorel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 05:35 PM

In article <4083cbcb$0$557$e4fe514c@news.xs4all.nl>, Casper H.S Dik wrote:

> Also, note that most systems implement PROT_WRITE as
> "PROT_READ|PROT_WRITE" because "write only" memory is often
> nonsensical or not supported by the hardware.

Although Signetics made a laudable effort.

http://www.teaser.fr/~amajorel/misc/humour/wom1.jpg
http://www.teaser.fr/~amajorel/misc/humour/wom2.jpg

--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
"Finally I am becoming stupider no more." -- Paul Erdös' epitaph





[ Post a follow-up to this message ]



    Re: mmap & copying files  
Markus Gyger


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 05:35 PM

"Alex Vinokur" <alexvn@big.foot.com> writes:
> I would like to copy files using mmap.

Possibly using sendfile() or sendfilev() [on Solaris] instead might be
more efficient; see e.g. http://linuxgazette.com/issue91/tranter.html


Markus





[ Post a follow-up to this message ]



    Re: mmap & copying files  
Markus Gyger


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-19-04 05:35 PM

Barry Margolin <barmar@alum.mit.edu> writes:
> The more common technique is to mmap() the source file, and just write
> the entire thing to the destination file:
>
> write(fd_out, ptr, file_size);

Possibly using madvise(ptr, file_size, MADV_SEQUENTIAL); in between...


Markus





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 05:40 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register