|
Home > Archive > Unix Programming > April 2004 > mmap & copying files
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 |
mmap & copying files
|
|
| Alex Vinokur 2004-04-19, 8:34 am |
| 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
| |
| Casper H.S. Dik 2004-04-19, 8:34 am |
| "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.
| |
| Alex Vinokur 2004-04-19, 8:34 am |
|
"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
| |
| Casper H.S. Dik 2004-04-19, 9:34 am |
| "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 because:
- 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.
| |
| Barry Margolin 2004-04-19, 9:34 am |
| 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 ***
| |
| Alex Vinokur 2004-04-19, 9:34 am |
|
"Barry Margolin" <barmar@alum.mit.edu> wrote in message news:barmar-F2F24B.08541519042004@comcast.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
| |
|
|
|
|
| Markus Gyger 2004-04-19, 12: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
| |
| Markus Gyger 2004-04-19, 12: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
|
|
|
|
|