-
Notifications
You must be signed in to change notification settings - Fork 5
/
mmap001.c
57 lines (45 loc) · 1.25 KB
/
mmap001.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
/*
* mmap001.c
*
* Tests mmapping a big file and writing it once
*
* (C) Juan Quintela <[email protected]>, 2000
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "misc_lib.h"
#define FILENAME "testing_file"
int main(int argc, char * argv[])
{
char *array;
unsigned int i;
int fd;
int ramsize = MEGAS(RAMSIZE);
if(argc == 2)
ramsize = MEGAS(atoi(argv[1]));
unlink(FILENAME);
fd = open(FILENAME, O_RDWR | O_CREAT, 0666);
if ((fd == -1))
error_exit(errno, "Problems opening files");
if (lseek(fd, ramsize, SEEK_SET) != ramsize)
error_exit(errno, "Problems doing the lseek");
if (write(fd,"\0",1) !=1)
error_exit(errno, "Problems writing");
array = mmap(NULL, ramsize, PROT_WRITE | PROT_READ | PROT_EXEC,
MAP_SHARED, fd, 0);;
if (array == MAP_FAILED)
error_exit(errno, "The mmap has failed");
for(i = 0; i < ramsize; i++) {
array[i] = i;
}
msync(array, ramsize, MS_SYNC);
close(fd);
unlink(FILENAME);
exit(EXIT_SUCCESS);
}