Executing and Monitoring External Programs in C

  • Follow


I want to write a C program that randomly writes other C programs,
compiles them, executes them, monitors their progress, and captures
the output.  (I'm experimenting with genetic programming).  Something
like this:

int main() {
   while(1) {
      ...
      // randomly write program to 'file.c'
      ...
      system_command('gcc file.c');
      output = system_command('/a.out');
      // some sort of threading thing to check if the program is
      // taking too long, and if so, kill it
   }

}

Question 1)  How do I execute external programs? (i.e. what is
'system_command'?).  What #includes do I need?

Question 2)  How do I capture the output of the program?

Question 3)  How can I kill a program if it has been running for,
say,
x milliseconds?  This is basically a question about threads, since
I've never done multithreading in C.  I think I'll have to use
fork().

Question 4)  The program will be writing thousands to millions of
programs.  Thus, I'd like the compile-assemble-link process to be as
quick as possible.  What is the quickest compiler available?  I don't
need the bells and whistles of gcc, just something really fast.

Thanks in advance,
-mach7
0
Reply mach7sonic (5) 1/29/2008 7:35:52 PM

On Jan 29, 2:35 pm, mach7so...@gmail.com wrote:
> I want to write a C program that randomly writes other C programs,
> compiles them, executes them, monitors their progress, and captures
> the output.  (I'm experimenting with genetic programming).

Cool, I've only seen that done with lisp ;-)

>
> Question 1)  How do I execute external programs? (i.e. what is
> 'system_command'?).  What #includes do I need?

the system()/popen(), respectively in stdlib.h and stdio.h.

>
> Question 2)  How do I capture the output of the program?
>

the easiest way is to use popen() otherwise you go with the fork()/
pipe() duet.

> Question 3)  How can I kill a program if it has been running for,
> say,
> x milliseconds?  This is basically a question about threads, since
> I've never done multithreading in C.  I think I'll have to use
> fork().

you do not have other choice, modified code needs to be executed in a
separate process. You cannot compile code and replace it on a running
process (text segment is not writable), that why lisp is the most used
language for genetic programming.
And yes, you will use the kill command and use some kind of timer
(alarm() is not the best but should be enough).

>
> Question 4)  The program will be writing thousands to millions of
> programs.  Thus, I'd like the compile-assemble-link process to be as
> quick as possible.  What is the quickest compiler available?  I don't
> need the bells and whistles of gcc, just something really fast.

your expectations are, I think, too high.
Anyhow AFAIK tcc (the tiny compiler) is one of the fastest for
compile. The code quality is usually not as good as gcc or icc but it
should be ok.

Cheers,
Paulo
0
Reply vodoom (70) 1/29/2008 8:40:14 PM


In article 
<74601de8-088e-41ef-b4f3-24135c1ccf55@1g2000hsl.googlegroups.com>,
 ppi <vodoom@gmail.com> wrote:

> On Jan 29, 2:35 pm, mach7so...@gmail.com wrote:
> > Question 3)  How can I kill a program if it has been running for,
> > say,
> > x milliseconds?  This is basically a question about threads, since
> > I've never done multithreading in C.  I think I'll have to use
> > fork().
> 
> you do not have other choice, modified code needs to be executed in a
> separate process. You cannot compile code and replace it on a running
> process (text segment is not writable), that why lisp is the most used
> language for genetic programming.

While I wouldn't recommend it, couldn't you do it by compiling a shared 
object, and then loading it into the current process with dlopen()?

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Reply barmar (5660) 1/30/2008 5:19:29 AM

> While I wouldn't recommend it, couldn't you do it by compiling a shared
> object, and then loading it into the current process with dlopen()?
>

True, that could do the trick ;-)
0
Reply vodoom (70) 1/30/2008 4:22:20 PM

3 Replies
78 Views

(page loaded in 0.057 seconds)

Similiar Articles:













7/25/2012 2:55:17 AM


Reply: