1

How can I make a C++ program start another program/process (C++ specifically) and not wait for it to end? (so system() won't work here because it starts the other program/process as child, right?)

I was thinking about starting the second program/process on another thread or something but I'm not sure if that would work.

Thanks for the help.

Edit: Running linux, sorry for missin that precision... (ubuntu 11.10 to be precise)

glesage
  • 955
  • 2
  • 16
  • 31
  • The usual solution is to fork()/exec() See http://stackoverflow.com/questions/1653340/exec-and-fork – sehe Oct 31 '11 at 00:09
  • What you're looking for is [Fork and Exec](http://stackoverflow.com/questions/1653340/exec-and-fork/1653349#1653349) – Brian Roach Oct 31 '11 at 00:12

4 Answers4

3

You can use the nohup with system(). So your called c++ program will work in the background but your other c++ program will get the exit signal from nohup and finish the system call. E.g.:

 system("nohup gedit &")
tune2fs
  • 7,605
  • 5
  • 41
  • 57
2

Create a fork with fork() (or clone() if you wanted threads rather than jobs), and then run the program using execve() or system() in one process, and continue running the original program in the other.

Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
0

Assuming you're doing this on Unix/Linux, you could use fork and a derivative of exec, where fork will basically create a new process by duplicating the current one. You can then also use wait or waitpid to wait for the child and retrieve it's exit status.

EDIT: damn it people always post faster than me. xD

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
0

If it's for MS Windows, CreateProcess() is the way to do it. Quote: Note that the function returns before the process has finished initialization.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180