use strict; use warnings; use Fcntl; use Inline C => Config => LIBS => '', BUILD_NOISY => 1; use Inline C => <<'END_OF_C_CODE'; #include #include #include int unmap_region(void *m, int sz){ if( munmap(m, sz) != 0 ){ fprintf(stderr, "unmap_region() : error, call to munmap() has failed for memory address %p and size %ld : %s\n", m, sz, strerror(errno)); return 0; // failed } return 1; // success } void *map_region(int fd, int sz){ char *map = (char *)mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0); if (map == MAP_FAILED){ fprintf(stderr, "map has failed for size %ld : %s\n", sz, strerror(errno)); return (void *)NULL; // failed } return map; // success } END_OF_C_CODE my $test_file = 'test_mmap.bin'; sysopen(my $fh, $test_file, O_RDWR|O_CREAT|O_TRUNC) or die "can't open file '$test_file' : $!"; binmode $fh; my $size = 4096; my $addr = map_region(fileno($fh), $size) or die "mmap failed!!: $^E "; my $test_str = "Hello Linux mmap!"; syswrite($fh, $test_str); unmap_region($addr, $size) or die "fail to unmap"; close($fh); open($fh, '<', $test_file) or die "failed to open file '$test_file'/2, $!"; my $contents; { local $/ = undef; $contents = <$fh> } close $fh; print "Sucess, contents of the mmap'ed file '$test_file':\n${contents}\n--end contents.\n";