CS360 Midterm Exam #2. March 11, 2014. James S. Plank

Answers and Grading


Question 1: 16 Points

Here we go again. In the first three statements we set:

Next, we set x to 0x572b20. s[1] is 0x572b30, so the for loop sets the four bytes starting at 0x572b30 to 'T' '0' 'L' '8'. The '8' overwrites the null character, so s[1] becomes seven digits long. That means that the string s[1] is "TOL8<+W".

Finally, the hex. 0x7c54 = 0111 1100 0101 0100. The right shift turns that into: 0111 1100 0101 01, and when we regroup that by fours, that becomes 0001 1111 0001 0101. The answer is 0x1f15.

0xa45e = 1010 0100 0101 1110. The left shift turns that into: 1010 0100 0101 1110 00, and when we regroup by fours, that becomes 10 1001 0001 0111 1000. The answer is 0x29178.

Final output:

1. 0x572b20
2. X+W
3. H+W
4. 5712716
5. 5712696
6. T0L8<+W
7. 0x1f15
8. 0x29178

Grading

Two points per part.

For line 5, you got full credit if the integer you printed was the one directly after the one that you printed in line 4.

For line 8, you got 1.8 points if you left off the initial "2".


Question 2: 15 Points

Grading

1 point per answer. I wrote a program to grade this so that y'all could get optimal partial credit. First, there were two typos on the exam: Here's the partial credit rubric:


Question 3: 16 Points

This is a nuts and bolts opendir/readdir/stat/jrb program:

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include "jrb.h"

main()
{
  DIR *d;
  struct dirent *de;
  struct stat buf;
  JRB t, tmp;

  d = opendir(".");
  if (d == NULL) { perror("."); exit(1); }

  t = make_jrb();
  for (de = readdir(d); de != NULL; de = readdir(d)) {
    if (stat(de->d_name, &buf) != 0) {
      perror(de->d_name);
      exit(1);
    }
    if (jrb_find_int(t, buf.st_ino) == NULL) {
      jrb_insert_int(t, buf.st_ino, new_jval_s(strdup(de->d_name)));
    }
  }
  jrb_traverse(tmp, t) printf("%s\n", tmp->val.s);
}

Grading: 16 points

8 points for reading the files and printing them, another 8 for calling stat and using the red-black tree on inodes.


Question 4: 16 Points

You had to spend time reading the code, but I was hoping that it was pretty clear that R16_read() reads 16 bytes from fd into buf, but it does it by using a 4K buffer. That way, it's only doing one read() call every 4096 bytes.

The mechanics of eof and p are not too subtle. If we've reached EOF, then eof is the index one past the end of the file. If we haven't readched EOF, then eof is -1.

On the other hand, p is the pointer in the buffer to where we need to read next. When it equals 4096, we have to make a read() call to fill the buffer.

Part A: We need to set up the buffer, and then successively call R16_read() until it returns an empty string:

#include <stdio.h>
#include <stdlib.h>
#include "r16.h"

main()
{
  char buf[17];
  void *r16;

  r16 = R16_setup(0);
  while (1) {
    R16_read(r16, buf);
    if (buf[0] == '\0') exit(0);
    printf("%s", buf);
  }
}

Part B: Slower because we are making 4096/64 = 64 times as many read() calls.

Part C: Slower, but not by much. Fread() buffers, so it's going to make the same number of read() calls. However, the following three factors will make it slower:

  1. Since we call it with 15, it will have to make more fread() calls than the Part A program made R16_read() calls.
  2. There are fewer parameters to process on the R16_read() calls than the fread() calls.
  3. The R16_Read() calls work in units of 16, so it never has to worry about calls that read from the end of the buffer, then require a read() call, and then from the beginning of the buffer. Since 15 does not divide 4096, the fread() version has to both check for this, and then do it every now and then.

Part D: If my input file contains the byte 0, then the printf() statement will stop printing when it sees the 0, because that's the null character.

Part E: The read() call will fail and return -1. Therefore, it will act as though the buffer is full, and it will fill in buf with the first 16 random bytes of r->b.

Grading