system()
does not necessarily open a new terminal. But – unlike the
exec()
family of functions – it runs the given command in the
"command processor". This means that
system("thingumabob")
will
not simply start a new
"thingumabob[.exe]" process, but it rather starts a new
shell sub-process –
cmd.exe on Windows, or the default shell (e.g.
bash) on Linux/Unix – and it passes the given command (e.g.
"thingumabob") as a command-line argument to the shell process. In other words, the code
system("thingumabob")
effectively starts:
1 2
|
execl("cmd.exe", "cmd.exe", "/c", "thingumabob", NULL); /* <-- Windows */
execl("/bin/sh", "sh" "-c", "thingumabob", NULL); /* <-- Linux/Unix */
|
That is also why shell
built-in commands work with
system()
, but most certainly
not with
exec()
.
I think if used in a pure GUI program, then the command (shell) started by
system()
runs "invisibly" in the background. And, even worse, unlike with the
popen()
function, you won't even be able to read the child's
standard output from the parent process. All output of the child process gets lost in the void...
________
For example, see the implementation of
system()
in the glibc (GNU C Library) used in most Linux systems:
https://github.com/bminor/glibc/blob/master/sysdeps/posix/system.c#L147
Effectively the implementation comes down to:
1 2 3 4
|
int system(const char *line)
{
posix_spawn(&pid, "/bin/sh", /*...*/, { "sh, "-c", line, NULL });
}
|