29

I want to execute a binary within my C code. Which is better to execute with? popen() or system()

EDIT: I tried to use system, but the process executing seems to get stuck in the end and does not return to my code.

Any suggestions on what to do?

Thanks

Wolf
  • 9,679
  • 7
  • 62
  • 108
Syntax_Error
  • 5,964
  • 15
  • 53
  • 73

2 Answers2

45

popen() gives you control over the process's input or output file streams. system() doesn't. If you don't need to access the process's I/O, you can use system() for simplicity.

system() is in C89 and C99; popen() is Posix only (though the Windows API also has one).

Both functions invoke some form of a shell to execute the command (e.g. /bin/sh on Linux, and probably cmd.exe on Windows). If you want to execute an executable file directly and you are on Posix, you can also look at the exec*-family of functions in conjuction with fork() (since exec() replaces the current process).

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    @HeathHunnicutt: Neither did I until five minutes ago, when I followed Joachim's advice :-) – Kerrek SB Dec 16 '11 at 18:25
  • 5
    Important note about `popen`: It give you control over the input **OR** output stream, not both. – Some programmer dude Dec 16 '11 at 22:24
  • 5
    @JoachimPileborg: Sure. Maybe I should have written "xor"? English and C are different, alas. – Kerrek SB Dec 16 '11 at 22:32
  • If I close the pipe fd with `pclose`, does `pclose` return the exit value of the command I fired with `popen`? – Strubbl Apr 16 '13 at 17:13
  • @Strubbl: According to `man pclose` it would seem so, though you should check carefully what happens when the process exits with a signal. – Kerrek SB Apr 16 '13 at 17:22
  • A recent test revealed that I get the exit code of the command multipled by 256 as a return value from pclose, e.g. for exit code 43 I get the value 11008. Why that? But at least I get my exit code. – Strubbl Apr 16 '13 at 17:33
  • 1
    @Strubbl: It'll be the same semantics as for `wait`, i.e you have to use those macros to figure out whether the process returned normally or via signal, and then the return value is shifted. – Kerrek SB Apr 16 '13 at 17:54
5

Use popen if you want to run a shell command and want the parent process to be able to talk to the child. (It hooks the child's input or output up to the stream you get back.) Otherwise, prefer the exec family of functions (likely in conjunction with fork); exec'd processes inherit most open file descriptors (including stdin, stdout, and stderr), so you can hook input and output up to whatever you like...plus, there are fewer security implications.

system is generally best avoided unless you have to run shell commands. It spawns a shell to run the command, and the shell can parse the command any way it likes. In particular, certain environment variables (like $IFS and/or $PATH) can be modified in such a way as to cause the parent to execute programs you never intended it to. Although popen appears to do the same thing, it at least provides functionality that makes it worthwhile in the general case.

cHao
  • 84,970
  • 20
  • 145
  • 172