定义

参数



注意事项


文件与内存映射
| |
| |
| #include <stdio.h> |
| #include <sys/mman.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| #include <fcntl.h> |
| #include <unistd.h> |
| #include <string.h> |
| |
| |
| int main(int argc, char *argv[]) { |
| int fd; |
| char *mapped_mem, *p; |
| int flength = 1024; |
| |
| |
| fd = open(argv[1], O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| flength = lseek(fd, 1, SEEK_END); |
| write(fd, "\0", 1); |
| lseek(fd, 0, SEEK_SET); |
| mapped_mem = (char *)mmap( NULL, |
| flength, |
| PROT_READ, |
| MAP_PRIVATE, |
| fd, |
| 0); |
| |
| printf("%s\n", mapped_mem); |
| close(fd); |
| munmap(mapped_mem, flength); |
| return 0; |
| } |
修改文件的内存映像
| #include <stdio.h> |
| #include <sys/mman.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| #include <fcntl.h> |
| #include <unistd.h> |
| #include <string.h> |
| |
| |
| int main(int argc, char *argv[]) { |
| int fd; |
| char *mapped_mem, *p; |
| int flength = 1024; |
| |
| fd = open(argv[1], O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| flength = lseek(fd, 1, SEEK_END); |
| write(fd, "\0", 1); |
| lseek(fd, 0, SEEK_SET); |
| mapped_mem = (char *)mmap( NULL, |
| flength, |
| PROT_READ | PROT_WRITE, |
| MAP_SHARED, |
| fd, |
| 0); |
| |
| printf("%s\n", mapped_mem); |
| |
| while ((p = strstr(mapped_mem, "ass"))) { |
| memcpy(p, "123", 3); |
| p += 3; |
| } |
| close(fd); |
| munmap(mapped_mem, flength); |
| return 0; |
| } |
| |

mmap用法示例(1)
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <ctype.h> |
| #include <fcntl.h> |
| #include <sys/mman.h> |
| #include <unistd.h> |
| #include <string.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| |
| #define MAX 1024 |
| |
| |
| int main(int argc, char *argv[]) |
| { |
| |
| int fd; |
| |
| |
| if (argc != 2) |
| { |
| printf("usage:./exe filename\n"); |
| exit(EXIT_FAILURE); |
| } |
| |
| |
| |
| |
| |
| mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| fd = open(argv[1], O_CREAT | O_RDWR, mode); |
| if (fd == -1) |
| { |
| perror("Error open func.\n"); |
| exit(EXIT_FAILURE); |
| } |
| |
| |
| ftruncate(fd, MAX); |
| |
| |
| struct stat st; |
| fstat(fd, &st); |
| printf("%s size is %d Byte.\n", argv[1], st.st_size); |
| |
| |
| unsigned char * mem_p = NULL; |
| mem_p = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| if (mem_p == NULL) |
| { |
| perror("Error mmap func.\n"); |
| close(fd); |
| exit(EXIT_FAILURE); |
| } |
| |
| |
| close(fd); |
| |
| |
| strcpy(mem_p, "mmap func test.\n"); |
| |
| |
| printf("mem_p = %s\n", mem_p); |
| |
| |
| for (int i = 0; i < st.st_size; i++) |
| { |
| mem_p[i] = toupper(mem_p[i]); |
| putchar(mem_p[i]); |
| } |
| |
| |
| if (munmap(mem_p, st.st_size) == -1) |
| { |
| perror("Error munmap func.\n"); |
| exit(EXIT_FAILURE); |
| } |
| exit(EXIT_SUCCESS); |
| } |
多线程并发拷贝数据
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <fcntl.h> |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #include <sys/mman.h> |
| #include <string.h> |
| #include <unistd.h> |
| #include <wait.h> |
| #include <pthread.h> |
| |
| #define FORK_NUM 4 |
| |
| |
| |
| |
| int main(int argc, char * argv[]) |
| { |
| |
| if (argc != 3) |
| { |
| printf("usage:./exe <a> <b>\n"); |
| exit(EXIT_FAILURE); |
| } |
| |
| |
| int src_fd = open(argv[1], O_RDWR); |
| if (src_fd == -1) |
| { |
| perror("Error open func.\n"); |
| exit(EXIT_FAILURE); |
| } |
| |
| |
| int new_fd = open(argv[2], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
| if (new_fd == -1) |
| { |
| perror("Error open func new.\n"); |
| exit(EXIT_FAILURE); |
| } |
| |
| |
| struct stat src_st; |
| fstat(src_fd, &src_st); |
| printf("src_fd size is:%d.\n", src_st.st_size); |
| ftruncate(new_fd, src_st.st_size); |
| |
| |
| unsigned char * src_p; |
| src_p = mmap(NULL, src_st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, src_fd, 0); |
| if (src_p == NULL) |
| { |
| close(src_fd); |
| perror("Error mmap func.\n"); |
| exit(EXIT_FAILURE); |
| } |
| |
| unsigned char * new_p; |
| new_p = mmap(NULL, src_st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, new_fd, 0); |
| if (new_p == NULL) |
| { |
| close(new_fd); |
| perror("Error mmap func.\n"); |
| exit(EXIT_FAILURE); |
| } |
| |
| |
| close(new_fd); |
| close(src_fd); |
| |
| |
| int cp_size = 0; |
| int main_size = 0; |
| cp_size = src_st.st_size / FORK_NUM; |
| main_size = src_st.st_size % FORK_NUM; |
| |
| |
| int i; |
| for (i = 0; i < FORK_NUM; i++) |
| { |
| if (fork() == 0) |
| break; |
| } |
| |
| |
| if (i < FORK_NUM) |
| { |
| memcpy(new_p + i * cp_size, src_p + i * cp_size, cp_size); |
| munmap(new_p, src_st.st_size); |
| munmap(src_p, src_st.st_size); |
| } |
| else |
| { |
| memcpy(new_p + i * cp_size, src_p + i * cp_size, main_size); |
| munmap(new_p, src_st.st_size); |
| munmap(src_p, src_st.st_size); |
| |
| pid_t pid; |
| while (1) |
| { |
| pid = wait(NULL); |
| if (pid == -1) |
| break; |
| printf("%d thread over.\n", pid); |
| } |
| printf("%d main thread over.\n", pid); |
| } |
| exit(EXIT_SUCCESS); |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)