Mail Archives: djgpp/1996/12/18/15:12:30
From: bas AT teclink DOT net (Digital Hippie)
Date: Tue, 17 Dec 1996 18:46:56 GMT
How do I execute an external program without using system()? Thanks
a bunch
Use:
if ((childpid = fork()) > 0) {
/* Parent process continues. */
/* Code specific to a successful fork. */
...
} else if (childpid == -1) {
/* Fork failed. Handle errors. */
/* Code specific to a failed fork. */
....
} else {
/* Child process code. */
/* Code to open and close files for child and setup a the child's
environment. */
...
execlp( execprog,
chld_argv0,
chld_argv1,
...
chld_argvN,
(char *)0 );
/* Code to handle the failure of execlp() which should never
return on success. */
...
}
/* Parent process only continues... */
...
/* In a REAL OS you might put a wait() system call here to wait for
the child process to finish. Of course since MS-DOG is single
tasking... */
Of course this is exactly what system does except that the process the
execprog argument system uses is: "command -c myjob" rather than "myjob"
which permits myjob to be a script.
--
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com
A proverb is no proverb to you 'till life has illustrated it. -- John Keats
- Raw text -