Computer Organization Fall 2000

Homework 1
Frequently Asked Questions

Questions:
Calling exec The unix time command Converting string to int

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.

Q: If I just use the UNIX time command on one of the subprograms (like iadd), it prints out the time with three decimal places, but when I run the wrapper program, it only prints out to one decimal place (real 0.1 s, etc.). So unless I'm using very, very large numbers, the times printed out are always 0.0 s. is there something I can do to change this, or is this not a big deal?

A: This is not a big deal. You are probably running 2 different time commands. From the command line your shell (bash?) is providing a time command (as an internal command). /usr/bin/time is what you exec, and the output format is apparently different.

You should be using very large numbers of iterations (hundreds of millions) - the idea is to get something that takes at least a few seconds. Otherwise the error in time measurement is too large, and the measurements are not reliable. 1/10s of seconds is more than enough for this assignment.

Q: How do I convert a command line argument from argv[1] to an integer?

A: The simplest way is to use the function atol (alpha-to-longint). Here is an example:

int lim;

lim = atol(argv[1]);

You can also use sscanf - which is like scanf but the input comes from a string. "man sscanf" for more details...