[comp.unix.internals] usable of attached shared memory

TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu (02/27/91)

Dear Unix-Wizards,
        I have implemented a simple message-passing utility using SysV shared
memory. I have an question of the use of shared memory after the process
fork(). The question is:
        If a process fork a child process AFTER attaching the shared memory
to its virtual addr say char *SHM, will SHM in the child process points to
the same shared memory? The question can be illustrated in the follwoing
C code segment:

        char    *SHM;
        int     pdata,  /* data sent by parent */
                cdata;  /* data recv/stored by child */
        int     shmid;

        shmid = shmget();
        ....
        SHM = shmat(shmid, 0, 0);
        if (fork())
        {
                /* parent process */
                memcpy(SHM, pdata, sizeof(data));
                ....
        }
        else
        {
                /* child process */
                memcpy(cdata, SHM, sizeof(data)); <--- is SHM (of child)
                                                       still points to the
                                                       same shared memory
                                                       attached by parent??
                ......
        }

        Thanks in advance.

- Beng Hang
  Dept of Information Systems and Computer Science
  National University of Singapore