大佬教程收集整理的这篇文章主要介绍了如何在Linux系统上使用mmap()进行读写,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我刚开始时,我遇到了段错误,如果我没有得到任何结果,那么我用谷歌@L_197_7@了一下.我发现这本书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,"%d\n",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: %d\n",Integer); sprintf ((char*) file_memory,2 * Integer); /* Release the memory (unnecessary because the program exits). */ munmap (file_memory,4 * 10);
然后我用这个替换“读取部分”:
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; }
这是一些工作代码,告诉我文件是空的.我尝试了几种方法来写入映射而不会对结果进行任何更改.
以上是大佬教程为你收集整理的如何在Linux系统上使用mmap()进行读写全部内容,希望文章能够帮你解决如何在Linux系统上使用mmap()进行读写所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。