与MMAP给总线错误只在一个特定的机器的shm_open [英] shm_open with mmap giving bus error only in one particular machine

查看:518
本文介绍了与MMAP给总线错误只在一个特定的机器的shm_open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的程序来创建共享内存。但是,这是给


  

总线错误(核心转储)


这是发生只在一个虚拟机,我想同样的code在多发VM,并在此工作正常每隔机。但是,只有一台机器上这个问题发生。任何人都可以指出我的问题。我所有的机器都在 2.6.32-279.el6.x86_64 运行。这将是一个内核问题或应用问题?

 #包括LT&;&stdio.h中GT;
#包括LT&; SYS / mman.h>
#包括LT&; SYS / stat.h>
#包括LT&;&fcntl.h GT;
#包括LT&;&stdint.h GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
INT主(INT ARGC,CHAR *的argv [])
{
        为const char * shpath =/ shm_path_for_data;
        无效* memPtr;
        shm_unlink(shpath);
        INT FD = 0;
        FD =的shm_open(shpath,O_RDWR | O_CREAT | O_EXCL,0777);
        如果(FD℃,){
                的printf(失败的shm_open \\ n);
                返回1;
        }
        如果((ftruncate(FD,getpagesize的()))≤; 0){
                的printf(Ftruncate失败\\ n);
                返回1;
        }        memPtr = MMAP(NULL,getpagesize的(),PROT_READ | PROT_WRITE,MAP_SHARED,FD,0);
        如果(memPtr == MAP_FAILED){
                返回1;
        }
        的strcpy((字符*)memPtr,测试input.Just复制的东西\\ n);
        的printf(映射出:%S \\ n,(字符*)memPtr);
}


解决方案

您正在复制50个字节

 的memcpy((字符*)memPtr,测试input.Just复制的东西\\ N,50);
/ * BTW:^这个转换是不必要的* /

有只有36个,所以你正在阅读超越字符串,这是不确定的行为,这就是为什么它工作在一台机器上,并未能在另一个,这是行为如何不确定的行为。

尝试

 的strcpy((字符*)memPtr,测试输入刚才复制的东西\\ n);

I have the following simple program to create a shared memory. But this is giving

Bus error (core dumped)

This is happening only on one virtual machine and I tried the same code in mutiple VM and in every other machine this is working correctly. But only on one machine this issue is happening. Can anyone point me the issue. All my machines are running on 2.6.32-279.el6.x86_64 . Will this be a kernel issue or application issue?

#include<stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
        const char *shpath="/shm_path_for_data";
        void *memPtr;
        shm_unlink(shpath);
        int fd=0;
        fd = shm_open(shpath, O_RDWR|O_CREAT|O_EXCL, 0777);
        if(fd<0) {
                printf("shm_open failed\n");
                return 1;
        }
        if((ftruncate(fd, getpagesize())) <0) {
                printf("Ftruncate failed\n");
                return 1;
        }

        memPtr = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
        if(memPtr == MAP_FAILED) {
                return 1;
        }
        strcpy((char *)memPtr, "test input.Just copying something\n");
        printf("mapped out: %s\n", (char *)memPtr);
}

解决方案

You are copying 50 bytes

memcpy((char *)memPtr, "test input.Just copying something\n", 50);
/* BTW: ^ this cast is unneeded */

there are only 36 available, so you are reading beyond the string literal, which is undefined behavior, that's why it works on one machine and fails on another, that's how undefined behavior behaves.

Try

strcpy((char *) memPtr, "Test input. Just copying something\n");

这篇关于与MMAP给总线错误只在一个特定的机器的shm_open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆