Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does java.lang.ProcessBuilder.start() starts a process by a fork()?

java.lang.ProcessBuilder.start() calls to java.lang.ProcessImpl.forkAndExec(), which is a native method. From what I learned in my Operating System class, a fork will basically create a clone of the process which called it. I have checked the PPID of processes created by ProcessBuilder.start() and confirmed that those PPIDs are equivalent to PID of current Java process.

However, ProcessBuilder.start() may run an arbitrarily process which share nothing with current Java process. So why does java.lang.ProcessBuilder.start() starts a new process by a fork()?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Why does ProcessBuilder.start() starts a process by a fork()?

Because if it didn’t fork() a child process, then the subsequent execve() would replace the JVM’s code and data with the code and data of the new command you are running.

Why?

Because that is how the fork() and execve() system calls work.

Why?

Because:

  • it allows the child process (prior to the exec) to set up the file descriptors for that the exec’d command will see, and potentially other things
  • it was designed that way back in ~1970’s era UNIX, and there hasn’t been a strong enough case do it (significantly) differently.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading