先给自己打个广告,本人的微信公众号正式上线了,搜索:张笑生的地盘,主要关注嵌入式软件开发,股票基金定投,足球等等,希望大家多多关注,有问题可以直接留言给我,一定尽心尽力回答大家的问题
一 what
在《linux进程间通信(四)----IPC篇----共享内存初识篇》文章中,我们知道了共享内存是什么,通过几个常用的函数shmget、ftok、shmat、shmdt、shmctl,了解了如何创建共享内存,但是创建好的共享内存之间,如何实现进程间通信呢?
共享内存对象是存在于内核中的,两个进程间如何访问这个共享内存对象呢?这篇文章介绍两种访问方式,分别是有亲缘关系的父子进程访问同一个共享内存,以及非亲缘关系之间访问同一个共享内存。
共享内存使用步骤
1. 创建/打开共享内存
2. 映射共享内存,即把指定的共享内存映射到进程的地址空间用于访问
3. 读写共享内存
4. 撤销共享内存映射
5. 删除共享内存对象
使用共享内存时的一些注意点或是限制条件
6. 共享内存的数量是有限制的,通过ipcs -l命令查看,当然如果我们具有管理员权限,可以通过 cat /proc/sys/kernel/shmmax来查看
7. 共享内存删除的时间点,shmctl添加删除标记,只有当所有进车都取消共享内存映射时(即所有进程调用shmdt之后),才会删除共享内存。
二 how
2.1 亲缘关系的进程访问同一个共享内存
要想让共享内存能够在亲缘关系的进程中访问,fork函数必须在shmget函数之后
#include "stdio.h"
#include "stdlib.h"
#include <unistd.h>
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "string.h"
void myfun(int signum)
{
return;
}
int main(int argc, char *argv[])
{
int shmid;
int key;
char *p;
int pid;
// shmget函数必须在fork函数之前,先创建一个共享内存
shmid = shmget(IPC_PRIVATE, 128, IPC_CREAT | 0777);
if (shmid < 0) {
printf("create shared memory fail\n");
return -1;
}
printf("create shared memory sucess, shmid = %d\n", shmid);
pid = fork();
if (pid > 0) { // parent process
signal(SIGUSR2, myfun);
p = (char *)shmat(shmid, NULL, 0);
if (p == NULL) {
printf("shmat fail\n");
return -1;
}
printf("parent process shmat sucess\n");
while (1) {
//write share memory
printf("parent process begin to write memory data\n");
fgets(p, 128, stdin);
kill(pid, SIGUSR1); // tell child process to read data
pause(); // wait child process read
}
}
if (pid == 0) { // child process to read
signal(SIGUSR1, myfun);
p = (char *)shmat(shmid, NULL, 0);
if (p == NULL) {
printf("shmat fail\n");
return -1;
}
printf("child process shmat sucess\n");
while (1) {
pause(); // wait parent process write
//start read share memory
printf("child process read share memory data:%s\n", p);
kill(getppid(), SIGUSR2);
}
}
//在用户空间删除共享内存的地址
shmdt(p);
//memcpy(p, "abcd", 4); //执行这个语句会出现segment fault
shmctl(shmid, IPC_RMID, NULL);
system("ipcs -m");
return 0;
}
2.2 非亲缘关系的进程访问同一个共享内存
进程server
#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 fail\n");
return -1;
}
printf("create key sucess\n");
shmid = shmget(key, 128, IPC_CREAT | 0777);
if (shmid < 0) {
printf("create shared memory fail\n");
return -1;
}
printf("create shared memory sucess, shmid = %d\n", shmid);
signal(SIGUSR2, myfun);
p = (struct mybuf *)shmat(shmid, NULL, 0);
if (p == NULL) {
printf("shmat fail\n");
return -1;
}
printf("parent process shmat sucess\n");
// 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 data\n");
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
#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 fail\n");
return -1;
}
printf("create key sucess\n");
shmid = shmget(key, 128, IPC_CREAT | 0777);
if (shmid < 0) {
printf("create shared memory fail\n");
return -1;
}
printf("create shared memory sucess, shmid = %d\n", shmid);
signal(SIGUSR1, myfun);
p = (struct mybuf *)shmat(shmid, NULL, 0);
if (p == NULL) {
printf("shmat fail\n");
return -1;
}
printf("client process shmat sucess\n");
// 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:%s\n", 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;
}