Reputation: 11
might be my question is a bit too deep in the matter and i think of a problem that does not exist. I hope you can help. ;-) The problem is the following: I start a process in Linux at startup (rc.d) which then creates a shared memory and forks two daemon processes. The daemons, once detached from the parent processes have inherited the shared memory of the parent process but have also an own session and are no more connected to the parent. Do they have an own link to the shared memory and does the kernel count the references? I ask because i would like to safely detach the parent process before it bails out. In my implementation it works, the shared memory is detached by the parent process but the daemons still can use it. But ist that safe or just by coincidence??
Thanks for your thoughts in advance!
Martell
Upvotes: 1
Views: 70
Reputation: 14866
In linux each process has a File Descriptor Table
, file descriptors index into a per-process file descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the file table.
Now at fork, each of the child processes will have its own FD
and each entry pointing to the same Objects as in the parent process. So if the parent process closes his shared-memory index in his FD
this won't affect the other processes as the OS won't close this shared memory as its still in use by different processes. (In other words, child processes are still attached to this S.M and have a link to it and the OS counts them)
Upvotes: 2