Assignment 6, Solutions
Part of
the homework for 22C:112, Fall 2012
|
if (a == b) thing1(); if (!strcmp(a, b)) thing2();
a) Under what circumstances does the first if statement execute thing1()? (0.4 points)
When the pointers a and b are equal -- that is, when they point to the same place.
b) Under what circumstances does the second if statement execute thing2()? (0.4 points)
When the strings pointed to by a and b are equal -- that is, when they contain identical text
# comment should be simply ignored /bin/echo a "b c" 'd e' # comment # output: a b c d e setenv gribble "- #" /bin/echo $gribble # output: - # /bin/tcsh -c "echo $gribble" # output: - # exitNote that the above does not test any of the error conditions, nor does it test how you deal with areas where the assignment was ambiguous.
a) The above linear ordering is stupid. What is the most sensible order? (0.4 points)
- surface 0, cylinder 0, sector 0
- surface 0, cylinder 0, sector 1
- surface 1, cylinder 0, sector 0
- surface 1, cylinder 0, sector 1
- surface 0, cylinder 1, sector 0
- surface 0, cylinder 1, sector 1
- surface 1, cylinder 1, sector 0
- surface 1, cylinder 1, sector 1
Hint: If the sectors are read in a sensible order, the latency (sum of rotational and head positioning latency) should be near-minimal. The sensible order should also remain sensible if you enlarged the disk to have a realistic and useful number of sectors per track, tracks per cylinder, and cylinders. Please ignore any restrictions on reading consecutive sectors on one track, or alternatively and equivalently, assume that interleaving, if required, is done in the hardware.
b) Given the constants SURFACES, CYLINDERS, and SECTORS (per track, for the latter), write a C function linearize(surface,track,cylinder) that produces a linear disk address from the indicated surface, track and cylinder numbers. (0.3 points)
There was a bug in the problem specification (strangely, nobody asked about it!). The sector number obviously matters, and track is both ambiguous and redundant in each reading of the ambiguity.
int linearize(int surface, int cylinder, int sector){ return sector + surface * SECTORS + cylinder * (SECTORS + SURFACES); }
c) Given the constants SURFACES, CYLINDERS, and SECTORS (as in the previous part), write expressions to convert the linear disk address linear to the three variables that make up a disk address, surface, cylinder, and sector. (0.3 points)
As above, there was the same bug in the problem specification.
sector = linear % SECTORS; surface = (linear / SECTORS) % SURFACES; cylinder = linear / (SECTORS * SURFACES);
There are two possible insertion rules: Given a request for disk address a, it is inserted just before the lowest item already in the queue that has an address greater than or equal to a, or it is inserted just after the highest item in the queue that has an address less than or equal to a.
A question: Which of the above is correct, and what could go wrong with the other alternative. (0.5 points)
The key question is, if the disk is currently working on a sector with linearized address a while another request for a arrives in the request queue, is that second request going to be processed immediately when the current request is completed, or will it be put off until all other pending requests have been served.
The former option leads to the potential that some ill-behaved customer (or a group of customers) could prevent any other user from getting service by repeated requests for access to one particular sector. The latter option guarantees fairness.
Therefore, a request for address a must be "inserted just before the lowest item already in the queue that has an address greater than or equal to a."