Angs
Angs

Reputation: 1645

is there another way than system() to execute a binary

I develop a C code on Linux and I would like to execute a binary say /usr/sbin/binary_program -p xxx, Is there another way than system() call to execute a binary?

Upvotes: 1

Views: 210

Answers (3)

It is important to realize that you can have several programs running simultaneously, and communicating thru pipes (or others Inter Process Communication). This is mostly possible thru a mixture of syscalls.

I strongly suggest reading Advanced Linux Programming, or some other good books explaining a lot more (than we can do in a few minutes) about various syscalls(2) involved, notably fork(2), pipe(2), dup2(2), execve(2), waitpid(2) and several others (perhaps poll(2) for multiplexing, e.g. to avoid deadlocks in circular pipes). The system(3) function is built above these syscalls (and /bin/sh)

That Advanced Linux Programming book has an entire chapter devoted to processes.

I also suggest to understand how a Unix command shell works. Either by studying the source code of some simple free shell (like sash) or at least by strace-ing it.

Practically speaking, popen(3) is more useful then system(3). You can get the output of the companion command.

Some libraries (Poco, Qt, Glib/GTK) also have powerful process management functions.

A new process is created with fork which is tricky to understand. A new program is started in the same process with execve.

All processes are created by fork (or perhaps vfork) except some few started magically by the kernel (/sbin/init, /sbin/modprobe, ...)

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215287

Yes, and in general, system should never be used, for at least these reasons:

  • It suffers from all the dangers of shell quoting issues, so anything but a hard-coded command line is potentially dangerous.
  • It is not thread-safe.
  • It interferes with signal handling in the calling program.
  • It provides no way to get output from the executed program except for the exit status, unless the command explicitly saves output to a file.

For executing external programs, you should use posix_spawn, or fork followed by one of the exec-family functions. However, if possible you should avoid dependency on external programs/commands, especially when it would be easier and less error-prone to do the work directly in your program. For example I've seen ridiculous usages like system("sleep 1"); instead of sleep(1);.

Upvotes: 5

ouah
ouah

Reputation: 145839

Yes, you can use the exec* family of functions.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/execv.html

If needed to simulate the behavior of system you can fork and then call an exec function.

The POSIX page of system says:

The system() function shall behave as if a child process were created using fork(), and the child process invoked the sh utility using execl() as follows:

execl(< shell path>, "sh", "-c", command, (char *)0);

Upvotes: 3

Related Questions