Mercurial > hg > Applications > Grep
comparison c/mmap/main.cc @ 26:0f4ccdbaf57f
add mmap
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 11 May 2014 03:49:24 +0900 |
parents | c/read_lseek/main.cc@57b2c00b9dc6 |
children | 178cf2dfc45b |
comparison
equal
deleted
inserted
replaced
25:8c7e1b34582f | 26:0f4ccdbaf57f |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <unistd.h> | |
4 #include <fcntl.h> | |
5 #include <string.h> | |
6 #include <stdbool.h> | |
7 #include <sys/stat.h> | |
8 #include <sys/time.h> | |
9 #include <sys/types.h> | |
10 #include "read.h" | |
11 | |
12 static double st_time; | |
13 static double ed_time; | |
14 | |
15 /* | |
16 * -b : bulk read 一括read | |
17 * -d : divide read 分割read (default) | |
18 */ | |
19 const char *usr_help_str = "Usage : ./read_lseek [-file filename] [-b -d]"; | |
20 const int ONE_TASK_READ_SIZE = 4096 * 1; | |
21 bool bulk_flag = true; | |
22 bool divide_flag = false; | |
23 | |
24 void result_printf(int,char *); | |
25 void divide_read(int,char*,read_t); | |
26 void bulk_read(int,char*,read_t); | |
27 | |
28 static double | |
29 get_time(){ | |
30 struct timeval tv; | |
31 gettimeofday(&tv,NULL); | |
32 return tv.tv_sec + (double)tv.tv_usec*1e-6; | |
33 } | |
34 | |
35 int main(int argc, char *argv[]){ | |
36 | |
37 struct stat sb; | |
38 read_t r; | |
39 | |
40 char *filename = 0; | |
41 for (int i = 1; argv[i]; ++i) { | |
42 if (strcmp(argv[i], "-file") == 0){ | |
43 filename = argv[i+1]; | |
44 }else if(strcmp(argv[i], "-b") == 0){ | |
45 bulk_flag = true; | |
46 divide_flag = false; | |
47 }else if(strcmp(argv[i], "-d") == 0){ | |
48 bulk_flag = false; | |
49 divide_flag = true; | |
50 } | |
51 } | |
52 | |
53 if (filename == 0){ | |
54 puts(usr_help_str); | |
55 exit(1); | |
56 } | |
57 | |
58 st_time = get_time(); | |
59 | |
60 int fd = -1; | |
61 if ((fd=open(filename,O_RDONLY,0666))==0){ | |
62 fprintf(stderr,"can't open %s\n",filename); | |
63 } | |
64 | |
65 if (fstat(fd,&sb)){ | |
66 fprintf(stderr,"can't open %s\n",filename); | |
67 } | |
68 | |
69 r.text_size = sb.st_size; | |
70 char *text = (char *)malloc(sizeof(char)*r.text_size); | |
71 | |
72 r.one_task_read_size = ONE_TASK_READ_SIZE; | |
73 | |
74 r.task_num = r.text_size / r.one_task_read_size; | |
75 r.task_num += ((r.text_size % r.one_task_read_size) != 0); | |
76 | |
77 if(bulk_flag == true){ | |
78 bulk_read(fd,text,r); | |
79 }else if(divide_flag == true){ | |
80 divide_read(fd,text,r); | |
81 } | |
82 | |
83 free(text); | |
84 close(fd); | |
85 | |
86 ed_time = get_time(); | |
87 printf("Time: %0.6f\n",ed_time-st_time); | |
88 | |
89 return 0; | |
90 } | |
91 | |
92 /* lseekで読む場所を指定してあげないといけないと思ったが、 | |
93 * readで読み込むだけでも読み込む場所が変わってくれるらしい。 | |
94 */ | |
95 void divide_read(int _fd,char* _text,read_t _r){ | |
96 for(int i = 0; i < _r.task_num; i++){ | |
97 //lseek(_fd, i * _r.one_task_read_size,SEEK_SET); | |
98 read(_fd,_text,_r.one_task_read_size); | |
99 result_printf(i,_text); | |
100 } | |
101 puts("divide read"); | |
102 printf("text size :%lu\n",_r.text_size); | |
103 printf("one task size:%d\n",_r.one_task_read_size); | |
104 printf("task num :%d\n",_r.task_num); | |
105 } | |
106 | |
107 void bulk_read(int _fd,char* _text,read_t _r){ | |
108 int i = 0; | |
109 read(_fd,_text,_r.text_size); | |
110 result_printf(i,_text); | |
111 puts("bulk read"); | |
112 printf("text size :%lu\n",_r.text_size); | |
113 } | |
114 | |
115 void result_printf(int _i,char *_text){ | |
116 //printf("-------%d--------\n",_i + 1); | |
117 printf("[task No. %d]%s\n",_i,_text); | |
118 //printf("-----------------\n\n"); | |
119 } |