Created
March 30, 2017 06:04
-
-
Save apage43/9dbe388ddb36f54beef78af360756465 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 'make' doesn't need a makefile to compile basic C programs | |
$ make shr && ./shr | grep 'Vm\|\[\|Rss' | |
[P] Parent, pre munmap | |
VmPeak: 23708 kB | |
VmSize: 23708 kB | |
VmLck: 0 kB | |
VmPin: 0 kB | |
VmHWM: 20656 kB | |
VmRSS: 20656 kB | |
RssAnon: 88 kB | |
RssFile: 1076 kB | |
RssShmem: 19492 kB | |
VmData: 56 kB | |
VmStk: 140 kB | |
VmExe: 8 kB | |
VmLib: 1888 kB | |
VmPTE: 64 kB | |
VmPMD: 12 kB | |
VmSwap: 0 kB | |
[C] In child 2280363, parent 2280361 | |
[C] Child sees map = AAAAAAAAAA... | |
[C] Child status: | |
VmPeak: 23708 kB | |
VmSize: 23708 kB | |
VmLck: 0 kB | |
VmPin: 0 kB | |
VmHWM: 88 kB | |
VmRSS: 88 kB | |
RssAnon: 88 kB | |
RssFile: 0 kB | |
RssShmem: 0 kB | |
VmData: 56 kB | |
VmStk: 140 kB | |
VmExe: 8 kB | |
VmLib: 1888 kB | |
VmPTE: 28 kB | |
VmPMD: 12 kB | |
VmSwap: 0 kB | |
[C] Child waiting for parent unmap... | |
[P] Parent, post munmap | |
VmPeak: 23708 kB | |
VmSize: 4176 kB | |
VmLck: 0 kB | |
VmPin: 0 kB | |
VmHWM: 20656 kB | |
VmRSS: 1164 kB | |
RssAnon: 88 kB | |
RssFile: 1076 kB | |
RssShmem: 0 kB | |
VmData: 56 kB | |
VmStk: 140 kB | |
VmExe: 8 kB | |
VmLib: 1888 kB | |
VmPTE: 24 kB | |
VmPMD: 12 kB | |
VmSwap: 0 kB | |
[C] Child (post parent munmap) sees map = AAAAAAAAAA... | |
[C] Child status: | |
VmPeak: 23708 kB | |
VmSize: 23708 kB | |
VmLck: 0 kB | |
VmPin: 0 kB | |
VmHWM: 88 kB | |
VmRSS: 88 kB | |
RssAnon: 88 kB | |
RssFile: 0 kB | |
RssShmem: 0 kB | |
VmData: 56 kB | |
VmStk: 140 kB | |
VmExe: 8 kB | |
VmLib: 1888 kB | |
VmPTE: 28 kB | |
VmPMD: 12 kB | |
VmSwap: 0 kB | |
[P] waited: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/mman.h> | |
#include <sys/wait.h> | |
#include <sys/sendfile.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#define MAP_SIZE 1000000 * 20 | |
// just bail hard if *anything* goes wrong | |
#define CHECK(x) ({ \ | |
int _res = (x); \ | |
if(_res == -1) { perror(#x); exit(1); } \ | |
_res; }) | |
void dumpfile(const char* path) { | |
int fd = CHECK(open(path, O_RDONLY)); | |
while(CHECK(sendfile(STDOUT_FILENO, fd, NULL, 4096)) > 0); | |
close(fd); | |
} | |
int main(int argc, char **argv) { | |
int parent = getpid(); | |
int child; | |
int childstatus; | |
int p[2]; | |
char buf[1]; | |
CHECK(pipe(p)); | |
setvbuf(stdout, NULL, _IONBF, 0); | |
// get some memory | |
void *map = mmap( | |
NULL, MAP_SIZE, PROT_READ | PROT_WRITE, | |
MAP_ANONYMOUS | MAP_SHARED, -1, 0); | |
if (map == NULL) { | |
perror("mmap"); | |
exit(1); | |
} | |
// touch it | |
memset(map, 0x41, MAP_SIZE); | |
child = CHECK(fork()); | |
if (child == 0) { | |
close(p[1]); | |
child = getpid(); | |
printf("[C] In child %d, parent %d\n", child, parent); | |
CHECK(read(p[0], buf, 1)); | |
printf("[C] Child sees map = %.10s...\n", map); | |
printf("[C] Child status:\n", map); | |
dumpfile("/proc/self/status"); | |
printf("[C] Child waiting for parent unmap...\n"); | |
CHECK(read(p[0], buf, 1)); | |
printf("[C] Child (post parent munmap) sees map = %.10s...\n", map); | |
printf("[C] Child status:\n", map); | |
dumpfile("/proc/self/status"); | |
exit(0); | |
} | |
close(p[0]); | |
printf("[P] Parent, pre munmap\n"); | |
dumpfile("/proc/self/status"); | |
write(p[1], "_", 1); | |
sleep(1); | |
munmap(map, MAP_SIZE); | |
printf("\n[P] Parent, post munmap\n"); | |
dumpfile("/proc/self/status"); | |
write(p[1], "_", 1); | |
waitpid(child, &childstatus, 0); | |
printf("[P] waited: %d\n", childstatus); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment