How to Not Wait for a System() Command to Finish? (In C)

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

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.

wait(NULL) is not waiting till forked process is finished

Your child process never exits! When the system function is done, then the child process will just continue. You need to explicitly exit it.

Or do like most shell-like implementations do and use the exec family of function to execute the command instead of system. The exec functions replace the process with the executed program, and never returns (unless there's an error). When the executed program terminates, that terminates the process as well.

Wait for the system() call to complete

I believe this is handled by the --wait command line argument, which does seem to be a pretty recent addition. You could try building gedit on your own, to at least verify if it works (since your distro probably won't have a recent-enough build).

How to wait for system() completion before advancing into next cycle

I found out what the problem was.

I saw here: https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file

that in order to save the stderr to file I needed to do &>output.txt.
So I was doing "./foo 1 2 3 &>output.txt" but that & causes the system process to go into background.

+1 to @Random832 for guessing it (even though I never said I was using &> -sorry guys, my bad ).

Btw, if you want the stderr to be exported to a file you can use 2>output.txt

Waiting for child process to terminate, or not - C

Although waitpid would get you the return status of the child, its default usage would force parent to wait until the child terminates.

But your requirement (if i understood correctly) only wants parent to wait for a certain time, alarm() can be used to do that.

Then, you should use waitpid() with a specific option that returns immediately if the child has not exited yet (study the api's parameters). So if the child didn't exit, you could kill it, else you already receive its return status.

Call an executable from C++, and wait until it is done, on Linux

system() waits for the command to finish:

http://linux.die.net/man/3/system

system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed. During
execution of the command, SIGCHLD will be blocked, and SIGINT and
SIGQUIT will be ignored.

Waiting for system call to finish

I think that you are looking for fork/execv. Here is an example:

http://www.cs.ecu.edu/karl/4630/spr01/example1.html

An other alternative could be popen.



Related Topics



Leave a reply



Submit