Mercurial > hg > Applications > Grep
view 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 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/mman.h> const char *usr_help_str = "Usage : ./mmap [-file filename]"; int main(int argc, char *argv[]){ struct stat sb; char *filename = 0; for (int i = 1; argv[i]; ++i) { if (strcmp(argv[i], "-file") == 0){ filename = argv[i+1]; i++; } } if (filename == 0){ puts(usr_help_str); exit(1); } int fd = -1; if ((fd=open(filename,O_RDONLY,0666))==0){ fprintf(stderr,"can't open %s\n",filename); } if (fstat(fd,&sb)){ fprintf(stderr,"can't open %s\n",filename); } int file_size = sb.st_size; int page_size = getpagesize(); int loop_num = file_size / page_size; loop_num += (file_size % page_size != 0); char * file_mmap = NULL; char * file_head = NULL; for (int i = 0; i < loop_num; i++) { file_mmap = (char *)mmap(0, page_size, PROT_READ, MAP_PRIVATE, fd, i*page_size); if (i == 0) file_head = file_mmap; file_mmap += page_size; } printf("%s\n",file_head); munmap(file_mmap, file_size); close(fd); return 0; }