15.6 swap (5196)

Before plunging into all the detail of the file “bio.c”, it will be instructive as well as convenient to examine one routine which was introduced earlier, namely “swap”.

The buffer head “swbuf” was declared to control swapping input/output, which must share access to the disk with other activity. No element of “buffers” is associated with “swbuf”. Instead the core area occupied (or to be occupied) by the program serves as the data buffer.

5200:

The address of the flags in “swbuf” is transferred to the register variable “fp” for convenience and economy;

5202:

The “B_BUSY” flag is tested, and if it is on, a swap operation is already under way, so that the “B_WANTED” flag is set and the process must wait via a call on “sleep”.

Note that the code loop on lines 5202 to 5205 runs at priority level six, i.e. one higher than the disk interrupt priority.

Can you see why this is necessary? Under what conditions will the “B_BUSY” flag be set?

5206:

The flags are set to reflect:

  • “swbuf” is in use (“B_BUSY”);

  • physical i/o implying a large transfer direct to/from the user data segment
    (“B_PHYS”);

  • whether the operation is read or write. (“rdflg” is a parameter to “swap”);

5207:

The “b_dev” field is initialised. (Presumably this could have been performed once during initialisation rather than every time “swbuf” is used, i.e. in “binit”.);

5208:

“b_wcount” is initialised. Note the negative value and the effective multiplication by 32;

5210:

The hardware device controller requires a full physical address (18 bits on the PDP 11/40). The block number of a 32 word block must be converted into two parts: the low order ten bits are shifted left six places and stored as “b_addr”, and the remaining six high order bits as “b_xmem”. (On the PDP 11/40 and 11/45 only two of these bits are significant.);

5212:

A mouthful at first glance! Shift “swapdev” eight places to the right to obtain the major device number. Use the result to index “bdevsw”. From the structure thus selected, extract the strategy routine and execute it with the address of “swbuf” passed as a parameter;

5213:

Explain why this call on “spl6” is necessary;

5214:

Wait until the i/o operation is complete. Note that the first parameter to “sleep” is in effect the address of “swbuf”;

5216:

Wakeup those processes (if any) which are waiting for “swbuf”;

5218:

Reset the process or priority to zero, thus allowing any pending interrupts to “happen”;

5219:

Reset both the “B_BUSY” and “B_WANTED” flags.