linux C编程中中mmap函数总线错误的问题
使用gcc编译器#include<stdio.h>#include<unistd.h>#inlcude<fcntl.h>#include<sys/mman.h>intma...
使用gcc编译器
# include <stdio.h>
# include <unistd.h>
# inlcude <fcntl.h>
# include <sys/mman.h>
int main()
{
int fd = open("temp", O_RDWR | O_CREAT | O_EXCL, 0666);
if(-1 == fd)
{
fd = open("temp", O_RDWR);
if(-1 == fd)
perror("open error!");
}
printf("open success!\n");
int* p = mmap(NULL, 4, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
if((void*)-1 == p)
perror("mmap error!");
int i = 0;
for(i = 0; i < 10; i++)
{
*p = i;
printf("*p = %d\n", *p);
sleep(1);
}
munmap(p, 4);
close(fd);
return 0;
}
编译没有问题,运行输出
open success!
总线错误 (core dumped)
求解!!!! 展开
# include <stdio.h>
# include <unistd.h>
# inlcude <fcntl.h>
# include <sys/mman.h>
int main()
{
int fd = open("temp", O_RDWR | O_CREAT | O_EXCL, 0666);
if(-1 == fd)
{
fd = open("temp", O_RDWR);
if(-1 == fd)
perror("open error!");
}
printf("open success!\n");
int* p = mmap(NULL, 4, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
if((void*)-1 == p)
perror("mmap error!");
int i = 0;
for(i = 0; i < 10; i++)
{
*p = i;
printf("*p = %d\n", *p);
sleep(1);
}
munmap(p, 4);
close(fd);
return 0;
}
编译没有问题,运行输出
open success!
总线错误 (core dumped)
求解!!!! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询