Example Homework & Test Questions
Q:
Why does "fread" take two integer arguments? What's wrong with
this from an interface design perspective, and how would you fix it?
A:
"fread" takes two different size arguments--size and nmemb--which it
multiplies together. The idea is that "size" gives the number of
bytes per element (where the definition of "element" is up to the user,
and could be bytes, integers, or records), and "nmemb" gives the number
of elements.
The problem with this interface is that it's too complex. A
better design would have the caller multiply the size and element
count, and pass "fread" a single integer byte count. Google for
"fread(ptr," and you'll see that something like 90% of all calls to
fread pass the constant 1 for one or the other argument.
Q:
If you write a program that uses too much memory and starts paging, why
do other programs (e.g., the debugger window you're trying to use to
stop the runaway program) slow down? How would you change the
operating system's behavior to fix this?
A:
When a program B asks for memory, to make room the operating system may
have to swap some existing memory to disk. If this paged-out
memory contains code, data, or I/O buffers that another program C will
need, then C will have to read that memory back in before it can
continue execution. This means program B's request for memory
slowed down program C--and because disk I/O is so slow, the slowdown
can be a factor of millions.
The fix for this is to somehow stop B's requests from paging out data
belonging to C. For example, we could limit the amount of memory
B is allowed to consume (in UNIX, using "ulimit"), which would prevent
it from impacting the performance of other processes. If only
process C is important, we could mark its data as locked in memory (in
UNIX, using "mlock"), to prevent it from being paged out.
Example Questions I'll Never Ask
14 (a). Simulate the FIFO, Optimal, LRU, and MRU page replacement
algorithms for this page reference stream. Simulate three runs,
assuming a memory size of 1, 2, and 3 pages, initially all empty.
Show which page is to be swapped out at each step.
1,2,3,1,1,4,3,2,1,4,3,2,1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4
[ This is an example of the sort of actual test question I've had to
crank through by hand. It's just tedious busywork. It'd
make a good machine problem, though. ]
26 (c). How many times does the
letter 'e' appear on page 487 of the textbook? Count both upper
and lower case. (NO CHEATING: CLOSED BOOK)
[ This is satire of the irrelevant "minutia" question that we
professors love to ask, because they're really easy to grade. ]
83 (b). Sort the following
operating systems by the length of the facial hair of the listed key
designer. For the purposes of this question, consider a 5 o'clock
shadow as having some small positive length epsilon.
[ This is satire of the "make a judgement" question (about a clearly
related but useless topic). ]