1. name of the file
2. the offset within the file from where the contents have to be printed
3. length in bytes from the offset that have to be printed on to stdout.
Let us take some input for our understanding. Let the size of the file be 6500 bytes
1. name of the file is foo.txt
2. offset is 5000
3. length = 1000 bytes
As per the user input, mmap should now copy the file contents from the offet 5000 to 6000 (offset + length) to the memory and map this memory to the virtual address (symbol addr in the program) of the current task.
Since the offset parameter of mmap should be page aligned, pa_offset will be 4096 (0x1000) and thus mmap will copy the file contents from the offset 4096 to 6000.
The following will be the mmap parameters, considering the page size as 4K.
addr = mmap(NULL, 1000+5000-4096, PROT_READ,MAP_PRIVATE, fd, 4096 )
The virtual address returned, say 0x08054000, will be accessing the data of the file from the offset 4096, but the program has to print 1000 (length) bytes of contents from the offset 5000. i.e. from the virtual address 0x08054000 + 904 (5000 - 4096) i.e effectively (addr + offset - pa_offset) that is the 2nd parameter of the write system call.
write (STDOUT_FILENO, 0x08054000 + 5000 - 4096, 1000).
Hope this clarifies,
Regards,
Prabhu
On Mon, Sep 20, 2010 at 2:29 PM, mohit verma <mohit89mlnc@xxxxxxxxx> wrote:
hi all ,
i was reading manual of mmap()
there i got stuck at the line :
< s = write(STDOUT_FILENO, addr + offset - pa_offset, length); >
can anyone please describe me what is going on in this line?