• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Simple example of running a command script

Guys

Anyone got a simple example of how to run a command script ("c:\test.bat") from inside a C program?

It should not wait for the script to complete.

Cheers

Pete
0
rintsp
Asked:
rintsp
  • 7
  • 6
  • 6
  • +2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:


You can run the script with the system() API.

system ("C:\\test.bat");

To run the script without waiting, you'll want to fork() another task or create another thread and run the script from there.


Kent
0
 
rintspAuthor Commented:
Is the system() API available on HP-UX and Windows?

Also I cannot see fork() as available on Windows

Pete
0
 
ankuratvbCommented:
You can use the exec... set of functions.
They are available on windows.

system() will block furthur execution of ur program till the system() is executing.



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kent OlsenData Warehouse Architect / DBACommented:

exec...() does not meet the originally stated goals.  The APIs in this family overwrite the current process with the exec...() function so that it never returns to the original caller.

fork() is not available on Windows.  Use CreateProcess() or a related API.


Kent
0
 
ankuratvbCommented:
In windows,i think pthread is the command to create a new thread.
0
 
ankuratvbCommented:
Well,try this:

system("start/m test.bat");

This assumes Win95/98/NT.

start lets you run other dos and windows programs and spawns a new process to execute it.so system() will return as soon as start returns.

type start at the command prompt and see the instructions.
0
 
ankuratvbCommented:
Thanx Kent for the exec.. info.
I never knew that exec.. dont return to the calling program.
0
 
rintspAuthor Commented:
I have no example of CreateProcess that is simple for me to understand.

Do you have a simple example?

For the moment I have used WinExec for windows, and will use fork() and system() on UNIX.

Cheers

Pete
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi rintsp,

Here's an example from the MicroSoft support pages.  Also, here's the URL to the function description.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp


void main( VOID )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( NULL, // No module name (use command line).
        "MyChildProcess", // Command line.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        FALSE,            // Set handle inheritance to FALSE.
        0,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        &si,              // Pointer to STARTUPINFO structure.
        &pi )             // Pointer to PROCESS_INFORMATION structure.
    )
    {
        ErrorExit( "CreateProcess failed." );
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

Hi ankuratvb,

exec...() was a lot more useful (to me) before windows programming!  Calling it from a Windows application can have disastrous effects since the app can no longer clean up any of the Windows task chains.


Kent
0
 
rintspAuthor Commented:
Tried that, could not get it to compile....
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Make sure that you've got at least the first two of these header files, and perhaps all 4:

#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>

Kent
0
 
ankuratvbCommented:
Hi kent,

exec.. then are a dangerous set of commands.
Since they dont return to the calling program,they can be used for Hacking.by calling the hacking program using exec..from within my program.
0
 
dtkernsCommented:
well, you COULD say dangerous .. but on Unix, without exec, NOTHING would EVER get done.
0
 
ankuratvbCommented:
Hi dtkerns,

Why is that?
I mean you could always use fork() and system()
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi ankuratvb,

It's more the other way around.....

You can execute a command by doing a fork() and the child process then issuing an exec...().

system() initiates its own fork or thread, I don't recall which.  Because it's support in *nix and Winblows, I suspect that system() calls create new threads.


Kent
0
 
rintspAuthor Commented:
On Windows system() does not produce a new thread....hence I have used winexec
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Interesting....

system() fires up an address space and initiates a shell to execute the command.  Since fork() is not an option, if it doesn't create a new thread it must use a variant of CreateProcess().


0
 
rintspAuthor Commented:
On UNIX I think you are correct, but on Windows this is the behaviour I am seeing.....hence I have used WinExec
0
 
dtkernsCommented:
on Unix system() is a library call, .. ie user space, both fork and exec are system calls, ie privilaged.

system is not much more then a fork, if child exec, else wait for death of child
0
 
rintspAuthor Commented:
So if I want to fire a background shell script on UNIX, I will need to use fork and exec, as system() will wait for the shell script to complete?

I want to fire and forget

Pete
0
 
Kent OlsenData Warehouse Architect / DBACommented:

fork() and exec..() will do what you want.  So will fork() and system().

There are subtle differences between system() and the exec..() functions, so choose your weapon correctly.  :)   But it sounds like you can savely use either one.


Kent
0
 
jonathan6587Commented:
Pete,

You could also use _beginthreadex

Jonathan
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now