如何在Linux系统上使用mmap()进行读写
我需要在
Linux中使用mmap()创建一些流入和派出类.为此,我尝试制作一些测试代码,将一些整数写入文件,保存,再次加载并将文件中的数据写入cout.如果该测试代码有效,那么之后将流输入和输出不会成为问题.
当我刚开始时,我遇到了段错误,如果我没有得到任何结果,那么我用谷歌搜索了一下.我发现这本书http://www.advancedlinuxprogramming.com/alp-folder/alp-ch05-ipc.pdf在第107页左右有一些有用的代码.我复制粘贴该代码并做了一些小改动并得到了这段代码: int fd; void* file_memory; /* Prepare a file large enough to hold an unsigned integer. */ fd = open ("mapTester",O_RDWR | O_CREAT,S_IRUSR | S_IWUSR); //Make the file big enough lseek (fd,4 * 10 + 1,SEEK_SET); write (fd,"",1); lseek (fd,SEEK_SET); /* Create the memory mapping. */ file_memory = mmap (0,4 * 10,PROT_WRITE,MAP_SHARED,fd,0); close (fd); /* Write a random integer to memory-mapped area. */ sprintf((char*) file_memory,"%dn",22); /* Release the memory (unnecessary because the program exits). */ munmap (file_memory,4 * 10); cout << "Mark" << endl; //Start the part where I read from the file int integer; /* Open the file. */ fd = open (argv[1],O_RDWR,S_IRUSR | S_IWUSR); /* Create the memory mapping. */ file_memory = mmap (0,PROT_READ | PROT_WRITE,0); close (fd); /* Read the integer,print it out,and double it. */ scanf ((char *) file_memory,"%d",&integer); printf ("value: %dn",integer); sprintf ((char*) file_memory,2 * integer); /* Release the memory (unnecessary because the program exits). */ munmap (file_memory,4 * 10); 但是在“标记”cout之后,我得到了一个细分市场. 然后我用这个替换“读取部分”: fd = open("mapTester",O_RDONLY); int* buffer = (int*) malloc (4*10); read(fd,buffer,4 * 10); for(int i = 0; i < 1; i++) { cout << buffer[i] << endl; } 这是一些工作代码,告诉我文件是空的.我尝试了几种方法来写入映射而不会对结果进行任何更改. 那我怎么能让我的代码写出来呢? 我找到了一些其他资源但对我没有帮助,但由于我是新用户,我可能只发布最多2个链接. 解决方法
您应该测试
mmap的结果.如果它给出MAP_FAILED,请查看
errno以找出原因.
并且你最好mmap多个页面,通常每个4K字节,并由sysconf(_SC_PAGESIZE)给出 您可以使用stat查找某些给定文件的大小(以及许多其他数字). 您可以在现有Linux程序上使用strace来了解他们正在做什么系统调用. 关于/ proc /等,另见this (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |