Mercurial > hg > Applications > Grep
view c/blocked_mmap/main.cc @ 62:a49b4a8b8c14
implement isLiteral
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Jul 2015 16:45:07 +0900 |
parents | 4580f792d4c6 |
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 = (char *)malloc(4096); 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. printf("pre file_mmap addr : %p\n",file_mmap); mmap(file_mmap + i*page_size, page_size, PROT_READ|PROT_WRITE, MAP_FIXED, fd, i*page_size); printf("after file_mmap addr : %p\n",file_mmap); } printf("%s\n",file_mmap); munmap(file_mmap, file_size); close(fd); return 0; }