35

Similar to:
program not executing anything after a call to system()

I am fairly new to using C but basically, I want to execute the following line:

int a = system("python -m plotter");

which will launch a python module I developed. However, I want the rest of my c program to keep running instead of waiting for the command to finish executing (the python app is on an infinite loop so it wont close automatically). Is there any way to do this using C/C++?

Update: the solution was:

int a = system("start python -m plotter &");
gh4x
  • 746
  • 1
  • 9
  • 16
  • For what platform(s) do you need this? – pyroscope Aug 05 '11 at 20:24
  • 2
    http://stackoverflow.com/questions/6552712/program-not-executing-anything-after-a-call-to-system - exact same thing? – hari Aug 05 '11 at 20:25
  • @hari: The earlier question and answers didn't cover the Windows-specific solution (which turns out to be what this questioner needed). – Keith Thompson Aug 05 '11 at 23:08
  • 2
    @hari, nicol-bolas, ybungalobill, littleadv, pmr, and mu-is-too-short: the question is very similar I admit. However, my question was "how do I not wait for system() call to finish" and the other question was "Why are my calls after system() not happening?" My question has a great answer from keith-thompson whereas the other question's chosen answer just describes why the system() call prevented the following commands from executing. – gh4x Jan 13 '16 at 16:18

5 Answers5

40

system() simply passes its argument to the shell (on Unix-like systems, usually /bin/sh).

Try this:

int a = system("python -m plotter &");

Of course the value returned by system() won't be the exit status of the python script, since it won't have finished yet.

This is likely to work only on Unix-like systems (probably including MacOS); in particular, it probably won't work on MS Windows, unless you're running under Cygwin.

On Windows, system() probably invokes cmd.exe, which doesn't accept commands with the same syntax used on Unix-like systems. But the Windows start command should do the job:

int a = system("start python -m plotter");

As long as you're writing Windows-specific code (start won't work on Unix unless you happen to have a start command in your $PATH), you might consider using some lower-level Windows feature, perhaps by calling StartProcess. That's more complicated, but it's likely to give you more control over how the process executes. On the other hand, if system() meets your requirements, you might as well use it.

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
  • 1
    I ran a small test on my linux, and if the shell terminates, the background command will also terminate, so that doesn't necessarily solve the problem – littleadv Aug 05 '11 at 20:26
  • 3
    What happens if you change it to `int a = system("nohup python -m plotter &");`? – Keith Thompson Aug 05 '11 at 20:29
  • I tried both, with simply the & as well as with both the nohup and the & but neither works. I am programming in Windows by the way. – gh4x Aug 05 '11 at 20:43
  • 4
    Ok, that explains it. `system()` probably invokes `cmd.exe`, which doesn't accept the same syntax that Unix shells do. I should have thought of that earlier, but it would have been helpful if you'd told us in your question what platform you're using. A quick Google search found [this](http://www.tomshardware.com/forum/34598-45-windows-command-background-unix), which might be useful; try `start` rather than `nohup`. – Keith Thompson Aug 05 '11 at 20:46
  • 2
    start seems to have fixed it! I'm sorry I didn't realize this kind of thing was platform specific and not C specific. – gh4x Aug 05 '11 at 21:59
5

I believe if you add a '&' to the end of the command it will work. '&' tells the command to run in the background

int a = system("python -m plotter &");
love_me_some_linux
  • 2,291
  • 1
  • 12
  • 7
4

There's no way in the standard library, but you can fork out or create a separate thread that would run it on the background, if your OS supports it.

littleadv
  • 19,072
  • 2
  • 31
  • 46
  • Complement: [man page for the `exec` family of functions](http://linux.die.net/man/3/exec) – Simon Aug 05 '11 at 20:31
2

"System" command on Linux will let the rest of code execute only when it has done executing itself.

You should use fork() if you want simultaneous processing.

jscode
  • 123
  • 1
  • 1
  • 6
1

The word for this is an Asynchronous function/method call. C and C++ don't have such a feature so you have to either call the function in a separate thread or use a system call that will run it in a separate process. Both of these methods tend to be platform specific, although there are cross platform threading libraries.

I know that in unix to do this in a separate process you would use fork(2) to create a new process and exec(3) to execute a new program in that process. I do not know what system calls would be required in Windows, but should be something similar.

David Brown
  • 12,466
  • 3
  • 36
  • 53