内存共享例子.docx
- 文档编号:17637410
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:8
- 大小:15.15KB
内存共享例子.docx
《内存共享例子.docx》由会员分享,可在线阅读,更多相关《内存共享例子.docx(8页珍藏版)》请在冰点文库上搜索。
内存共享例子
5. 父子进程通信范例
父子进程通信范例,shm.c源代码如下:
#include
#include
#include
#include
#include
#include
#defineSIZE1024
intmain()
{
intshmid;
char*shmaddr;
structshmid_dsbuf;
intflag=0;
intpid;
shmid=shmget(IPC_PRIVATE,SIZE,IPC_CREAT|0600);
if(shmid<0)
{
perror("getshm ipc_iderror");
return-1;
}
pid=fork();
if(pid==0)
{
shmaddr=(char*)shmat(shmid,NULL,0);
if((int)shmaddr==-1)
{
perror("shmataddrerror");
return-1;
}
strcpy(shmaddr,"Hi,Iamchildprocess!
\n");
shmdt(shmaddr);
return 0;
}elseif(pid>0){
sleep(3);
flag=shmctl(shmid,IPC_STAT,&buf);
if(flag==-1)
{
perror("shmctlshmerror");
return-1;
}
printf("shm_segsz=%dbytes\n",buf.shm_segsz);
printf("parentpid=%d,shm_cpid=%d\n",getpid(),buf.shm_cpid);
printf("chlidpid=%d,shm_lpid=%d\n",pid,buf.shm_lpid);
shmaddr=(char*)shmat(shmid,NULL,0);
if((int)shmaddr==-1)
{
perror("shmataddrerror");
return-1;
}
printf("%s",shmaddr);
shmdt(shmaddr);
shmctl(shmid,IPC_RMID,NULL);
}else{
perror("forkerror");
shmctl(shmid,IPC_RMID,NULL);
}
return0;
}
编译gccshm.c–oshm。
执行./shm,执行结果如下:
shm_segsz=1024bytes
shm_cpid=9503
shm_lpid=9504
Hi,Iamchildprocess!
6. 多进程读写范例
多进程读写即一个进程写共享内存,一个或多个进程读共享内存。
下面的例子实现的是一个进程写共享内存,一个进程读共享内存。
(1)下面程序实现了创建共享内存,并写入消息。
shmwrite.c源代码如下:
#include
#include
#include
#include
#include
#include
typedefstruct{
charname[8];
intage;
}people;
intmain(intargc,char**argv)
{
intshm_id,i;
key_tkey;
chartemp[8];
people*p_map;
charpathname[30];
strcpy(pathname,"/tmp");
key=ftok(pathname,0x03);
if(key==-1)
{
perror("ftokerror");
return-1;
}
printf("key=%d\n",key);
shm_id=shmget(key,4096,IPC_CREAT|IPC_EXCL|0600);
if(shm_id==-1)
{
perror("shmgeterror");
return-1;
}
printf("shm_id=%d\n",shm_id);
p_map=(people*)shmat(shm_id,NULL,0);
memset(temp,0x00,sizeof(temp));
strcpy(temp,"test");
temp[4]='0';
for(i=0;i<3;i++)
{
temp[4]+=1;
strncpy((p_map+i)->name,temp,5);
(p_map+i)->age=0+i;
}
shmdt(p_map);
return0;
}
(2)下面程序实现从共享内存读消息。
shmread.c源代码如下:
#include
#include
#include
#include
#include
#include
typedefstruct{
charname[8];
intage;
}people;
intmain(intargc,char**argv)
{
intshm_id,i;
key_tkey;
people*p_map;
charpathname[30];
strcpy(pathname,"/tmp");
key=ftok(pathname,0x03);
if(key==-1)
{
perror("ftokerror");
return-1;
}
printf("key=%d\n",key);
shm_id=shmget(key,0,0);
if(shm_id==-1)
{
perror("shmgeterror");
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);
printf("age%d\n",(*(p_map+i)).age);
}
if(shmdt(p_map)==-1)
{
perror("detacherror");
return-1;
}
return0;
}
(3)编译与执行
① 编译gccshmwrite.c-o shmwrite。
② 执行./shmwrite,执行结果如下:
key=50453281
shm_id=688137
③ 编译gccshmread.c-oshmread。
④ 执行./shmread,执行结果如下:
key=50453281
shm_id=688137
name:
test1
age0
name:
test2
age1
name:
test3
age2
⑤ 再执行./shmwrite,执行结果如下:
key=50453281
shmgeterror:
Fileexists
⑥ 使用ipcrm-m688137删除此共享内存。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 内存 共享 例子