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: mailq command taking forever to execute - comp.unix.solaris ...... the same then it's likely the problem is external ... Execute command line program and capture output - comp.lang.rexx ... ... It's primary role is to run MRTG for monitoring ... how to monitor network status using C/C++ library functions - comp ...Another option is to execute the netstat command in the program, but is it ... soft-sys.matlab... of launching an external ... I am writing a system monitor, in C for Linux ... batch + mex files - comp.soft-sys.matlab... are invoked by a Matlab MEX file and Matlab should be able to monitor the execution of ... way matlab has a fair chance of > > > keeping track of when the external program ... Password protection in SAS - comp.soft-sys.sasScott Barry SBBWorks, Inc. Suggested Google advanced search arguments to consider for this topic/thread: execute external program unix site:sas.com send email ... continuously running a program - comp.soft-sys.matlabHowever, in my program, i am continuously reading from an external device ... how to monitor network status ... Another option is to execute the netstat command in the program ... fork() race in SIGCHLD handler - comp.unix.programmerActually at compile time the system call is considered an external function ... where an exec() call is to be made by the child process, the execution of the new program in ... NIOS: Run program from SDRAM - comp.arch.fpgaNIOS: Run program from SDRAM - comp.arch.fpga Run an external program directly, not via ... based on Micron datasheet and I put ... NIOS-II toolchain So to execute "prog ... advice on what programs to install on a new HP 50g - comp.sys.hp48 ...... whats the best file manager, battery monitor, or any other program that ... send to calc using Conn4x > any commands execute ... actual "port," however, because it is an "external ... How do I check the existence of a file programmatically in C ...Of course That's poor program design. read_data ... check the existence of a file programmatically in C ... How can I programmatically monitor CPU load - comp.os.ms ... exec with start and file names with spaces - comp.lang.tcl ...... until someone decides to install my package in C:/ Program ... COMSPEC) /c start {} $help] The you can execute ... file names with spaces - comp ... Run an external program ... Unix & Linux: Executing and Monitoring External Programs in C ...programming.itags.org: Unix & Linux question: Executing and Monitoring External Programs in C, created at:Thu, 01 May 2008 22:21:00 GMT with 1,296 bytes, last updated ... Executing and monitoring external programs - Wrox Programmer ForumsWrox Programmer Forums > C# and C > C# 1.0 > C#: Executing and monitoring external programs ... What is the best approach for executing external program from another program ... 7/25/2012 2:55:17 AM
|