Solved

Simple example of running a command script

Posted on 2004-04-20
24
223 Views
Last Modified: 2010-04-15
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
Comment
Question by:rintsp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 6
  • +2
24 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10868161


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
 

Author Comment

by:rintsp
ID: 10868214
Is the system() API available on HP-UX and Windows?

Also I cannot see fork() as available on Windows

Pete
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10868232
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!

 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10868408

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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10868491
In windows,i think pthread is the command to create a new thread.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10868623
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10868637
Thanx Kent for the exec.. info.
I never knew that exec.. dont return to the calling program.
0
 

Author Comment

by:rintsp
ID: 10868664
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
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 50 total points
ID: 10868760
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
 

Author Comment

by:rintsp
ID: 10868951
Tried that, could not get it to compile....
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10869105
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10869130
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
 
LVL 3

Expert Comment

by:dtkerns
ID: 10869360
well, you COULD say dangerous .. but on Unix, without exec, NOTHING would EVER get done.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10869378
Hi dtkerns,

Why is that?
I mean you could always use fork() and system()
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10871060
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
 

Author Comment

by:rintsp
ID: 10871577
On Windows system() does not produce a new thread....hence I have used winexec
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10871658
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
 

Author Comment

by:rintsp
ID: 10872293
On UNIX I think you are correct, but on Windows this is the behaviour I am seeing.....hence I have used WinExec
0
 
LVL 3

Expert Comment

by:dtkerns
ID: 10873223
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
 

Author Comment

by:rintsp
ID: 10875804
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
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10877366

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
 
LVL 1

Expert Comment

by:jonathan6587
ID: 10881347
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question