Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to not wait for a system() command to finish? (in c) [duplicate]

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 &");
like image 275
gh4x Avatar asked Aug 05 '11 20:08

gh4x


3 Answers

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.

like image 59
Keith Thompson Avatar answered Sep 21 '22 15:09

Keith Thompson


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 &");
like image 30
love_me_some_linux Avatar answered Sep 20 '22 15:09

love_me_some_linux


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.

like image 4
littleadv Avatar answered Sep 23 '22 15:09

littleadv