Mercurial > hg > Applications > Grep
comparison c/blocked_mmap/main.cc @ 30:edecf5b459c9
rename mmap to blocked_mmap
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 11 May 2014 19:32:39 +0900 |
parents | c/mmap/main.cc@b9d005c23aaa |
children | 4580f792d4c6 |
comparison
equal
deleted
inserted
replaced
29:b9d005c23aaa | 30:edecf5b459c9 |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include <unistd.h> | |
5 #include <fcntl.h> | |
6 #include <sys/stat.h> | |
7 #include <sys/types.h> | |
8 #include <sys/mman.h> | |
9 | |
10 | |
11 const char *usr_help_str = "Usage : ./mmap [-file filename]"; | |
12 | |
13 int main(int argc, char *argv[]){ | |
14 | |
15 struct stat sb; | |
16 | |
17 char *filename = 0; | |
18 for (int i = 1; argv[i]; ++i) { | |
19 if (strcmp(argv[i], "-file") == 0){ | |
20 filename = argv[i+1]; i++; | |
21 } | |
22 } | |
23 | |
24 if (filename == 0){ | |
25 puts(usr_help_str); | |
26 exit(1); | |
27 } | |
28 | |
29 int fd = -1; | |
30 if ((fd=open(filename,O_RDONLY,0666))==0){ | |
31 fprintf(stderr,"can't open %s\n",filename); | |
32 } | |
33 | |
34 if (fstat(fd,&sb)){ | |
35 fprintf(stderr,"can't open %s\n",filename); | |
36 } | |
37 | |
38 int file_size = sb.st_size; | |
39 int page_size = getpagesize(); | |
40 | |
41 int loop_num = file_size / page_size; | |
42 loop_num += (file_size % page_size != 0); | |
43 | |
44 char * file_mmap = NULL; | |
45 char * file_head = NULL; // head of read file | |
46 | |
47 for (int i = 0; i < loop_num; i++) { | |
48 // mmap 6th arg must be a multiple of the paging size. | |
49 file_mmap = (char *)mmap(0, page_size, PROT_READ, MAP_PRIVATE, fd, i*page_size); | |
50 if (i == 0) file_head = file_mmap; | |
51 file_mmap += page_size; | |
52 } | |
53 | |
54 printf("%s\n",file_head); | |
55 | |
56 munmap(file_mmap, file_size); | |
57 close(fd); | |
58 | |
59 return 0; | |
60 } |