进程间通信 函数mmap用法 (linux系统编程)

映射空间的创建

通过对映射空间的写入从而修改外部的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=%d\n",*p,val);
    }
    else if(pid>0)
    {
        sleep(1);//等待子进程修改
        printf("parent----*p=%d,val=%d\n",*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=%d\n",*p,val);
    }
    else if(pid>0)
    {
        sleep(1);//等待子进程修改
        printf("parent----*p=%d,val=%d\n",*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_shared\n");
        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_shared\n");
        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=%d\tname=%s\tsex=%c\n",mm->id,mm->name,mm->sex);
         sleep(2);
    }
    munmap(mm,sizeof(student));

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值