-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs-write.c
57 lines (48 loc) · 1.19 KB
/
fs-write.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#define FILESIZE (1024 * 1024 * 1024)
int main(int argc, char *argv[])
{
int fd, i;
int result;
char *map;
if (argc != 2) {
printf("Please specify filename\n");
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
if (fd == -1) {
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
result = lseek(fd, FILESIZE-1, SEEK_SET);
if (result == -1) {
close(fd);
perror("Error calling lseek() to 'stretch' the file");
exit(EXIT_FAILURE);
}
result = write(fd, "", 1);
if (result != 1) {
close(fd);
perror("Error writing last byte of the file");
exit(EXIT_FAILURE);
}
map = mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
/* trigger #PF */
for (i = 0; i < FILESIZE / (4096 * 512); i++)
map[4096 * 512 * i] = 'd';
if (munmap(map, FILESIZE) == -1)
perror("Error un-mmapping the file");
close(fd);
return 0;
}