Mercurial > hg > Applications > Grep
view 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 |
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; // head of read file for (int i = 0; i < loop_num; i++) { // mmap 6th arg must be a multiple of the paging size. 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; }