CS360 Midterm -- May 2, 2002
Answer to Question 3
This is quite straightforward: fork, make socket connection, dup, close,
execv. Creating the argument string was a pain, although you could
do that by simply starting with argv[1], since argv is
guaranteed to be NULL terminated. If you didn't remember that,
it's a simple malloc() call to create a new argv array.
main(int argc, char **argv)
{
char **tmpargv;
int i;
int fd;
int dummy;
i = fork();
if (i < 0) { perror("fork"); exit(1); }
if (i > 0) {
wait(&dummy);
} else {
fd = request_connection("cetus3a.cs.utk.edu", 13700);
dup2(fd, 2);
close(fd);
tmpargv = argv+1;
execv(argv[1], tmpargv);
perror("execv");
exit(1);
}
exit(0);
}
Grading
10 points:
- Calling fork: 1 point
- Setting up the socket connection: 1 point
- Duping it to standard error: 1 point
- Closing the socket connection: 1 point
- Not having the socket connection open in the parent: 1 point
- Correctly making argv: 2 points
- Calling execv: 1 point
- Explicitly calling exit if execv returns: 1 point
- Calling wait in the parent: 1 point
As always, you received points off for a variety of things:
- Doing things in the wrong order: 1 to 5 points off
- Having launcher's standard err go to the socket: .5 points
- Extraneous garbage: 1 to 5 points off