Task: Copy a File with mmap()
Navigate to file-descriptors/drills/tasks/mmap_cp and run make to generate support. As you know mmap() can map files in memory, perform operations on them, and then write them back to the disk. Let’s check how well it performs by comparing it to the cp command. The benchmarking is automated by benchmark_cp.sh so focus on completing mmap_cp.c for now.
Quiz: Checkout what syscalls cp uses
- Inside the
tests/directory, you will need to runchecker.sh. The output for a successful implementation should look like this:
./checker.sh
make: Nothing to be done for 'all'.
Test PASSED (File copies are identical)
-
Open
mmap_cp.cand complete the TODOs to map the files in memory and copy the contents. Do not forget to clean up by unmapping and closing the files.To test, run
make test-fileto generate a 1MB file with random data, and then runmmap_cp test-file output.txt. Ensure they have the same content with a simplediff:diff test-file.txt output.txt. -
Compare your implementation to the
cpcommand. Runmake large-fileto generate a 1GB file with random data, and then run./benchmark_cp.sh.Quiz: Debunk why
cpis winningIf you want a more generic answer, checkout this guide on
mmapvsread()-write(). -
This demo would not be complete without some live analysis. Uncomment the calls to
wait_for_input()and rerun the program. In another terminal, runcat /proc/$(pidof mmap_cp)/mapsto see mapped files, andps -o pid,vsz,rss <PID>to see how demand paging happens.