I have the following C++ - scenario under CentOS.
Process P1 contains:
Incoming socket connections are created with:
Outgoing socket connections are created with:
Process P1 is now forked to P2 and in P2 there should be a new outgoing connection in a new thread T2. I am not interested in the other old connections here.
What exactly do I have to consider here after the fork in P2? What are the best practices here? Are all my assumptions correct? I would realize it like the following:
After the fork
Would a following scenario also be possible where I want to reuse D4?
After the fork
General question:
If the live-time of P2 is always shorter than of P1 are not all descriptors automatically released/counted down after P2 terminates? Do I need to close any FD's in this case?
I close D1 in P2 directly, because I don't want to listen in different processes at the same port, although this would be possible. Correct?
Yes.
Because all FD's were copied (Ref-Counted?), I can safely close D2 and D3 in P2 without endangering the communication in P1, right?
Yes. And if you are not going interact with 'dupped' D2,D3 in P2,you should close them.
T1 is "dead" when forking anyway and so I should close D4 in P2 here too, or? Finally, I can spawn T2 in P2 and create a new outgoing socket D5 there
Only the thread that calls 'fork' will be 'cloned' into child process, all others will 'vanish'.
So, if T1 is not the one 'forking', it is 'dead'. Similar as previous item, if you are not going to access the 'dupped' D4 in P2, you should close it.
I spawn T2 and share the usage of D4 together with T1 from P1. Is this race-conditon-safe? Is this a good/common practice/pattern?
No, there is no 'race', the D4 in P2 is really 'dupped' from D4 in P1.
You can operate the D4 in P2 with out concerning the 'original' D4.