先给自己打个广告,本人的微信公众号正式上线了,搜索:张笑生的地盘,主要关注嵌入式软件开发,足球等等,希望大家多多关注,有问题可以直接留言给我,一定尽心尽力回答大家的问题
![17c3b9aaaed13adab1887d585f53832e.png](https://i-blog.csdnimg.cn/blog_migrate/dc1a60d73f45d867784287d49cde79ca.jpeg)
一 what
在《linux进程间通信----IPC篇(一)----共享内存初识篇》文章中,我们知道了共享内存是什么,通过几个常用的函数shmget、ftok、shmat、shmdt、shmctl,了解了如何创建共享内存,但是创建好的共享内存之间,如何实现进程间通信呢?
共享内存对象是存在于内核中的,两个进程间如何访问这个共享内存对象呢?这篇文章介绍两种访问方式,分别是有亲缘关系的父子进程访问同一个共享内存,以及非亲缘关系之间访问同一个共享内存。
二 how
2.1 亲缘关系的进程访问同一个共享内存
要想让共享内存能够在亲缘关系的进程中访问,fork函数必须在shmget函数之后;鉴于我们前面好几篇文章都提到了fork函数,后期打算出一篇文章谈谈fork函数创建子进程的过程中究竟做了什么。
#include
2.2 非亲缘关系的进程访问同一个共享内存
进程server.c
#include "stdio.h"
#include "stdlib.h"
#include <unistd.h>
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "string.h"
struct mybuf
{
int pid;
char buf[124];
};
void myfun(int signum)
{
return;
}
int main(int argc, char *argv[])
{
int shmid;
int key;
struct mybuf *p;
int pid;
key = ftok("./a.c", 'a');
if (key , 0) {
printf("create key failn");
return -1;
}
printf("create key sucessn");
shmid = shmget(key, 128, IPC_CREAT | 0777);
if (shmid < 0) {
printf("create shared memory failn");
return -1;
}
printf("create shared memory sucess, shmid = %dn", shmid);
signal(SIGUSR2, myfun);
p = (struct mybuf *)shmat(shmid, NULL, 0);
if (p == NULL) {
printf("shmat failn");
return -1;
}
printf("parent process shmat sucessn");
// get client pid
p->pid = getpid(); //write server pid to share memory
pause(); // wait client to read server pid
pid=p->pid;
while (1) {
//write share memory
printf("parent process begin to write memory datan");
fgets(p->buf, 124, stdin);
kill(pid, SIGUSR1); // tell client process to read data
pause(); // wait client process read
}
//在用户空间删除共享内存的地址
shmdt(p);
//memcpy(p, "abcd", 4); //执行这个语句会出现segment fault
shmctl(shmid, IPC_RMID, NULL);
system("ipcs -m");
return 0;
}
进程client.c
#include "stdio.h"
#include "stdlib.h"
#include <unistd.h>
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "string.h"
struct mybuf
{
int pid;
char buf[124];
};
void myfun(int signum)
{
return;
}
int main(int argc, char *argv[])
{
int shmid;
int key;
struct mybuf *p;
int pid;
key = ftok("./a.c", 'a');
if (key , 0) {
printf("create key failn");
return -1;
}
printf("create key sucessn");
shmid = shmget(key, 128, IPC_CREAT | 0777);
if (shmid < 0) {
printf("create shared memory failn");
return -1;
}
printf("create shared memory sucess, shmid = %dn", shmid);
signal(SIGUSR1, myfun);
p = (struct mybuf *)shmat(shmid, NULL, 0);
if (p == NULL) {
printf("shmat failn");
return -1;
}
printf("client process shmat sucessn");
// get server pid
//read share memory
pid = p->pid;
// write client pid to share memory
p->pid = getpid();
kill(pid, SIGUSR2); // tell client process to read data
//client start to read share memory
while (1) {
pause(); // wait server process write share memory
printf("client process read data:%sn", p->buf); // read data
kill(pid, SIGUSR2); // server can write share memory
}
//在用户空间删除共享内存的地址
shmdt(p);
//memcpy(p, "abcd", 4); //执行这个语句会出现segment fault
shmctl(shmid, IPC_RMID, NULL);
system("ipcs -m");
return 0;
}