#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
阅读(1425) | 评论(0) | 转发(0) |