mmap函数和sysconf函数使用页偏移例子--linux函数
2014-03-20 08:56
417 查看
#include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { char *addr; int fd; struct stat sb; off_t offset, pa_offset; size_t length; ssize_t s; if (argc < 3 || argc > 4) { fprintf(stderr, "%s file offset [length]\n", argv[0]); exit(EXIT_FAILURE); } fd = open(argv[1], O_RDONLY); if (fd == -1) handle_error("open"); if (fstat(fd, &sb) == -1) /* To obtain file size */ handle_error("fstat"); offset = atoi(argv[2]); pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1); /* offset for mmap() must be page aligned */ if (offset >= sb.st_size) { fprintf(stderr, "offset is past end of file\n"); exit(EXIT_FAILURE); } if (argc == 4) { length = atoi(argv[3]); if (offset + length > sb.st_size) length = sb.st_size - offset; /* Can't display bytes past end of file */ } else { /* No length arg ==> display to end of file */ length = sb.st_size - offset; } addr = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_PRIVATE, fd, pa_offset); printf("addr is 0x%08x\n\n\n",&addr); if (addr == MAP_FAILED) handle_error("mmap"); s = write(STDOUT_FILENO, addr + offset - pa_offset, length); if (s != length) { if (s == -1) handle_error("write"); fprintf(stderr, "partial write"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } /* main */
相关文章推荐
- linux中使用pthread_kill函数测试线程是否存活的例子
- linux中使用pthread_kill函数测试线程是否存活的例子
- linux中使用pthread_kill函数测试线程是否存活的例子
- linux 系统调用sysconf函数使用
- linux 系统调用sysconf函数使用
- linux中使用pthread_kill函数测试线程是否存活的例子
- LINUX中使用PTHREAD_KILL函数测试线程是否存活的例子
- linux mmap使用例子_wallwind-ChinaUnix博客
- PHP图片处理之使用imagecopyresampled函数裁剪图片例子
- LINUX使用tcgetattr函数与tcsetattr函数控制终端二
- 在linux下使用math库中的函数
- Linux下C/C++编译链接过程及库函数使用说明
- Linux内核的ioctl函数学习2 - linux系统ioctl使用示例
- linux系统编程之进程(五):exec系列函数(execl,execlp,execle,execv,execvp)使用
- linux基础编程 共享内存 使用内存映射接口mmap系统调用 blog.csdn.net/ghostyu/article/details/7736733
- Linux 函数调用栈的使用
- Oracle 正则表达式函数-REGEXP_REPLACE 使用例子
- linux几种定时函数的使用
- [Linux文件属性]使用umask函数指定新建文件权限
- [Linux文件属性]使用fcntl函数获取文件标志