Possible Duplicate:
How to create a process in C++ on Windows?
How should I run one program from within another in Windows? And why is system()
bad? I have read this, but it seems to cover the topic from a Unix point of view.
Possible Duplicate:
How to create a process in C++ on Windows?
How should I run one program from within another in Windows? And why is system()
bad? I have read this, but it seems to cover the topic from a Unix point of view.
If you need more control and monitoring there are more sophisticated options. For unix systems there is the fork() function and the exec() functions. For Windows specifically there is the CreateProcess() function.
Secondly,
system()
is alright for simple programs, however system offers less flexibility as well as it will be much slower then something implemented into the C/C++ language.
Spawning a child process is a very operating system dependent procedure. The system() in stdlib.h was designed with a Unix environment in mind and is not thread safe. Using system() will work in any C environment but the result is rigidly not defined. Also when using system() the the parent process has to wait until the child process has exited will will significantly slow down your application.
There is a simple way to run a program from within another. Assuming you want to run HelloWorld.cpp from another program. Just write something like:
#include<iostream>
using namespace std;
int main(void)
{
system("g++ -o abcd HelloWorld.cpp && ./abcd");
return 0;
}
(this assumes the program is in the directory you're in.)
Why system() is bad?
Well I don't know if it is bad per se but if you are spawning another process you mostly want to have some kind of control of the process. A call like ShellExecute gives you many more options to fine tune the process and above all get a handle back which you can use for further communication. system()
OTOH gives you no indication whether the process even started.
If you are doing windows programming (and have no portability requirement) then I would recommend using the WinAPI when you can for best result. e.g. file handling, shared memory etc.
system
is not the devil. It is simply what, in the most of the cases, what you are not intending to do.
First of all, what you are asking is almost impossible: the way an OS manage processes is one of the things that makes a OS different from another, hence don't expect a single way of doing to work with all the OSs.
Unless you are working with a very basic function like system
. But what system
does, in fact is not just launching a program. It launches the OS default command interpreter and gives it the string you passed to that function. With the most of the OS, this will result in a program to be launched. But what happens after that depends not only on what the target OS is, but also on how it is configured and what privilege the process ho place the call has in respect to it.
It is very easy to fool such a command: just let the interpreter find a batch file along it search path with the same name as the program you want to call and it will be executed. Simply anyone, even without touching your code, can make your code to exec whatever he likes without any knowledge from you or your user. A very fantastic hippodrome for Trojan horses! Not something to rely on, especially where security is important.
But ... if you are writing a system utility whose purpose is to automates a set of system actions, system
is most likelly what you need.