First, your function header is in the oldy-moldy K&R style which does not conform to ANSI C. gcc still compiles it, but you should use the standard instead of an anachronism:
int main(int argc, char *argv)
1) The program has two argument in main as in
Do I included these value at the command line when I
run the program as in ./waitfile prog1 a
Those two parameters are
the command line.
argv is an array of strings which contain the command and every one of the parameters given on the command line. argc is the number of strings in argv. You use them to read in the parameters.
2) How do I get this program to run ?
You have to give it a filename or else it will crash. See 4).
3) How does main(argc, argv) work ?
The book must describe it somewhere, because this has been a C standard for just about as long as there's been C.
As I said, argv is an array of strings which contain the command and every one of the parameters given on the command line and arcg is the number of strings in argv. parameters are delimited by white space. So using your example of ./waitfile prog1 a :
argc is the number of parameters plus the command; here it is 3.
argv is the command that was entered. It would be waitfile (I'm not sure if the directory path (./) is also part of the string).
argv is the first parameter, which here is prog1 .
argv is the second parameter, which here is a .
In the code, we see that the first parameter is used as a filename in the fopen() call. We also see that the first parameter is required because if argc is less than 2 then all that was entered in was the command and no parameters. Later, we see that if argc is equal to 2, then only one parameter was entered, so we do not use the command, else a second parameter was entered so we do use a command.
The thing to remember is that the value of argc will always be one more than the number of parameters entered and that parameters 1 through n will be in argv through argv[n].
BTW, execlp and execvp will replace the current program with the program being called, so they never return from the call.
4) root:~# ./waitfile
./waitfile: Ã3: Unknown error 3221225157
You didn't give the program any filename, so it exited with an error message. BTW, I have no idea what the code is for the error() function and I could not find it in the man pages. What's up with that function?