概述
映射空间的创建
通过对映射空间的写入从而修改外部的mytest.txt文件
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<string.h>
int main()
{
int fd,len,ret;
char* p=NULL;
fd=open("mytest.txt",O_CREAT| O_RDWR,0644 );
if(fd<0)
{
perror("open error");
exit(1);
}
len=ftruncate(fd,4); //文件截断为大小4
if(len==-1)
{
perror("ftruncate error:");
exit(1);
}
p=mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
//返回p是文件到内核区的映射首地址,默认为void*,此处为隐式转换为char×
//1.默认为NULL,内核自动分配映射空间和地址
//2.映射空间大小
//3.映射区权限,此处为读写
//4.标志位参数,常用来设定更新物理区域、设置共享、创建匿名映射区,
//此处MAP_SHARED会将映射区操作反映到物理设备上(磁盘)上
//5.文件描述符
//6.映射文件的偏移
if(p==MAP_FAILED)
{
perror("mmap error");
exit(1);
}
strcpy(p,"abc");
ret=munmap(p,4); //回收映射区域
if(ret==-1)
{
perror("munmap error:");
exit(1);
}
close(fd);
return 0;
}
父子进程间
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<sys/wait.h>
int val=100;
int main()
{
int* p;
pid_t pid;
int fd;
fd=open("temp",O_RDWR|O_CREAT|O_TRUNC,0644);
if(fd<0)
{
perror("open error:");
exit(1);
}
unlink("temp"); //删除临时文件目录项,使之具备被释放条件
//当所有占用该文件进程结束时,该文件释放
ftruncate(fd,4);//文件截断为4
// p=(int*)mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
p=(int*)mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
//父子进程间SHARED时共享映射区,都打印*p=2000;
//PRIVATED时,子*p=2000,父*p=0
if(p==MAP_FAILED)
{
perror("mmap error:");
exit(1);
}
close(fd);
pid=fork();
if(pid==0)
{
*p=2000;
val=1000;//父子进程读时共享,写时复制
printf("child----*p=%d,val=%dn",*p,val);
}
else if(pid>0)
{
sleep(1);//等待子进程修改
printf("parent----*p=%d,val=%dn",*p,val);
wait(NULL);
int ret=munmap(p,4);
if(ret==-1)
{
perror("munmap error");
exit(1);
}
}
else
{
perror("fork error");
exit(1);
}
return 0;
}
匿名建立映射区
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<sys/wait.h>
int val=100;
int main()
{
int* p;
pid_t pid;
/* int fd;
fd=open("/dev/zero",O_RDWR);//zero文件可任意指定大小,适合创建映射区
if(fd<0)
{
perror("open error:");
exit(1);
}*/
p=(int*)mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANON,-1,0);
//MAP_ANON创建匿名映射区,不需要open文件
//仅适用于linux系统,通用方式为上面注释的利用zero文件创建映射区
if(p==MAP_FAILED)
{
perror("mmap error:");
exit(1);
}
//close(fd);
pid=fork();
if(pid==0)
{
*p=2000;
val=1000;//父子进程读时共享,写时复制
printf("child----*p=%d,val=%dn",*p,val);
}
else if(pid>0)
{
sleep(1);//等待子进程修改
printf("parent----*p=%d,val=%dn",*p,val);
wait(NULL);
int ret=munmap(p,4);
if(ret==-1)
{
perror("munmap error");
exit(1);
}
}
else
{
perror("fork error");
exit(1);
}
return 0;
}
非血缘关系间进程通信
写数据进程:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>
#include<sys/mman.h>
struct STU{
int id;
char name[20];
char sex;
};
void sys_err(char *str)
{
perror(str);
exit(1);
}
int main(int argc,char *argv[])
{
int fd;
struct STU student ={
10,
"xiaoming",
'm',
};
char *mm;
if(argc<2)
{
printf("./a.out file_sharedn");
exit(1);
}
fd=open(argv[1],O_RDWR|O_CREAT,0664);
ftruncate(fd,sizeof(student));
mm=mmap(NULL,sizeof(student),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(mm==MAP_FAILED)
sys_err("mmap");
close(fd);
while(1)
{
memcpy(mm,&student,sizeof(student));
student.id++;
sleep(1);
}
munmap(mm,sizeof(student));
return 0;
}
读数据进程:
屏幕上循环打印student信息
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>
#include<sys/mman.h>
struct STU{
int id;
char name[20];
char sex;
};
void sys_err(char *str)
{
perror(str);
exit(-1);
}
int main(int argc,char *argv[])
{
int fd;
struct STU student;
struct STU *mm;
if(argc<2)
{
printf("./a.out file_sharedn");
exit(-1);
}
fd=open(argv[1],O_RDONLY);
if(fd==-1)
{
sys_err("open error");
}
mm=mmap(NULL,sizeof(student),PROT_READ,MAP_SHARED,fd,0);
if(mm==MAP_FAILED)
sys_err("mmap error");
close(fd);
while(1)
{
printf("id=%dtname=%stsex=%cn",mm->id,mm->name,mm->sex);
sleep(2);
}
munmap(mm,sizeof(student));
return 0;
}
最后
以上就是洁净夕阳为你收集整理的进程间通信 函数mmap用法 (linux系统编程)的全部内容,希望文章能够帮你解决进程间通信 函数mmap用法 (linux系统编程)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复