I can't read shared memory in C

c, shared-memory

Solution

You never put anything in the shared memory; you just changed the value of `text` to point to something other than the shared memory.

Instead of:

text=&words[0];

You probably wanted something like:

memcpy(text, &words[0], strlen(words[0]) + 1);

Problem

I have a program with two processes and two files and I want to read a var with the code of the second file by shared memory but I only get the "testing" word, not the text. the code of first file of the program: ``` 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); } ``` And the code of the second file: ``` a=shmget(key, 200, 0666); text=shmat(a,0,SHM_RDONLY); printf("testing, %s", text); ``` Any idea? Thanks.

Original source