Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Subprocesses

Name: Anonymous 2018-12-18 10:21

Is it bad practice to invoke a shell command via a subprocess in your Java/Python/C++ program? Sometimes it's easier to just use a shell utility using a one-liner than it is to try and figure out how to do the same thing in like 100+ lines of code in the language your program is currently written in.

Name: Anonymous 2018-12-18 10:45

using subprocesses has an overhead for launching them, and using shell also has an overhead for interpreting them. you can also get into portability issues (ask your're are self: will this work on Windows?), and if a shell utility is not standard then you also introduce dependencies, similar to using an external library. additional problem is that if used badly, it can lead to issues like shell injection or argument injection (which doesn't sound serious but can be - injecting an argument into something like tar can lead to arbitrary code execution). also, subprocess return output streams and return codes, while in your're are language you will preferably work with a more structured data (if output streams are structured, you need to parse them which is more overhead and more potential for bugs).

the way I've seen subprocesses used, their're are usually a bad practice: people use them for opening files and grepping them, deleting files/folders, shit like that. it might be easier to do from shell but the above mentioned issues make it not worth it, plus learning how to do those things in your're are language will be useful.

sometimes, subprocesses are the right thing to do. if I want to execute an external tool/script that is quite complex, already implemented and (preferably) I don't care much about its outputs, I'd definitely use subprocesses. if I want my program to schedule/balance/dispatch parallel stuff that doesn't share memory, subprocesses can be the best way of doing that.

but in general, you should learn how to do things in your're are language. and if you want to rely on shell commands, maybe structure your're are program in unixy way: make it take input from stdin and pipe shell commands into it instead of making your're are program execute and interpret shell commands.

Name: Anonymous 2018-12-23 6:52

On any Unix like OS, a child off a single mega process like the JVM is asking for pain. The right way is to do what init does and use a dedicated process to fork things for you. You don't need to worry about unintentional thread interactions or hacks like VM overcommit if you do this.

Unfortunately, none of the big language runtimes do it right, because the implementors don't want to deal with any other runtime processes; just theirs. It's quite sad really.

Name: Anonymous 2018-12-24 13:58

in your Java [...] program
Oh boy!

Name: Anonymous 2018-12-24 14:45

>>1
Since Linux kernel does it, i don't see why not.

Name: Anonymous 2018-12-25 0:20

Bad: system()
Good: exec*()

Name: Anonymous 2018-12-26 4:02

if there's already code/software to do a task adequately, it's good programming practice to use it instead of reinventing the wheel
if this shell command is a significant portion of your program's runtime and you have performance issues, it may be worth reimplementing

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List