今天写一个小程序遇到来bus error,
感觉挺新鲜的, 于是google之,原来是这样子阿
如果没有
write(fd, " ", 1);
这两句话,下面的程序就会有bus error,
具体原因是因为, mmap。。。 这个。。 看man把
- SIGBUS Attempted access to a portion of the buffer that does not correspond to the file (for example, beyond the end of the file, including the case where another process has truncated the file).
就是“你过界了”
下面这个小程序没有使用信号量来进行同步, 用的最二的方式
让子进程sleep 10s, 这种处理方式不推荐用到生产环境中,权当例子讲解使用。
- #include <sys/mman.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define FILE_NAME "/root/test.test"
- void child_work()
- {
- //getchar();
- int fd = open(FILE_NAME, O_RDWR);
- if (fd == -1) {
- perror("open file failed");
- }
- char *addr = NULL;
- addr = (char *)mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- printf("%s", addr);
- }
- int main(void)
- {
- int res = -1;
- int fd = -1;
- if ( (fd = open(FILE_NAME, O_CREAT|O_RDWR)) == -1){
- perror("open file failed");
- return -1;
- }
- /*settle bus error*/
- lseek(fd, 100, SEEK_SET);
- write(fd, " ", 1);
- char * addr = NULL;
- addr = (char *)mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (addr == MAP_FAILED) {
- perror("mmap failed ");
- close(fd);
- return -1;
- }
- switch(fork()) {
- case -1:
- perror("fork error");
- return -1;
- break;
- case 0:
- /* child */
- sleep(10);
- child_work();
- return;
- default:
- break;
- }
- /* parent */
- sprintf(addr, "hello child!\n\0");
- wait(0);
- return 0;
- }
这里有个小程序写的更好
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #define FILEPATH "/tmp/mmapped.bin"
- #define NUMINTS (1000)
- #define FILESIZE (NUMINTS * sizeof(int))
- int main(int argc, char *argv[])
- {
- int i;
- int fd;
- int result;
- int *map; /* mmapped array of int's */
- /* Open a file for writing.
- * - Creating the file if it doesn't exist.
- * - Truncating it to 0 size if it already exists. (not really needed)
- *
- * Note: "O_WRONLY" mode is not sufficient when mmaping.
- */
- fd = open(FILEPATH, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
- if (fd == -1) {
- perror("Error opening file for writing");
- exit(EXIT_FAILURE);
- }
- /* Stretch the file size to the size of the (mmapped) array of ints
- */
- result = lseek(fd, FILESIZE-1, SEEK_SET);
- if (result == -1) {
- close(fd);
- perror("Error calling lseek() to 'stretch' the file");
- exit(EXIT_FAILURE);
- }
-
- /* Something needs to be written at the end of the file to
- * have the file actually have the new size.
- * Just writing an empty string at the current file position will do.
- *
- * Note:
- * - The current position in the file is at the end of the stretched
- * file due to the call to lseek().
- * - An empty string is actually a single '\0' character, so a zero-byte
- * will be written at the last byte of the file.
- */
- result = write(fd, "", 1);
- if (result != 1) {
- close(fd);
- perror("Error writing last byte of the file");
- exit(EXIT_FAILURE);
- }
- /* Now the file is ready to be mmapped.
- */
- map = mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (map == MAP_FAILED) {
- close(fd);
- perror("Error mmapping the file");
- exit(EXIT_FAILURE);
- }
-
- /* Now write int's to the file as if it were memory (an array of ints).
- */
- for (i = 1; i <=NUMINTS; ++i) {
- map[i] = 2 * i;
- }
- /* Don't forget to free the mmapped memory
- */
- if (munmap(map, FILESIZE) == -1) {
- perror("Error un-mmapping the file");
- /* Decide here whether to close(fd) and exit() or not. Depends... */
- }
- /* Un-mmaping doesn't close the file, so we still need to do that.
- */
- close(fd);
- return 0;
- }
reading
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #define FILEPATH "/tmp/mmapped.bin"
- #define NUMINTS (1000)
- #define FILESIZE (NUMINTS * sizeof(int))
- int main(int argc, char *argv[])
- {
- int i;
- int fd;
- int *map; /* mmapped array of int's */
- fd = open(FILEPATH, O_RDONLY);
- if (fd == -1) {
- perror("Error opening file for reading");
- exit(EXIT_FAILURE);
- }
- map = mmap(0, FILESIZE, PROT_READ, MAP_SHARED, fd, 0);
- if (map == MAP_FAILED) {
- close(fd);
- perror("Error mmapping the file");
- exit(EXIT_FAILURE);
- }
-
- /* Read the file int-by-int from the mmap
- */
- for (i = 1; i <=NUMINTS; ++i) {
- printf("%d: %d\n", i, map[i]);
- }
- if (munmap(map, FILESIZE) == -1) {
- perror("Error un-mmapping the file");
- }
- close(fd);
- return 0;
- }
阅读(3561) | 评论(0) | 转发(0) |