Chinaunix首页 | 论坛 | 博客
  • 博客访问: 688663
  • 博文数量: 240
  • 博客积分: 3616
  • 博客等级: 大校
  • 技术积分: 2663
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-21 23:59
文章分类

全部博文(240)

文章存档

2013年(6)

2012年(80)

2011年(119)

2010年(35)

分类: LINUX

2010-11-05 17:18:44

#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

----------------------------------------------------*/
阅读(744) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-07 19:13:06

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com