shmat() & shmdt() questions.

Conor P. Cahill cpcahil at virtech.uucp
Thu Sep 13 22:13:18 AEST 1990


In article <24482 at adm.BRL.MIL> TAYBENGH%NUSDISCS.BITNET at cunyvm.cuny.edu writes:
>
>        1) can a child process inherit shmid from parent? i.e. does the shmid
>valid in the following program segment?

Yes.  In fact this is the only way to use private shared memory segments.
The child will also inherit an attached segment, so you could just attach
it once before you fork.

Once you have a shmid, you can pass that around through any means (pipe, 
file, etc) and any other process can use that id to gail access to the
shared memory segment (as long as they have the correct permissions).

I once used this mechanism for a daemon that read data packets from a 
serial port and was to forward them to any one of several other daemons.
The solution  I used was to encode the msgid in the data packet and use 
it in the daemon to send the packet to the appropriate daemon.

>        2) can a child inherit the virtual address return by shmat()?

Yes, see above.

>        3) if a process quit without shmdt() the shared memory it attached
>to, will the OS detect and decrement the shm_nattch in structure sgmid_ds?

Yes.  And if this segment is designed to be a temporary segment that is 
supposed to be removed when the last program is done with it, you can
run shmctl(... IPC_RM) once you have attached to it and then it will 
be removed when the total number of attached processes drops to zero.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.unix.internals mailing list