linux 进程间的通讯之共享内存 读取篇

时间:2021-04-08 15:23:18   收藏:0   阅读:19

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>


int main()
{
int shmId;
char* shmaddr;
key_t key;

key=ftok(".",1);

// int shmget(key_t key, size_t size, int shmflg);
shmId=shmget(key,1024*4,0);//找到内存空间

if(shmId==-1){
printf("memo get fail\n");
}

// void *shmat(int shmid, const void *shmaddr, int shmflg);
shmaddr=shmat(shmId,0,0);//内存的映射

printf("data:%s\n",shmaddr);//输出内存的内容

shmdt(shmaddr);//关闭映射

printf("quit\n");


return 0;
}

原文:https://www.cnblogs.com/1402192381hqf/p/14631550.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!