------------------------------------------------------------------------------- A while ago, I posted some questions on shared memory segments to this discussion group. A number of people expressed interest in a summary of the responses. So here it is. First of all, thanks to Sanjoy Mukherjee at pual.rutgers and Rich Casto of Bellcore for their replies. 1). When a process does a shmget to create a shared memory segment, is the memory space for it immediately allocated ? Answer : immediately. Sanjoy and Rich concurred on this point. 2). Process P1 creates a memory segment and attaches to it. Then process P2 gets the memory segment with the same key and attaches to it. If P1 detaches from the memory segment and deletes it, what happens to process P2's access to the memory segment? Sanjoy : The kernel maintains a reference count and the memory is not deleted until it is decreased to 0. P2 can keep on accessing the memory. (BTW, the same key without specification of IPC_PRIVATE will create a memory segment that is shared) 3). Following that, if another process P3 creates a memory segment with the same key as that used above, what happens to the memory segment that process P2 is attached to ? P2 is also OK 4). Should P3 use back the same physical memory space as P2 or will the system create a new physical memory space for P3 ? Rich : >From the SunOS manual page: IPC_RMID Remove the shared memory identifier specified by shmid from the system. If no processes are currently mapped to the corresponding shared memory segment, then the segment is removed and the associated resources are reclaimed. Otherwise, the segment will persist, although shmget(2) will not be able to locate it, until it is no longer mapped by any process. As I was writing this, I realized that it answers your questions #3. After P1 detaches from the segment, P2 can still access it, but P3 can't attach to it. Sanjoy thinks that P2 and P3 should use the same physical memory segment. Me: Since, there are differing views on this one, I decided to test it out. I wrote a small program (attached below) to duplicate the scenario described above. I ran it in the background. It has plenty of sleeps in it so it gives sufficient time for monitoring. After starting it, I used the command 'ipcs' (report ipc status) with options -m -a. I found that Rich is right. When a shared memory segment has been removed by the owner, it is marked deleted (D in status report). When all processes have detached from that segment, NATTCH = 0, the segment is physically removed. However, the important thing is that when the segment has been removed by the owner, the key field of the segment is zeroed. Hence, when P3 creates the memory segment with the same key, the new segment is created. Segments in P3 and P2 are physically separate and there is no conflict because key field of segment used by P2 is zero, while key field of segment used by P3 has the correct value. I tested the program on Decstation 5000 running Ultrix 4.2. If anyone is interested, please cut out the program and test it for yourself. I'd like to hear from people who can test this on different systems. 5). Can anyone recommend a good book that discusses the Unix shared memory segment in sufficient detail to answer questions such as those above ? Sanjoy : I strongly recommend Maurice J. Bach's "Design of Unix Operating System" (Prentice Hall). It mainly deals with System V (shared memory and rest of IPCs) For 4.2 BSD you can try Leffler, et al - 4.2 BSD Unix Operating System(Addison Wesley) Rich : "Advanced Unix Programming" by Marc J. Rochkind, (c) 1985 Prentice-Hall, ISBN 013-011818-4 "Unix Network Programming" by W. Richard Stevens, (c) 1990 Prentice-Hall, ISBN 013-949876-1 Both books have about 16 pages on the subject including sample code. Since Rochkind's book is 7 years old, the info might not be up to date. In fact, when I used shared memory in a project 2 years ago, I found a discrepancy. It says "Eventually you destroy the segment by calling shmctl() with a command of IPC_RMID. It's a serious error to destroy a segment that is mapped into a process's address space." But it isn't possible to do that - the manual said that IPC_RMID waits for all processes attached to the shared memory segment to exit before it removes the segment. Me: I have the books by Richard Stevens and Maurice Bach. They do not descibe implementation details of the Unix kernel to answer scenario questions like the ones above. Anyone out there want to write such a book? -------------------------------------------------------------------------------