- 论坛徽章:
- 0
|
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/mman.h>
- int main()
- {
- int fd;
- if ( (fd = open("./file", O_RDWR|O_CREAT, S_IRWXU)) < 0){
- printf("open file wrong!");
- exit(1);
- }
-
- struct stat file_stat;
- if ( fstat( fd, &file_stat) < 0 )
- {
- printf(" fstat wrong");
- exit(1);
- }
-
- void *start_fp;
- if( ( start_fp = mmap(NULL, file_stat.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 )) == MAP_FAILED)
- {
- printf("mmap wrong");
- exit(0);
- }
- snprintf( (char *)start_fp, 4, "test"); //不知道这里为何段错误,有时候还总线错误
- msync( start_fp, file_stat.st_size, MS_ASYNC);
- if ( munmap( start_fp, file_stat.st_size ) < 0 )
- {
- printf("munmap wrong");
- exit(1);
- }
- }
复制代码 |
|