linux进程通信之(九):共享内存

815阅读 1评论2010-11-05 andyluo324324
分类:LINUX

#include
#include
#include
#include
#include
#define BUFSZ 2048
int main()
{
 int shmid;
 char *shmadd;
 if((shmid-shmget(IPC_PRIVATE,BUFSZ,0666))<0)//创建共享内存
 {
  perror("shmget");
  exit(1); 
 }
 else
 {
  printf("create shared-memory:%d\n",shmid);
  system("ipcs -m"); 
 }
 if((shmadd=shmat(shmid,0,00))<(char *)0)//映射共享内存
 {
  perror("shmat");
  exit(1); 
 }
 else
 {
  printf("attached shared-memory\n"); 
 }
 system("ipcs -m");//显示系统内存情况
 if((shmdt(shmadd))<0)//删除共享内存
 {
  perror("shmdt");
  exit(1); 
 }
 else
 {
  printf("delect shared-memory\n"); 
 }
 system("ipcs -m");
 exit(0);
}
/*--------------------------------------------------------
1.共享内存的实现
共享内存的实现分为两个步骤,第一步是创建共享内存,这样用到的函数是shmget,也就是
从内存中获得一段共享内存区域。第二步映射共享内存,也就是把这个段创建的共享内存影
射到具体的进程空间中去,这里使用的是shmat,到这里,就可以使用这段共享内存了.当然还
有撤消映射的操作,其函数为shmdt.
2.shmget函数语法要点:
1)所需要的头文件:
#include
#include
#incldue
2)函数原型:
int shmget(key_t key,int size,int shmflg)
3)函数传人值
key:IPC_PRIVATE
size:共享内存区大小
shmflg:同open函数的权限位,也可以用八进制表示法
4)函数返回值:
成功:共享内存段标识符
出错:-1
3.shmat函数语法要点:
1)所需要的头文件:
#include
#include
#include
2)函数原型
char *shmat(int shmid,const void *shmaddr,int shmflg)
3)函数的输入参数
shmid:要映射的共享内存区标示符
shmaddr:将共享内存映射到指定位置(若为0则表示把该段共享内存映射到调用进程的地址空
间)
shmflg
SHM_RDONLY:共享内存只读
默然0:共享内存可读写
4)函数返回值
成功:被映射的段地址
出错:-1
4.shmdt函数的语法要点:
1)所需要的头文件:
#include
#include
#include
2)函数原型
int shmdt(const void *shmaddr)
3)函数的输入参数
shmaddr:被映射的共享内存段地址。
4)函数返回值
成功:0
出错:-1
5.实验:
[root@localhost the_eight_step]# gcc shmadd.c -o shmadd
[root@localhost the_eight_step]# ./shmadd
create shared-memory:8929268
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status     
0x00000000 65536      root      600        393216     2          dest        
0x00000000 98305      root      600        393216     2          dest        
0x00000000 131074     root      600        393216     2          dest        
0x00000000 163843     root      600        393216     2          dest        
0x00000000 196612     root      600        393216     2          dest        
0x00000000 229381     root      600        393216     2          dest        
0x00000000 262150     root      600        393216     2          dest        
0x00000000 294919     root      600        393216     2          dest        
0x00000000 327688     root      600        393216     2          dest        
0x00000000 360457     root      666        2048       0                      
attached shared-memory
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status     
0x00000000 65536      root      600        393216     2          dest        
0x00000000 98305      root      600        393216     2          dest        
0x00000000 131074     root      600        393216     2          dest        
0x00000000 163843     root      600        393216     2          dest        
0x00000000 196612     root      600        393216     2          dest        
0x00000000 229381     root      600        393216     2          dest        
0x00000000 262150     root      600        393216     2          dest        
0x00000000 294919     root      600        393216     2          dest        
0x00000000 327688     root      600        393216     2          dest        
0x00000000 360457     root      666        2048       0                      
shmdt: Invalid argument

----------------------------------------------------*/
上一篇:linux进程通信之(八):信号通信中的信号集函数
下一篇:linux进程通信之(十):消息队列

文章评论