Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1064090
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: C/C++

2009-07-09 17:27:28

#include
#include
#include
#include
#include
#include
#define KEY 1234
#define SIZE 1000
void main(int argc,char **argv[])
{
   int shmid;
   char *shmadd;
   struct shmid_ds  buf;
   if(fork()==0){
      shmid = shmget(KEY, SIZE, IPC_CREAT|0600);
      shmadd = (char *) shmat(shmid, NULL, 0);
      strcpy(shmadd, "i am toy");
      shmdt(shmadd);
      return;
   }else {
      sleep(1);
      shmid = shmget(KEY, SIZE, 0);
      shmctl(shmid, IPC_STAT, &buf);
      printf("shm_segsz = %d byte\n",buf.shm_segsz);
      printf("shm_cpid = %d \n",buf.shm_cpid);
      printf("shm_lpid = %d \n",buf.shm_lpid);
      shmadd = (char *) shmat(shmid, NULL, 0);
      printf("get:  %s \n",shmadd);
      shmdt(shmadd);
      shmctl(shmid, IPC_RMID, NULL);
   }
}
编译(makefile):
all : clean mem
.PHONY : all
clean :
 -rm *.o mem core
mem : mem.o
 cc -g -o mem mem.o
mem.o : mem.c
 cc -g -c mem.c
阅读(1375) | 评论(0) | 转发(0) |
0

上一篇:posix共享内存练习

下一篇:网游DIY(一)

给主人留下些什么吧!~~