Home > Archive > Unix Programming > January 2008 > Executing and Monitoring External Programs in C
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Executing and Monitoring External Programs in C
|
|
| mach7sonic@gmail.com 2008-01-29, 7:26 pm |
| 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
| |
|
| 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
| |
| Barry Margolin 2008-01-30, 4:34 am |
| 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:
>
> 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 ***
| |
|
| > 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 ;-)
|
|
|
|
|