c++ - to system() or fork()/exec()? -
c++ - to system() or fork()/exec()? -
there appear 2 mutual ways of running external executable c in unix,
system()
call and
pid = fork() switch(pid) //switch statement based on homecoming value of pid, //one branch of include , exec() command
is there reason prefer fork/exec on scheme in case functionally equivalent (parent process waits kid finish, no complex info returned child)?.
system
executes command-interpreter, i.e. shell, (a) slower direct fork/exec, (b) may behave differently on different systems , (c) potential security hazard if pass string untrusted source. also, system
waits kid process exit, while might want run concurrently parent process.
more in general, low-level fork/exec gives additional control: before or in between 2 operations, might want chdir
, open pipes, close file descriptors, set shared memory, etc.
(by different systems, don't mean windows vs. unix (as windows doesn't have fork): i'm talking reddish hat linux vs. ubuntu. former uses bash execute passed system
, latter lightweight posix-compatible shell.)
c++ c fork
Comments
Post a Comment