- 论坛徽章:
- 0
|
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <openssl/buffer.h>
- #include <signal.h>
- #define SHM_MODE SHM_R|SHM_W|IPC_CREAT|0666
- struct message{
- char name[10];
- int age;
- };
- int main()
- {
- struct message *mes;
- struct message *p;
- int shmid;
- int ret;
- pid_t pid;
- // long *Local=0;
-
- mes=(struct message *)malloc(10*sizeof(struct message));
- memcpy(mes[0].name,"abc",6);
- mes[0].age=100;
-
- shmid=shmget(IPC_PRIVATE,10*sizeof(struct message),SHM_MODE);
- if(shmid==-1)
- {
- printf("create shm error\n");
- return -1;
- }
- p=(struct message *)shmat(shmid,NULL,0);
- if(p==(struct message *)(-1))
- {
- error("shmat");
- return -1;
- }
- memcpy(p,mes,sizeof(p));
- printf("p[0].name=%s\n",p[0].name);
- signal(SIGCHLD,SIG_IGN);
- if((pid=fork())==0)
- {
- memcpy(p[1].name,"def",9);
- printf("p[1].name=%s\n",p[1].name);
- memcpy(mes,p,sizeof(mes));
- printf("mes[1].name=%s\n",mes[1].name);
- ret=shmdt((void *)p);
- if(ret==-1)
- {
- fprintf(stderr,"shmdelete failed\n");
- exit(1);
- }
- exit(0);
- }
- sleep(3);
- printf("mes[0].name=%s\n",mes[0].name);
- printf("mes[1].name=%s\n",mes[1].name);
- ret=shmdt((void *)p);
- if(ret==-1)
- {
- fprintf(stderr,"shmdelete failed\n");
- exit(1);
- }
- // shmctl(shmid,IPC_RMID,NULL);
- return -1;
- }
复制代码
为什么mes[1].name在父子进程中都显示不出来?? |
|