- 論壇徽章:
- 0
|
今天恰巧遇到了,就把我的例子寫出來:- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <string.h>
- int main()
- {
- int fd;
- char *start;
- char buf[100];
- fd = open("testfile", O_RDWR);
- if(fd < 0)
- {
- printf("fd < 0\n");
- return 0;
- }
- start = mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- strcpy(buf, start);
- printf("buf = %s\n", buf);
- strcpy(start, "Buf Is Not Null!");
- munmap(start, 100);
- close(fd);
- return 0;
- }
復(fù)制代碼 如果如上的testfile為空的話就會(huì)出現(xiàn)(注意:這里的為空是換行符都不能有):
Bus error (core dumped)
測試環(huán)境:
Linux ubuntu 3.5.0-40-generic #62-Ubuntu SMP Thu Aug 22 00:57:36 UTC 2013 i686 i686 i686 GNU/Linux
|
|