www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/12/18/15:12:30

From: kagel AT quasar DOT bloomberg DOT com
Date: Wed, 18 Dec 1996 14:54:49 -0500
Message-Id: <9612181954.AA16227@quasar.bloomberg.com >
To: bas AT teclink DOT net
Cc: djgpp AT delorie DOT com
In-Reply-To: <32b6ea24.2158405@news.teclink.net> (bas@teclink.net)
Subject: Re: executing files
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019