凡客建站网,宜昌网站建设开发费用,上海网站建设开发哪,怎样在手机上网站建设内存映射#xff1a;可以进行进程间的通信
1.如果对mmap的返回值(ptr)做操作(ptr), munmap是否能够成功? void * ptr mmap(…); ptr; 可以对其进行操作 munmap(ptr, len); // 错误,要保存地址
2.如果open时O_RDONLY, mmap时prot参数指定PROT_READ | PROT_WRITE会怎样? 错…内存映射可以进行进程间的通信
1.如果对mmap的返回值(ptr)做操作(ptr), munmap是否能够成功? void * ptr mmap(…); ptr; 可以对其进行操作 munmap(ptr, len); // 错误,要保存地址
2.如果open时O_RDONLY, mmap时prot参数指定PROT_READ | PROT_WRITE会怎样? 错误返回MAP_FAILED open()函数中的权限建议和prot参数的权限保持一致。
3.如果文件偏移量为1000会怎样? 偏移量必须是4K的整数倍返回MAP_FAILED
4.mmap什么情况下会调用失败? - 第二个参数length 0 - 第三个参数prot - 只指定了写权限 - prot PROT_READ | PROT_WRITE 第5个参数fd 通过open函数时指定的 O_RDONLY / O_WRONLY
5.可以open的时候O_CREAT一个新文件来创建映射区吗? - 可以的但是创建的文件的大小如果为0的话肯定不行 - 可以对新的文件进行扩展 - lseek() - truncate()
6.mmap后关闭文件描述符对mmap映射有没有影响 int fd open(“XXX”); mmap(,fd,0); // 0 是偏移量 close(fd); 映射区还存在创建映射区的fd被关闭没有任何影响。
7.对ptr越界操作会怎样 void * ptr mmap(NULL, 100,); //映射100个数据 4K 越界操作操作的是非法的内存 野地址- 段错误
使用内存映射实现文件拷贝 // 使用内存映射实现文件拷贝的功能
/*思路1.对原始的文件进行内存映射2.创建一个新文件拓展该文件3.把新文件的数据映射到内存中4.通过内存拷贝将第一个文件的内存数据拷贝到新的文件内存中5.释放资源
*/
#include stdio.h
#include sys/mman.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include unistd.h
#include string.h
#include stdlib.hint main() {// 1.对原始的文件进行内存映射int fd open(english.txt, O_RDWR);if(fd -1) {perror(open);exit(0);}// 获取原始文件的大小 表示从末尾开始偏移0 int len lseek(fd, 0, SEEK_END);// 2.创建一个新文件拓展该文件int fd1 open(cpy.txt, O_RDWR | O_CREAT, 0664);if(fd1 -1) {perror(open);exit(0);}// 对新创建的文件进行拓展truncate(cpy.txt, len);write(fd1, , 1);// 3.分别做内存映射void * ptr mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);void * ptr1 mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);if(ptr MAP_FAILED) {perror(mmap);exit(0);}if(ptr1 MAP_FAILED) {perror(mmap);exit(0);}// 内存拷贝memcpy(ptr1, ptr, len);// 释放资源munmap(ptr1, len);//谁后打开的先释放因为后打开的可能依赖先打开的munmap(ptr, len); close(fd1); //关闭文件描述符close(fd);return 0;
}匿名函数
/*匿名映射不需要文件实体进程一个内存映射 只能用在有关系的进程如父子进程
*/#include stdio.h
#include sys/mman.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include unistd.h
#include string.h
#include stdlib.h
#include sys/wait.hint main() {// 1.创建匿名内存映射区int len 4096;void * ptr mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); //MAP_SHARED对数据操作 会更改内容if(ptr MAP_FAILED) {perror(mmap);exit(0);}// 父子进程间通信pid_t pid fork(); //创建子进程if(pid 0) {// 父进程strcpy((char *) ptr, hello, world);wait(NULL); //wait 回收子进程的资源}else if(pid 0) {// 子进程sleep(1);printf(%s\n, (char *)ptr);}// 释放内存映射区int ret munmap(ptr, len);if(ret -1) {perror(munmap);exit(0);}return 0;
} 文章转载自: http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.jmbgl.cn.gov.cn.jmbgl.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.xq3nk42mvv.cn.gov.cn.xq3nk42mvv.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.rpth.cn.gov.cn.rpth.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.jydky.cn.gov.cn.jydky.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.cjsrg.cn.gov.cn.cjsrg.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.mgbcf.cn.gov.cn.mgbcf.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.snbry.cn.gov.cn.snbry.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn