我能';在C语言中读取共享内存

我能';在C语言中读取共享内存,c,shared-memory,C,Shared Memory,我有一个有两个进程和两个文件的程序,我想通过共享内存读取第二个文件的代码,但我只得到“测试”字,而不是文本。程序的第一个文件的代码: a=shmget(key, 200, 0666|IPC_CREAT); text=(char *)shmat(a,0,0); text=&words[0]; if ((P2=fork())==1) { perror("fork"); exit(-1); } if (P2==0)

我有一个有两个进程和两个文件的程序,我想通过共享内存读取第二个文件的代码,但我只得到“测试”字,而不是文本。程序的第一个文件的代码:

a=shmget(key, 200, 0666|IPC_CREAT);
text=(char *)shmat(a,0,0);
text=&words[0];
     if ((P2=fork())==1)
     {
     perror("fork");
     exit(-1);
     }
          if (P2==0)
          {
          execl("prog2","prog2",NULL);
          }
以及第二个文件的代码:

a=shmget(key, 200, 0666);
text=shmat(a,0,SHM_RDONLY);
printf("testing, %s", text);
有什么想法吗?
谢谢。

您从未在共享内存中放入任何内容;您刚刚更改了
text
的值,以指向除共享内存之外的其他内容

而不是:

text=&words[0];
你可能想要这样的东西:

memcpy(text, &words[0], strlen(words[0]) + 1);
另外
fork()==-1
不是1。