Hi,
I am writing a server program where it receives message from client and broadcast message to all previous clients. I need to create a shared memory between processes, but it seems that the shared memory is not working.
Here is my code:
int shmid2; key_t key2; void* shm2;
string name_list;
key2=ftok("tmp",'d');
//create
if ((shmid2 = shmget ( key2, sizeof(char)*1000, IPC_CREAT | 0666)) < 0) {
perror("shmget2");
exit(1);}
//attach
shm2 = shmat(shmid2, (void *)0, 0) ;
name_list= (char*) shm2;
if ( shm2 == (char *) -1) {
perror("shmat2");
exit(1);}
... do other things...
switch (pid=fork()){
case -1:
{ perror("ERROR on fork");
break;}
case 0://children
{
...modify name_list by getting message and append message to name_list..
name_list.append(message);
break;}
default://parent
close(connection);
}
When I modify name_list in the children process, it seems that this modification is not seen by other processes. Can anyone give any suggestions? Thanks!!
UPDATE: I tried to change to this as suggested, but still does not work.
name_list = (char*) shmat(shmid2, (void *)0, 0) ;
Anyone can help me on this? Many thanks!