Process #0 executes “sched”. When it is not waiting for the completion of an input/output operation that it has initiated, it spends most of its time waiting in one of the following situations:
None of the processes which are swapped out is ready to run, so that there is nothing to do. The situation may be changed by a call to “wakeup”, or to “xswap” called by either “newproc” or “expand”.
There is at least one process swapped out and ready to run, but it hasn’t been out more than 3 seconds and/or none of the processes presently in main memory is inactive or has been there more than 2 seconds. The situation may be changed by the effluxion of time as measured by “clock” or by a call to “sleep”.
When either of these situations terminate:
With the processor running at priority six, so that the clock can’t interrupt and change values of “p_time”, a search is made for the process which is ready to run and has been swapped out for the longest time;
If there is no such process then situation A holds;
Search for a main memory area of adequate size to hold the data segment. If an associated text segment must be present also but is not currently in main memory, the area is increased by the size of the text segment;
If an area of adequate size is available the program branches to “found2” (2031). (Note that the program does not handle the case where there is sufficient space for both text and data segments but in distinct areas of main memory. Would it be worth while to extend the code to cover this possibility?);
Search for a process which is in main memory, but which is not the scheduler or locked (i.e. already being swapped out), and whose state is “SWAIT” or “SSTOP” (but not
“SSLEEP”) (i.e. the process is waiting for an event of low precedence, or has stopped during tracing (see Chapter Thirteen)). If such a process is found, go to line 2021, to swap the image out.
Note that there seems to be a bias here against processes whose “proc” entries are early in the “proc” array;
If the image to be swapped in has been out less than 3 seconds, then situation B holds;
Search for the process which is loaded, but is not the scheduler or locked, whose state is “SRUN” or “SSLEEP” (i.e. ready to run, or waiting for an event of high precedence) and which has been in main memory for the longest time;
If the process image to be swapped out has been in main memory for less than 2 seconds, then situation B holds.
The constant “2” here (also the “3” on line 2003) is somewhat arbitrary. For some reason the programmer has departed from his usual practice of naming such constants to emphasise their origins;
The process image is flagged as not loaded and is swapped out using “xswap” (4368).
Note that the “SSWAP” flag is not set here because the process swapped out is not the current process. (Cf. lines 1907, 2286);
Read the text segment into main memory if necessary. Note that the arguments for the “swap” procedure are:
an address within the swap area of the disk;
a main memory address (ordinal number of a 32 word block);
a size (number of 32 word blocks to be transferred);
a direction indicator (“B_READ==1” denotes “disk to main memory”);
Swap in the data segment and ...
Release the disk swap area to the available list, record the main memory address, set the “SLOAD” flag and reset the accumulated time indicator.