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: As always, you received points off for a variety of things: