15LinuxC进程间通信之mmap无血缘进程间通信

1 mmap无血缘进程间通信

1)写文件:

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>

typedef struct Student{
    int id;
    char name[64];
    int age;
}student;

void sys_err(cost char *str){
    perror(std);
    exit(1);
}

int main(void)
{
    struct student stu = {1, "xiaoming", 18};
    struct student *p = NULL;
    int fd;

    fd = open("test_map", O_RDWR | O_CREAT | O_TRUNC, 0664);
    if(fd == -1){
        sys_err("open failed.");
    }
    ftruncate(fd, sizeof(stu));

    p = mmap(NULL, sizeof(stu), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (p == MAP_FAILED) {
        sys_err("mmap error");
    }
    close(fd);

    while (1){
        memcpy(p, &stu, sizeof(stu));
        stu.id++;
    }

    munmap(p, sizeof(stu));

    return 0;
}

2)读文件:

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>

typedef struct Student{
    int id;
    char name[64];
    int age;
}student;

void sys_err(cost char *str){
    perror(std);
    exit(1);
}

int main(void)
{
    struct student stu;
    struct student *p = NULL;
    int fd;

    fd = open("test_map", O_RDONLY, 0664);
    if(fd == -1){
        sys_err("open failed.");
    }
    //ftruncate(fd, sizeof(stu));

    p = mmap(NULL, sizeof(stu), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (p == MAP_FAILED) {
        sys_err("mmap error");
    }
    close(fd);

    while (1){
        //memcpy(p, &stu, sizeof(stu));
        //stu.id++;
        printf("id=%d, name=&s, age=%d\n", p->id, p->name, p->age);
    }

    munmap(p, sizeof(stu));
    

    return 0;
}

结果如下,注意,由于我们开辟的映射区刚好是一个stu的大小,所以前面可能因为读得慢导致被覆盖掉几个student类型的数据。
在这里插入图片描述

2 多个读写mmap

首先:将上面的写程序的open,去掉O_TRUNC截断标志,保证多个进程写时文件可以追加。
1)多个写,一个读,可以看到,mmap是可以重复读取内容的,因为文件内容不变,而管道pipe,fifo只能读取一次,读走就没有了。
在这里插入图片描述

3 总结

  • 1)mmap的数据可以重复读取,而pipe,fifo只能读取一次。
  • 2)mmap能在无血缘的进程间通信根本原因是因为有共同的映射区。pipe,fifo因为有共同的管道。当然有血缘关系的mmap和匹配,fifo也可以这样解释。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值