例1:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#define SIZE 1024
int main()//子进程写一句话,父进程读
{
int shmid ;
char *shmaddr ;
struct shmid_ds buf ;
int flag = 0 ;
int pid ;
shmid = shmget(IPC_PRIVATE, SIZE, IPC_CREAT|0600 ) ;//创建共享内存
if ( shmid < 0 )
{
perror("get shm ipc_id error") ;
return -1 ;
}
pid = fork() ;
if ( pid == 0 )//child
{
shmaddr = (char *)shmat( shmid, NULL, 0 ) ;//子进程attach,取得共享内存地址
//if ( (int)shmaddr == -1 )//这种用法会报编译错误cast from 'char*' to 'int' loses precision,不理解
if ( shmaddr == (char*)-1 )//返回-1出错的比较判断
{
perror("shmat addr error") ;
return -1 ;
}
strcpy( shmaddr, "Hi, I am child process!\n") ;//向共享内存写
shmdt( shmaddr ) ;//detach
return 0;
} else if ( pid > 0) {//parent
sleep(3 ) ;//等待3秒
flag = shmctl( shmid, IPC_STAT, &buf) ;//取得共享进程的状态信息
if ( flag == -1 )
{
perror("shmctl shm error") ;
return -1 ;
}
printf("shm_segsz =%d bytes\n", buf.shm_segsz ) ;//共享内存大小
printf("parent pid=%d, shm_cpid = %d \n", getpid(), buf.shm_cpid ) ;//创建该共享内存的进程
printf("chlid pid=%d, shm_lpid = %d \n",pid , buf.shm_lpid ) ;//最后操作该共享内存的进程
shmaddr = (char *) shmat(shmid, NULL, 0 ) ;
if ( shmaddr == (char*)-1 )
{
perror("shmat addr error") ;
return -1 ;
}
printf("%s", shmaddr) ;
shmdt( shmaddr ) ;
shmctl(shmid, IPC_RMID, NULL) ;//删除共享内存
}else{
perror("fork error") ;
shmctl(shmid, IPC_RMID, NULL) ;//出错也要删除
}
return 0 ;
}
执行结果
shm_segsz =1024 bytes
parent pid=6644, shm_cpid = 6644
chlid pid=6645, shm_lpid = 6645
Hi, I am child process!
例2:
shmwrite.cpp//写进程
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
typedef struct{
char name[8];
int age;
} people;
int main(int argc, char** argv)
{
int shm_id,i;
key_t key;
char temp[8];
people *p_map;
char pathname[30] ;
strcpy(pathname,"/tmp") ;
key = ftok(pathname,0x03);//生成一个key
if(key==-1)
{
perror("ftok error");
return -1;
}
printf("key=%d\n",key) ;
shm_id=shmget(key,4096,IPC_CREAT|IPC_EXCL|0600);//创建一个,如果已经存在则报错
if(shm_id==-1)
{
perror("shmget error");
return -1;
}
printf("shm_id=%d\n", shm_id) ;
p_map=(people*)shmat(shm_id,NULL,0);//指向共享内存
memset(temp, 0x00, sizeof(temp)) ;//temp初始化为0
strcpy(temp,"test") ;
temp[4]='0';;
for(i = 0;i<3;i++)
{
temp[4]+=1;
strncpy((p_map+i)->name,temp,5);//这里可以看出共享内存里要放的是一个people[]
(p_map+i)->age=0+i;
}
shmdt(p_map) ;
return 0 ;
}
shmread.cpp//读进程
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
typedef struct{
char name[8];
int age;
} people;
int main(int argc, char** argv)
{
int shm_id,i;
key_t key;
people *p_map;
char pathname[30] ;
strcpy(pathname,"/tmp") ;
key = ftok(pathname,0x03);
if(key == -1)
{
perror("ftok error");
return -1;
}
printf("key=%d\n", key) ;
shm_id = shmget(key,0, 0);
if(shm_id == -1)
{
perror("shmget error");
return -1;
}
printf("shm_id=%d\n", shm_id) ;
p_map = (people*)shmat(shm_id,NULL,0);
for(i = 0;i<3;i++)
{
printf( "name:%s\n",(*(p_map+i)).name );//访问people[]
printf( "age %d\n",(*(p_map+i)).age );
}
if(shmdt(p_map) == -1)
{
perror("detach error");
return -1;
}
return 0 ;
}
执行结果
<52 mdsp209 [mct] :/home/mdcc/mct/cpp/shm>./shmwrite.exe
key=50463426
shm_id=149455084
<53 mdsp209 [mct] :/home/mdcc/mct/cpp/shm>./shmread.exe
key=50463426
shm_id=149455084
name:test1
age 0
name:test2
age 1
name:test3
age 2
引用http://blog.youkuaiyun.com/ljianhui/article/details/10253345里面关于共享内存的总结
使用共享内存的优缺点
1、优点:我们可以看到使用共享内存进行进程间的通信真的是非常方便,而且函数的接口也简单,数据的共享还使进程间的数据不用传送,而是直接访问内存,也加快了程序的效率。同时,它也不像匿名管道那样要求通信的进程有一定的父子关系。
2、缺点:共享内存没有提供同步的机制,这使得我们在使用共享内存进行进程间通信时,往往要借助其他的手段如信号量来进行进程间的同步工作。