Q:
My program has an integer variable that is set to the
number of iterations entered by the user. How do I pass this
value to exec?
A:
The parameters to exec (I'll use execl as an example - this is
the same exec function I showed in class) are all strings.
The first thing you need to do is to convert the number numItrs
to a string. There are many ways to do this, but the simplest is
to use the function sprintf which is like printf by the destination
is a string instead of standard output. Here is an example of
using sprintf:
char buf[100];
sprintf(buf,"%d",numItrs);
Now buff will be an ascii string that has the value of numItrs in it.
At this point your probably have everything as a string, so you can
call execl using something like the code shown below. I'm assuming you
are execing the time command and passing the name of your program in a
variable named prog and the number of iterations as
parameters.
execl("/bin/time","time",prog,buf,NULL);
Note that the first parameter to execl is the path of the program you
want to exec. the second parameter is what that program should get
as ARGV[0], the third is what it gets as ARGV[1], etc. So the call
above means that the program /bin/time would get 3
parameters.