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