0

Suppose we have requested shared memory and attached it to the main process of our program.

This program creates several processes. As the manual of shmat says

After a fork(2) the child inherits the attached shared memory segments.

So we don't have to attach the shared memory to the child processes.
But what about detaching it? Should we do it at the child's code as well?
Or just detaching the shared memory at the main process and destroying it is enough?

Chris
  • 3,619
  • 8
  • 44
  • 64
  • 1
    Detaching shared memory segments is like closing open file handles - it is performed automatically by most OS kernels when a process terminates (wilfully or not), but good programming practices demand that you do it explicitly. – Hristo Iliev Dec 03 '12 at 12:31
  • @HristoIliev: That's what I think, as well. Leaving it without detaching would lead to having dangling pointers. – Chris Dec 03 '12 at 18:13

1 Answers1

0

It really depends on what you are doing after. But if you are calling execve or one of its cousins, it will detach the shared memory segments.

kmkaplan
  • 18,655
  • 4
  • 51
  • 65
  • I am not calling execve at all. After the child process exits it has finished its job (it's a client - server assignment) – Chris Dec 02 '12 at 17:13