- 论坛徽章:
- 0
|
回复 #1 onmeiei 的帖子
我测试的没问题啊
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- int main()
- {
- int fdin=open("data.in",O_RDWR|O_CREAT);
- int size = 1025;
- lseek(fdin, 1024, SEEK_SET);
- write(fdin, "", 1);
- char *src=NULL;
- if((src=mmap(NULL,size,PROT_WRITE,MAP_SHARED,fdin,0))==MAP_FAILED)
- {
- printf("src map error\n");
- return -1;
- }
- memset(src, '\0', 1025);
- const char *buf = "hello";
- memcpy(src, buf, strlen(buf));
- msync(src, strlen(src) + 1, MS_SYNC);
- }
复制代码
od -h data.in的结果:
0000000 6568 6c6c 006f 0000 0000 0000 0000 0000
0000020 0000 0000 0000 0000 0000 0000 0000 0000
*
0002000 0000
0002001 |
|