Does a forked process inherit any (server) ports bind from the parent process?
I have a java process which forks a child process and from netstat -anpt |grep
I see that both parent and child are bound to the same port.
Is this possible? Is there a way to get arround this?
Asked
Active
Viewed 9,146 times
2

Jim
- 18,826
- 34
- 135
- 254
1 Answers
0
Does a forked process inherit any (server) ports bind from the parent process?
Its inherits the resources of its parent. This is the way the OS behaves.
Is there a way to get around this?
The child has to close all the resources it doesn't need. This is relatively easy to do in C but not simple in Java. There might be a way to start a Java process with clean set of resources, but I don't know of a way to do this.

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
-
The problem I have is that I enable remote debugging port in the parent process `-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8989` and when I do `netstat` I see that both child and parent process are "bind" to this port.So it is not possible for me to debug.Do you have any ideas on what I could try to work arround this? – Jim Oct 09 '12 at 12:21
-
Why does this prevent debugging? Only one process will be accepting connections of that port. – Peter Lawrey Oct 09 '12 at 12:22
-
Because the forked process is `C++` and it seems that it tries to deploy a server and endsup using that port.So `jpda` can not bind there.Does this sound reasonable assumption?This is what I understand that is happening – Jim Oct 09 '12 at 12:25
-
Which debugger is used to debug the C++ process? A different one, right? – TheBlastOne Oct 09 '12 at 12:46
-
@TheBlastOne:I am not trying to debug `C++` process.Only the java process.It seems that the forked process deploys a server and perhaps is bind to that port first? – Jim Oct 09 '12 at 13:02
-
BTW I assume you are using Runtime.exec? – Peter Lawrey Oct 09 '12 at 13:43
-
1Look at the FD_CLOEXEC thingy in http://stackoverflow.com/questions/6945865/process-started-from-system-command-in-c-inherits-parent-fds – TheBlastOne Oct 09 '12 at 13:48
-
1@Jim that totally depends upon how you fork the child process. I wish you'd add the process-spawning call from your Java code that does the fork, then we might answer that. – TheBlastOne Oct 10 '12 at 12:03