4

This question is similar to this one that I searched.

However, in my case, I wanted to launch a sub-process from Java,where it would not inherit files/ports from the parent process (JVM). Is this possible in Java at all? Any workarounds?

from what I gathered, it appears that Java does not provide a way to mark file descriptors with the close on exec flag (FD_CLOEXEC), unlike C. Any insights would be greatly appreciated.

Thanks!

Community
  • 1
  • 1
Anand
  • 409
  • 3
  • 7
  • 15
  • Which platform are you working with? The semantics of inherited handles when launching subprocesses are quite different between Windows and POSIX. – Greg Hewgill Jan 25 '12 at 04:01
  • possible duplicate of [Launch a child process on OSX/Unix that doesn't inherit files/ports](http://stackoverflow.com/questions/1309521/launch-a-child-process-on-osx-unix-that-doesnt-inherit-files-ports) – Greg Hewgill Jan 26 '12 at 20:48
  • 1
    Neither Java nor the JVM describes any semantics around inheriting file handles (since the behaviour is system dependent). So when you eliminate the Java part of your question, it's the same as the similar one you linked to. See also http://stackoverflow.com/questions/5507082/is-there-a-way-to-automatically-close-certain-handles-on-a-fork for further information. – Greg Hewgill Jan 26 '12 at 20:49

1 Answers1

2

Any files you open in Java are automatically marked with FD_CLOEXEC. If you run the JVM in strace you would see this output corresponding to a FileInputStream.open call:

5926  open("file.txt", O_RDONLY|O_LARGEFILE) = 6                                                                         
5926  fstat64(6, {st_mode=S_IFREG|0664, st_size=869, ...}) = 0
5926  fcntl64(6, F_GETFD)               = 0
5926  fcntl64(6, F_SETFD, FD_CLOEXEC)   = 0

If you checked the OpenJDK source code I'm sure you would find a fcntl call immediately following the open.

Joni
  • 108,737
  • 14
  • 143
  • 193