comparison c/mmap/main.cc @ 29:b9d005c23aaa

inplement blocked mmap & remove e.txt
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Sun, 11 May 2014 19:02:56 +0900
parents 178cf2dfc45b
children
comparison
equal deleted inserted replaced
28:178cf2dfc45b 29:b9d005c23aaa
33 33
34 if (fstat(fd,&sb)){ 34 if (fstat(fd,&sb)){
35 fprintf(stderr,"can't open %s\n",filename); 35 fprintf(stderr,"can't open %s\n",filename);
36 } 36 }
37 37
38 int text_size = sb.st_size; 38 int file_size = sb.st_size;
39 char *text = (char *)mmap(0, text_size, PROT_READ, MAP_PRIVATE, fd, 0); 39 int page_size = getpagesize();
40 40
41 printf("%s\n",text); 41 int loop_num = file_size / page_size;
42 loop_num += (file_size % page_size != 0);
42 43
43 munmap(text, text_size); 44 char * file_mmap = NULL;
45 char * file_head = NULL;
46
47 for (int i = 0; i < loop_num; i++) {
48 file_mmap = (char *)mmap(0, page_size, PROT_READ, MAP_PRIVATE, fd, i*page_size);
49 if (i == 0) file_head = file_mmap;
50 file_mmap += page_size;
51 }
52
53 printf("%s\n",file_head);
54
55 munmap(file_mmap, file_size);
44 close(fd); 56 close(fd);
45 57
46 return 0; 58 return 0;
47 } 59 }