写程序
- #include"sys/ipc.h"
- #include"sys/shm.h"
- #include"sys/types.h"
- #include"unistd.h"
- #include"stdio.h"
- #include"string.h"
- typedefstruct{
- charname[4];
- intage;
- }people;
- main(intargc,char**argv)
- {
- intshm_id,i;
- key_tkey;
- chartemp;
- people*p_map;
- char*name="..";
- key=ftok(name,0);
- if(key==-1)
- perror("ftokerror!");
- shm_id=shmget(key,4096,IPC_CREAT|0666);
- if(shm_id==-1)
- {
- perror("shmgeterror!");
- return;
- }
- p_map=(people*)shmat(shm_id,NULL,0);
- temp='a';
- for(i=0;i<10;i++)
- {
- temp++;
- memcpy((*(p_map+i)).name,&temp,1);
- (*(p_map+i)).age=20+i;
- }
- if(shmdt(p_map)==-1)
- perror("detacherror!");
- }
读程序
- #include"sys/ipc.h"
- #include"sys/shm.h"
- #include"sys/types.h"
- #include"unistd.h"
- #include"stdio.h"
- typedefstruct{
- charname[4];
- intage;
- }people;
- main(intargc,char**argv)
- {
- intshm_id,i;
- key_tkey;
- //chartemp;
- people*p_map;
- char*name="..";
- key=ftok(name,0);
- if(key==-1)
- perror("ftokerror!");
- shm_id=shmget(key,4096,IPC_CREAT|0666);
- if(shm_id==-1)
- {
- perror("shmgeterror!");
- return;
- }
- p_map=(people*)shmat(shm_id,NULL,0);
- //temp='a';
- for(i=0;i<10;i++)
- {
- printf("name:%s\n",(*(p_map+i)).name);
- printf("age%d\n",(*(p_map+i)).age);
- }
- if(shmdt(p_map)==-1)
- perror("detacherror!");
- }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/732489