Sunday 20 November 2011

How can I run another program after mine?in C programming

How can I run another program after mine?

Of course, the easiest way to run another program after yours is to put them both in a batch file, one after the other. When you run the batch file, the programs listed will execute in order. But you already knew that.

There is no specific method for performing a “when my program is finished, run this one” function call in C or in DOS. However, C provides two sets of functions that will allow a program at any time to run another program that basically ends the first program upon execution of the second. If you were to put such a call at the end of your first application, you could do exactly what you need. The two functions provided are exec() and spawn(). In reality, each function is actually a family of functions, each function allowing a unique twist over the other functions in the family. The exec family of functions is execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), and execvpe(). The following list shows what the e, l,

p, and v additions mean to the function:
e An array of pointers to environment parameters is explicitly passed to the child process.
l Command-line arguments are passed individually to the executable.
p Uses the PATH environment variable to find the file to be executed.
v Command-line arguments are passed to the executable as an array of pointers.

Which combination of options you choose in your application is entirely up to you and the needs of the application you want to launch. Following is an example of a program that calls another application whos name is specified on the command line:

#include <stdio.h>
#include <process.h>
char *envString[] = { /* environment for the app */
“COMM_VECTOR=0x63”, /* communications vector */
“PARENT=LAUNCH.EXE”, /* name of this app */
“EXEC=EDIT.COM”, /* name of app to exec */
NULL}; /* must be NULL-terminated */
void
main(int argc, char ** argv)
{
/* Call the one with variable arguments and an environment */
_execvpe(“EDIT.COM”, argv, envString);
printf(“If you can read this sentence, the exec didn’t happen!\n”);
}

In the preceding short example, _execvpe() is called to execute EDIT.COM, the DOS file editor. The arguments to EDIT come from the command line to the example. After the exec has occurred, the original application has gone away; when EDIT exits, you are returned to the DOS prompt. If the printf() statement is displayed on-screen, something went awry in the exec attempt, because you will not get there if the exec succeeds. Note that the environment variables for EDIT.COM are completely meaningless. However, if you did exec a program that needed the environment variables, they would be available for use by the application.

There is a second way to accomplish this same task—spawn(). The spawn function family is spawnl(),
spawnle(), spawnlp(), spawnlpe(), spawnv(), spawnve(), spawnvp(), and spawnvpe(). The e, l, p, and v
appendages mean the same as they do with exec. In fact, this function family is identical to exec except for
one small difference—spawn can either launch another application while killing the original, or launch
another application and return to the original when the second application is complete. The argument list
for the spawn functions is identical to that for exec except that one additional argument is required: you must
use _P_OVERLAY (original application dies) or _P_WAIT (return to original when finished) as the first argument to the spawn functions. The following example demonstrates the same task as the preceding example:
#include <stdio.h>
#include <process.h>
char *envString[] = { /* environment for the app */
“COMM_VECTOR=0x63”, /* communications vector */
“PARENT=LAUNCH.EXE”, /* name of this app */
“EXEC=EDIT.COM”, /* name of app to exec */
NULL}; /* must be NULL-terminated */
void
main(int argc, char ** argv)
{
/* Call the one with variable arguments and an environment */
_spawnvpe(_P_OVERLAY, “EDIT.COM”, argv, envString);
printf(“If you can read this sentence, the exec didn’t happen!\n”);
}

The only difference here is the name change from exec to spawn, and the additional mode argument. What is nice about spawn’s capability to overlay versus wait is the ability to make a runtime decision regarding waiting or leaving during the spawn. In fact, the _P_WAIT argument answers the next FAQ.

Cross Reference:

XX.11: How can I run another program during my program’s execution?

No comments:

Post a Comment