Executing and Monitoring External Programs

M

mach7sonic

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
}
}

I suspect I'll be writing this on a linux box, but I can work with
Windows code too.

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
 
V

vippstar

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:
OT for this newsgroup post, try <This newsgroup is about ISO C programming, which does not have
threads, processes, etc.
 
M

mach7sonic

On Jan 29, 9:22 pm, (e-mail address removed) wrote:> I want to write a C program that randomly writes other C programs,

OT for this newsgroup post, try <This newsgroup is about ISO C programming, which does not have
threads, processes, etc.

my bad, how do i delete this thread?
 
M

Martin Ambuhl

system_command('gcc file.c');
^^^^^^^^^^^^^^
There is no such standard C function. Look for the function system() in
your elementary C text.

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

There is no such standard C function. Look for the function system() in
your elementary C text.
 
V

vippstar

my bad, how do i delete this thread?

You cannot do that, one more reason to think double before posting on
usenet.
Don't worry, this newsgroup gets a *lot* of OT questions daily, if
every OT poster stopped asking OT questions after being directed to
the appropriate newsgroup (assuming you will) it will do more good
than bad to ask an OT question.
 
S

santosh

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
}
}

I suspect I'll be writing this on a linux box, but I can work with
Windows code too.

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

Within Standard C the only way is to use the system() function.
Question 2) How do I capture the output of the program?

If your OS supports piping then you can take advantage of that.
Otherwise your program has to create a disk file to communicate data.
Other concepts like sockets and shared memory are also possible but all
of this depends on the capabilities of your system. None of this doable
within purely Standard C. You need to code for something like POSIX
(SUSv3).
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().

Again these are not possible within Standard C. But POSIX specifies many
facilities to do all that you are asking.

Please ask in <or
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.

Try Tiny C compiler.
 
S

santosh

my bad, how do i delete this thread?

Just abandon it. Most Usenet servers do not recognise CANCEL requests.
Google Groups will allow you to delete posts you have written through
it, but not other peoples replies. The deletion also has effect only
for Google's servers, not others in the Usenet network.

Just abandon the thread.
 
J

jacob navia

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
}
}

I suspect I'll be writing this on a linux box, but I can work with
Windows code too.

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

#include <stdlib.h>

system("program.exe");
Question 2) How do I capture the output of the program?

You can open a pipe and redirect the output of the pipe to a file.

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().
Windows offers threads. Just read the documentation, it is well done
and quite easy.

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.

gcc is way too slow.
lcc-win is faster, but for your kind of application you would need
a faster method. If you have deep pockets I can build a solution for
you.

If not, you will have to learn quite a lot.

:)
 
M

Malcolm McLean

Question 1) How do I execute external programs? (i.e. what is
'system_command'?). What #includes do I need?
system("gcc temp.c -lm\n");
I think it is in stdlib.h. If not it will be stdio.h certainly a standard
header.
Question 2) How do I capture the output of the program?
Very much harder. You cat set up pipes, but not in standrad C. What you can
do is

system("gcc temp.c -l > temp.out\n");

Then fopen() temp.out.
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().
Can't do in standard C.
But
system(gcc temp.c -l > temp.out &");
will run it in the background for you. You then iteratively call time()
then, if the process is still running (system("ps\n")), execute a
system("kill ... \n"); with the id you extracted from ps.
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.
I'm sure there's a really fast compiler avialable. In fact you might want to
look at tiny C compilers to see if you can't hold the programs in memory.

(The answers are semi-jocular, by the way. Whilst you can do what you want
in standard C, it is far more trouble than any other method and too
inefficient for practical work).

Try getting sourcecode to a tiny C interpreter, embedding it in your app,
and running the programs that way.
 
W

William Pursell

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:

You might want to seriously consider using something other
than C. Generating text output (the C program) is really
easy in languages like Python and perl. A reasonable
choice might even be to use a simple shell script which
generates the code using awk and then executes/monitors/captures.
If the goal is a toy for playing with C, you still get
some benefit because you'll need to write code to
construct the C code.

Consider how easy this is:

#!/bin/sh

cat << EOF > file.c
#include <stdio.h>
#include <stdlib.h>
int main(void) { puts("hello, world"); return EXIT_SUCCESS; }
EOF

gcc file.c -o prog && ./prog > output && cat output
 
W

Walter Roberson

You might want to seriously consider using something other
than C. Generating text output (the C program) is really
easy in languages like Python and perl.

The original poster might also wish to consider writing a small
binary interpreter and generating random byte-code sequences for it.
This approach would avoid having to compile and link each program,
and the interpreter could possibly be written with built-in bounds
checks so as to smoothly handle problems with wild accesses; it
could also have built-in timing checks.

Proceeding in this way would not be the same as writing random C
programs (imagine the generations required to evolve proper
format strings for printf()!), but it might be be more useful GA
environment.
 
K

Keith Thompson

Malcolm McLean said:
system("gcc temp.c -lm\n");
I think it is in stdlib.h. If not it will be stdio.h certainly a
standard header.

The argument to system() is implementation-specific, but ... what's
the '\n' for?
Very much harder. You cat set up pipes, but not in standrad C. What
you can do is

system("gcc temp.c -l > temp.out\n");

Then fopen() temp.out.

Presumably the OP wants to capture the output of the program, not of
the compiler.

[...]
 
K

Keith Thompson

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
}
}
[...]

The details of compiling an executing the program, capturing its
output, etc. are system-specific, but the idea of generating a random
C program is interesting.

The simplest approach would be to generate random text, but getting
that to converge to a valid C program seems difficult. If you can
generate something random in a higher-level representation, you can
exclude invalid programs and concentrate on selecting from the valid
programs those that produce output that you like. Generating a random
parse tree consistent with the C grammar, and then generating C source
from that, will at least give you syntactically correct C, but you'll
still have semantic errors (undeclared identifiers, etc).
 
M

mach7

Wow, I got a lot of replies for an OT post :) I particularly like Mr.
Roberson's suggestion of an interpreter. I actually got that
suggestion from a friend and went ahead and implemented it, and it
works like a charm because I can avoid the compile-link process.

In response to Mr. Thompson's remarks about the difficulty of
generating random programs: you're absolutely right. That's why I'm
actually generating random brainfuck programs (http://en.wikipedia.org/
wiki/Brainfuck) and converting them to C on the fly. These programs
only have 6 valid operators (plus two i/o operators), each of which is
a single character, so it's very easy to generate valid code. The
only complexity is making sure that you balance your loop constructs.

If anyone is interested, I could post the simulator code - it's fun to
play with.

-mach7
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top