6.9 swtch (2178)

2184:

“p” is a static variable (2180), which means that its value is initialised to zero (1566) and is preserved between calls. For the very first call on “swtch”, “p” is set to point to “proc[0]”;

2189:

“savu” is called to save the stack pointer and the environment pointer for the current process in “u.u_rsav”;

2193:

“retu” is called to reset the kernel address register for segment #6 to the value passed as an argument (this causes a change in the current process!), and to reset the stack and environment pointers to values appropriate to the revised current process, whose execution is about to be resumed.

The combination of successive calls on “savu” and “retu” at this point constitutes a so-called “coroutine jump” (Cf. “exchange jump” on the Cyber or “Load PSW” on the /360 or “Move Stack” on the B6700).

This time however the coroutine jump is from process 0 to process 0 (not very interesting!).

2201:

The set of processes is searched to find the process whose state is “SRUN” and which is loaded and for which “p_pri” is a maximum.

The search is successful and process #1 is found. (N.B. The state of process #0 was just changed from “SRUN” to “SSLEEP” in “sleep” so it no longer satisfies the search criterion);

2218:

Since “p” is not “NULL”, the idle loop is not entered;

2228:

“retu” (0740) causes a coroutine jump to process #1 which becomes the current process.

What is process #1 ? It is a copy of process #0, made at a previous stage of the latter’s existence.

This call on “retu” was not preceded by a call on “savu” because the necessary information has in fact been saved already. (Where?)

2229:

“sureg” is a routine 1738) which copies into the user mode segmentation registers, the values appropriate for the current process. These have been stored earlier in the arrays “u.u_uisa” and “u.u _uisd”.

The very first call on “sureg” copies zeroes and serves no real purpose.

2240:

The “SSWAP” flag is not set, so that this enigmatic (2239) section can be ignored for now;

2247:

Finally “swtch” returns with a value of “1”. But where does the “return” return to? Not to “sleep” !

The “return” follows values set by the stack pointer and the environment pointer. These (just before the return) have values equal to those in force when the most recent “savu(u.u_rsav)” was performed.

Now process #1, which is only just starting has never performed a “savu”, but values were stored in “u.u_rsav” before the copy of process #0 was made by “newproc”, which had been called from “main”.

Thus in this case, the return from “swtch” is made to “main”, with a value of one. (Look over this again, to be sure you understand!)