一、共享内存
共享内存为多个进程之间共享和传递数据提供了一种有效的方式。共享内存是先在物理内存上申请一块空间,多个进程可以将其映射到自己的虚拟地址空间中。所有进程都可以访问共享内存中的地址,就好像它们是由 malloc 分配的一样。如果某个进程向共享内存写入了数据,所做的改动将立刻被可以访问同一段共享内存的任何其他进程看到。由于它并未提供同步机制,所以我们通常需要用其他的机制来同步对共享内存的访问。
二、相关函数用法介绍
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
int shmget(key_t key, size_t size, int shmflg);
/*
shmget()用于创建或者获取共享内存
shmget()成功返回共享内存的 ID, 失败返回-1
key: 不同的进程使用相同的 key 值可以获取到同一个共享内存
size: 创建共享内存时,指定要申请的共享内存空间大小
shmflg: IPC_CREAT IPC_EXCL
*/
void* shmat(int shmid, const void *shmaddr, int shmflg);
/*
shmat()将申请的共享内存的物理内存映射到当前进程的虚拟地址空间上
shmat()成功返回返回共享内存的首地址,失败返回 NULL
shmaddr:一般给 NULL,由系统自动选择映射的虚拟地址空间
shmflg: 一般给 0, 可以给 SHM_RDONLY 为只读模式,其他的为读写
*/
int shmdt(const void *shmaddr);
/*
shmdt()断开当前进程的 shmaddr 指向的共享内存映射
shmdt()成功返回 0, 失败返回-1
*/
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
/*
shmctl()控制共享内存
shmctl()成功返回 0,失败返回-1
cmd: IPC_RMID
*/
三、共享内存的使用
3.1 示例代码 1
进程 a 向共享内存中写入数据,进程 b 从共享内存中读取数据并显示
shma.c 的代码:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
int main()
{
int shmid = shmget((key_t)1234, 128, IPC_CREAT | 0600);
assert( shmid != -1 );
char *s = (char *)shmat(shmid, NULL, 0);
assert( s != NULL );
strcpy(s, "hello");
shmdt(s);
exit(0);
}
shmb.c 的代码:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
int main()
{
int shmid = shmget((key_t)1234, 128, IPC_CREAT | 0600);
assert( shmid != -1 );
char *s = (char *)shmat(shmid, NULL, 0);
assert( s != NULL );
printf("shmb s = %s\n", s);
shmdt(s);
shmctl(shmid, IPC_RMID, NULL);
exit(0);
}
运行结果:
3.2 示例代码 2
进程 a 从键盘循环获取数据并拷贝到共享内存中,进程 b 从共享内存中获
取并打印数据。要求进程 a 输入一次,进程 b 输出一次,进程 a 不输入,进程 b 也不输出。
sem.h 的代码
#ifndef __SEM_H
#define __SEM_H
#define SEM_W 0
#define SEM_R 1
typedef union SemUn
{
int val;
}SemUn;
int SemGet(int key, int semVal[], int nsems);
int SemP(int semid, int index);
int SemV(int semid, int index);
int SemDel(int semid);
#endif
sem.c 的代码
#include "sem.h"
#include <sys/sem.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>
int SemGet(int key, int semVal[], int nsems)
{
int semid = semget((key_t)key, nsems, 0664);
if(semid == -1)
{
semid = semget((key_t)key, nsems, 0664|IPC_CREAT);
if(semid == -1)
{
return -1;
}
int i = 0;
for(; i < nsems; ++i)
{
SemUn un;
un.val = semVal[i];
if(-1 == semctl(semid, i, SETVAL, un))
{
perror("semctl error");
return -1;
}
}
}
return semid;
}
int SemP(int semid, int index)
{
struct sembuf buf;
buf.sem_num = index;
buf.sem_op = -1;
buf.sem_flg = SEM_UNDO;
if(-1 == semop(semid, &buf, 1))
{
return -1;
}
return 0;
}
int SemV(int semid, int index)
{
struct sembuf buf;
buf.sem_num = index;
buf.sem_op = 1;
buf.sem_flg = SEM_UNDO;
if(-1 == semop(semid, &buf, 1))
{
perror("semop error");
return -1;
}
return 0;
}
int SemDel(int semid)
{
if(-1 == semctl(semid, 0, IPC_RMID))
{
perror("semctl del error");
return -1;
}
return 0;
}
shmA.c 的代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include <sys/shm.h>
#include "sem.h"
int main()
{
int shmid = shmget((key_t) 1234, 128, IPC_CREAT | 0664);
assert(shmid != -1);
int semVal[2] ={1, 0};
int semid = SemGet(1000, semVal, 2);
char *ptr = (char*)shmat(shmid, NULL, 0);
assert(ptr != (char*)-1);
while(1)
{
char buff[128] = {0};
printf("please input: ");
fgets(buff, 128, stdin);
SemP(semid, SEM_W);
strcpy(ptr, buff);
SemV(semid, SEM_R);
if(strncmp(ptr, "end", 3) == 0)
{
break;
}
}
shmdt(ptr);
shmctl(shmid, IPC_RMID, NULL);
exit(0);
}
shmB.c 的代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include <sys/shm.h>
#include "sem.h"
int main()
{
int shmid = shmget((key_t) 1234, 128, IPC_CREAT | 0664);
assert(-1 != shmid);
int semVal[2] = {1, 0};
int semid = SemGet(1000, semVal, 2);
char *ptr = (char *)shmat(shmid, NULL, 0);
assert((char*)-1 != ptr);
while(1)
{
SemP(semid, SEM_R);
if(strncmp(ptr, "end", 3) == 0)
{
break;
}
printf("lenth = %d : %s", strlen(ptr) - 1, ptr);
SemV(semid, SEM_W);
}
shmdt(ptr);
shmctl(shmid, IPC_RMID, NULL);
SemDel(semid);
exit(0);
}
编译过程:
运行结果: