annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdio.h>
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include <stdlib.h>
28
178cf2dfc45b add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 26
diff changeset
3 #include <string.h>
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 #include <unistd.h>
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #include <fcntl.h>
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 #include <sys/stat.h>
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 #include <sys/types.h>
28
178cf2dfc45b add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 26
diff changeset
8 #include <sys/mman.h>
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
28
178cf2dfc45b add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 26
diff changeset
11 const char *usr_help_str = "Usage : ./mmap [-file filename]";
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 int main(int argc, char *argv[]){
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 struct stat sb;
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 char *filename = 0;
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 for (int i = 1; argv[i]; ++i) {
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 if (strcmp(argv[i], "-file") == 0){
28
178cf2dfc45b add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 26
diff changeset
20 filename = argv[i+1]; i++;
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 }
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 }
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 if (filename == 0){
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 puts(usr_help_str);
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 exit(1);
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 }
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 int fd = -1;
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 if ((fd=open(filename,O_RDONLY,0666))==0){
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 fprintf(stderr,"can't open %s\n",filename);
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 }
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 if (fstat(fd,&sb)){
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 fprintf(stderr,"can't open %s\n",filename);
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 }
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
29
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
38 int file_size = sb.st_size;
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
39 int page_size = getpagesize();
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
40
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
41 int loop_num = file_size / page_size;
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
42 loop_num += (file_size % page_size != 0);
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
43
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
44 char * file_mmap = NULL;
30
edecf5b459c9 rename mmap to blocked_mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
45 char * file_head = NULL; // head of read file
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
29
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
47 for (int i = 0; i < loop_num; i++) {
30
edecf5b459c9 rename mmap to blocked_mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
48 // mmap 6th arg must be a multiple of the paging size.
29
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
49 file_mmap = (char *)mmap(0, page_size, PROT_READ, MAP_PRIVATE, fd, i*page_size);
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
50 if (i == 0) file_head = file_mmap;
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
51 file_mmap += page_size;
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
52 }
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
29
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
54 printf("%s\n",file_head);
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
55
b9d005c23aaa inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
56 munmap(file_mmap, file_size);
3
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 close(fd);
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 return 0;
cd6ac4ce5c3c add some files
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }