CS360 Final Exam - April 30, 2015 - James S. Plank

Answers and Explanations

Question 1

Just because the code is here on an exam doesn't mean that you should write code like this. This is bad code. But the purpose is to allow you to demonstrate your understanding of malloc().

As described in the lecture notes, the size of the allocated chunk is stored 8 bytes behind the pointer returned from malloc(). Therefore, after the XXX statement, p and q point to the sizes of their allocated chunks:

Grading: 3 points per part, (4 on part D).


Question 2

This was the only code-writing of the exam. It's a pretty straightforward fork()/dup()/exec() program, with a socket connection thrown in: Call pipe and fork. In the child, dup standard output onto the pipe and call exec for /home/boss/del. In the child, close standard input (so that the child can see EOF), and then read from the pipe. Send all lines from the pipe to both standard output and the socket connection:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sockettome.h"

main()
{
  int p[2];
  char buf[1000];
  int fd;
  FILE *fin;
  FILE *fsock;

  pipe(p);
  if (fork() == 0) {
    dup2(p[1], 1);
    close(p[1]);
    close(p[0]);
    execl("/home/boss/dez", "dez", NULL);
    perror("Execl failed");
    exit(1);
  }

  fd = request_connection("boss.evilcorp.com", 6666);
  fsock = fdopen(fd, "w");
  fin = fdopen(p[0], "r");
  close(p[1]);
  while (fgets(buf, 1000, fin) != NULL) {
    fputs(buf, stdout);
    fflush(stdout);
    fputs(buf, fsock);
    fflush(fsock);
  }
  exit(0);
}

Grading: The following components made up the grade:

You got the "close fd 0" point if you called dup2(p[0], 0), but you didn't get the point if you read from p[0] rather than 0 afterwards.


Question 3

The code is in q3.c.

I was hoping that the code here was straightforward enough. We fork off as many threads as there are words on the command line. The characters of the words then become commands to the threads.

If you want to verify, I have two shell scripts -- q3-manyrun.sh that runs q3 a given number of times -- and q3-script.sh that runs each test case and pipes the answer to sort -u. Running it on my macintosh gives every answer described above:
UNIX> sh q3-script.sh
Run 1
A A B B
B B A A
Run 2
A B C
A C
B A C
B C
C
Run 3
B C A
Run 4
B B A
Run 5
B
B A
Run 6
A B
Run 7
B A C
B C A
UNIX> 
Grading: Runs 2 and 7 were worth 3, and the rest were worth 2. Divide the points by the number of correct answers per run, and that's what each answer is worth. Incorrect answers are negative half that amount. You couldn't get negative for a part.


Question 4

This one is similar in flavor to Question 3, in that we are using the strings of argv as commands. This time, though, it is to two communicating processes. The process with id equal to 1 is the child, and the pricess with id equal to 2 is the parent. The processes share a pipe in the variable p. Each process has an output file in f, which is either O1.txt (for the child) or O2.txt (for the parent).

The commands that each process can execute are:

You are asked, for each run, how the processes exits, and what their output files are.

Grading: 0.5 per correct answer.