使用mmap可以方便地添加共享内存

本文提供了一个使用mmap实现的共享内存示例程序,展示了父子进程间如何通过共享内存进行数据交换。需要注意的是,mmap创建的共享内存仅限于具有亲属关系的进程之间使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用mmap添加的共享内存。

局限:

只能在有亲属关系的进程之间使用。

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

#define MEMSIZE 1024

int main() {
        char *str;
        pid_t pid;

        str = (char *)mmap(NULL, MEMSIZE,
                   PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS,
                   -1, 0);

        if (str == MAP_FAILED) {
                perror("mmap");
                exit(1);
        }

        pid = fork();
        if (pid < 0) {
                perror("fork");
                exit(1);
        }

        if (pid == 0) {
                strncpy(str, "Hello!", 6);
                munmap(str, MEMSIZE);
                printf("child exit\n");
                exit(0);
        }
        else {
                wait(NULL);
                puts(str);
                munmap(str, MEMSIZE);
                printf("father exit\n");
                exit(0);
        }

}

运行结果:

[work@ mmap1]$g++ -o mmap_sharedm mmap_sharedm.cpp
[work@ mmap1]$./mmap_sharedm 
child exit
Hello!
father exit

注意以上各个头文件的作用:

//#include <stdio.h>
mmap_sharedm.cpp:22: error: `perror' was not declared in this scope
mmap_sharedm.cpp:28: error: `perror' was not declared in this scope
mmap_sharedm.cpp:35: error: `printf' was not declared in this scope
mmap_sharedm.cpp:40: error: `puts' was not declared in this scope
mmap_sharedm.cpp:42: error: `printf' was not declared in this scope

//#include <stdlib.h>
mmap_sharedm.cpp:23: error: `exit' was not declared in this scope
mmap_sharedm.cpp:29: error: `exit' was not declared in this scope
mmap_sharedm.cpp:36: error: `exit' was not declared in this scope
mmap_sharedm.cpp:43: error: `exit' was not declared in this scope

//#include <unistd.h>
mmap_sharedm.cpp:26: error: `fork' was not declared in this scope

//#include <sys/mman.h>
mmap_sharedm.cpp:18: error: `PROT_READ' was not declared in this scope
mmap_sharedm.cpp:18: error: `PROT_WRITE' was not declared in this scope
mmap_sharedm.cpp:18: error: `MAP_SHARED' was not declared in this scope
mmap_sharedm.cpp:18: error: `MAP_ANONYMOUS' was not declared in this scope
mmap_sharedm.cpp:19: error: `mmap' was not declared in this scope
mmap_sharedm.cpp:21: error: `MAP_FAILED' was not declared in this scope
mmap_sharedm.cpp:34: error: `munmap' was not declared in this scope
mmap_sharedm.cpp:41: error: `munmap' was not declared in this scope

//#include <string.h>
mmap_sharedm.cpp:33: error: `strncpy' was not declared in this scope

//#include <wait.h>
mmap_sharedm.cpp:39: error: no matching function for call to `wait::wait(NULL)'
/usr/include/bits/waitstatus.h:68: note: candidates are: wait::wait()
/usr/include/bits/waitstatus.h:68: note:                 wait::wait(const wait&)

 

转载于:https://www.cnblogs.com/charlesblc/p/5427471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值