Lab 12 Preparation

To prepare for Lab 12 on the exec() function calls and the corresponding O/S services, first review the lecture notes on Exec. Copy the source files for the sample programs exec 1 to exec 5 and exec 0 (all will be found in the directory ~allisor/Student/exec in the Linux Lab) and compile them, then run each.

Read over the source code carefully and check the output from your test runs until you understand why each one behaves as it does. Modify some or all of the samples to see how the behaviour changes as you re-compile and re-run each.

In Stevens/Rago you should already be familiar with most of Chapter 8. Read especially section 8.10 on exec(), but you may also wish to review fork() and wait(). Note also that the system() function in section 8.13 is only to be used in this course for clearing the screen if necessary (which is hardly ever, in spite of some students desire to do it all the time): system("clear"); although you can easily duplicate this in a few lines by using fork()/wait() and execlp().

Finally, you can use your programs from previous labs as the basis for this lab. Copy the completed source files and your Makefile (you do have a Makefile, don't you?) into a new directory.

exec(3): List form [derived from man 3 execl]

#include <unistd.h>

int execl(char *pgmpath, char *arg0, char *arg1, ... NULL);

int execlp(char *pgmfile, char *arg0, char *arg1, ... NULL);

The list form requires that the entire list of command-line arguments be given as a NULL-terminated list to exec. It can be used when the number of arguments is known before the exec call needs to be made.

exec(3): Vector form [derived from man 3 execv]

#include <unistd.h>

int execv(char *pgmpath, char *argarray[]);

int execvp(char *pgmfile, char *argarray[]);

The vector form requires that the list of command-line arguments be given as vector similar in structure to argv[], which is how it will be used by the exec()ed program (execv() will count the elements that you supply in order to set argc). That is, it is an array of pointers to character arrays (each ending in the end-of-string value, '\0') with the last entry set to NULL. It can always be used, unlike execl() which needs a predetermined list, and is especially useful when the number of arguments is not known before the exec call needs to be made.

Each member of the family of exec(3) functions will replace the current process with another one, leaving all the process environmental items (i.e., signal and I/O tables) as they were before the exec was issued. There is also an 'le' and a 've' form for the list and vector argument styles, used when the environment variables need to be changed or replaced for the new process. Exec is often, but not always, invoked from a child process.

If any form of exec(3) ever returns, an error has occurred. The returned value will be -1 and errno will be set.