-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathcow.c
74 lines (59 loc) · 1.38 KB
/
cow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#define BUFFER_SIZE (100 * 1024 * 1024)
#define PAGE_SIZE 4096
#define COMMAND_SIZE 4096
static char command[COMMAND_SIZE];
static void child_fn(char *p) {
printf("*** child ps info before memory access ***:\n");
fflush(stdout);
snprintf(command, COMMAND_SIZE,
"ps -o pid,comm,vsz,rss,min_flt,maj_flt | grep '^ *%d'",
getpid());
system(command);
printf("*** free memory info before memory access ***:\n");
fflush(stdout);
system("free");
int i;
for (i = 0; i < BUFFER_SIZE; i += PAGE_SIZE)
p[i] = 0;
printf("*** child ps info after memory access ***:\n");
fflush(stdout);
system(command);
printf("*** free memory info after memory access ***:\n");
fflush(stdout);
system("free");
exit(EXIT_SUCCESS);
}
static void parent_fn(void) {
wait(NULL);
exit(EXIT_SUCCESS);
}
int main(void)
{
char *p;
p = malloc(BUFFER_SIZE);
if (p == NULL)
err(EXIT_FAILURE, "malloc() failed");
int i;
for (i = 0; i < BUFFER_SIZE; i += PAGE_SIZE)
p[i] = 0;
printf("*** free memory info before fork ***:\n");
fflush(stdout);
system("free");
pid_t ret;
ret = fork();
if (ret == -1)
err(EXIT_FAILURE, "fork() failed");
if (ret == 0)
child_fn(p);
else
parent_fn();
err(EXIT_FAILURE, "shouldn't reach here");
}