Oh, the things you can schedule,
the games you can play!
If you keep a clear head, you’ll
be well on your way.
You start with a blank sheet,
three nuts and a bolt,
a strong sense of fairness,
a large can of Jolt.
And you try to imagine,
as best as you’re able
a rulerless kingdom
that won’t grow unstable.
Responsiveness would be
a good Prime Directive.
Defer to the needs
of the greater collective.
For proper perspective,
let’s widen the frame:
Yes, the slicing of time
IS a zero-sum game.
So you start a committee,
instead of a war;
Agree to take turns
when assuming the floor.
Should you find yourself idling
and twiddling those thumbs,
take a break and defer
to your runnable chums.
But don’t squander your quantum,
because, if you do
you’ll be sent to the back
of the runnable queue.
Your cycles are precious,
and once they are spent
you must wake the next worker,
wait on your Event.
To hand over, shout
“SignalObjectAndWait()“.
You’ll go cold, but don’t worry:
you reincarnate.
And sure as can be
– if in doubt, ask Paul Randal –
the sleeper awakes
when you signal her handle.
She waited so long
on her kernel event,
now gets on with “DROP TABLE”
or “sp_AskBrent“.
We take it on faith
that her thread will resume,
on the premise there’s no
other game in the room.
But the CPU isn’t
exclusively yours –
a system must breathe
and a system has chores.
There are interrupts, APCs,
housekeeping stuff,
the network, the disk,
and if that’s not enough
there may be other apps;
that’s the curse which allows her
to wrestle for time
with a scurrilous browser.
Ideally, however,
you won’t have this war on –
the sysadmin surely
can’t be such a moron?
Our game works quite well:
it’s fair and it’s pretty.
But some have agendas
outside the committee.
Though rather irregular,
sometimes you need
to abandon the scheduler
ere you proceed
into places one should
only venture alone,
lest Medusa turn ALL
of your friends into stone.
Take xp_cmdshell,
committee of one,
the powerful, petulant,
prodigal son.
Packs his bags,
singing “PREEMPTIVE_OS_PIPEOPS“.
He won’t say where he goes
You can’t tell where he stops.
But he picks a successor
as he steps out the gate.
Though he calls SignalObject(),
he refuses to wait.
His sister awakens,
chairperson du jour,
and the merry-go-round
just goes on as before
while our preemptive pal
lives on alms and on dribblings
and acts as a tax
on his scheduler siblings.
Dark deeds done, he returns
from the dank and the cold.
And gibbering, slithers back
into the fold.
He will signal no sibling.
Just one last thing to do:
he’ll add himself onto
the runnable queue.
As he sinks into sleep
there’s a smile with the snore:
In the fullness of time
he’ll get signalled once more.
Further study
Should you wish to proceed,
you have options. Don’t panic!
Spend some time in the classroom
with Adam Machanic,
Kehayias, Klaus, Kline,
Adkin, Shaw, Ward and Dorr,
Delaney, Rastogi,
Niederstadt… wait, there’s more…
Fernandez, White, Davis,
oracular @kevin.
Read Tejani
in Henderson (2007).
Go forth! Read some code!
Count those ticks, get obsessed.
For the folks with the fire
are the ones truly blessed.
3 thoughts on “The Thread in the Head: Dr Sqlos explains context switching”