Before I begin, a brief bit of technical background:
A while back, Scott Hanselman posted two sets of questions that he feels are
useful when interviewing:
ASP.NET Interview Questions
What Great .NET Developers Ought to Know
As Scott says, "knowing groking these questions may not make
you a good or bad developer, but it WILL save you time when problems
arise. " Even if they weren't in some ways instrumental to your
development path if you are a .NET developer, the knowledge will still come in
handy.
And now a brief bit of *other* background:
My grandmother is part of the readership of this blog(*),but is not necessarily
all that technically minded. She is a wizard at using her DVD player, and
I have no doubt that she could pick up any topic if she so chose since she is a
brilliant woman; however, she is not a programmer, and thus a discussion of
processes vs. threads would fall on deaf ears. And what if my grandmother
wanted to work at Corillian one day?
Would she be able to get through a Scott Hanselman interview?
That being said, this is the first of a series for my grandmother:
Hanselman Question #01: What is the difference between a thread and a process?
Let’s pretend that we have a nasty gang called the Narayans (no relation to any living beings...honestly),
who have overrun the Rockarts neighborhood and are now slowly converting the
homes of the Rockarts neighborhood into drug factories. Having taken over the neighborhood, they also
have free access to most of the banks, grocery stores, and other local businesses in
the neighborhood.
Each member of the Narayan gang wears the same crusted-over hoodie as a gang
symbol. To tell each member apart, they
have a name and number on the back of their hoodie.
The police don’t really care about the Rockarts
neighborhood
all that much - in fact, they never really liked the Rockarts
neighborhood in the first place!! Thus, they don’t care if it
gets run over by the Narayans. However, to protect the city from gang
warfare, they’ve erected a barrier between the Rockarts neighborhood and the
next one over, so as to prevent the Narayans and the rival gang, the Fedirkos
from having a shoot out.
Now, the Narayans are a pretty organized gang; they have a
detailed report that describes the Rockarts neighborhood and details the plan
to convert all the homes into drug dens.
It also has a tally of all of the stores that the Narayans now run, as
well as information on every outsider in contact with the Narayan gang and
their varying levels of access to different areas of the neighborhood.
You might think being part of the Narayans gives some decent
benefits (except for wearing that dirty hoodie) and you’d be right. Each member gets a storage locker in the
neighborhood, and a blackberry that automatically updates with the state of the
city as a whole. Each member operates
either overtly or undercover depending on the situation. Overtly members carry a rocket launcher,
while undercover members carry a pen knife.
Now, onto processes and threads.
"Windows Internals" (an excellent book that every developer should read at some point) describes a process as the virtual address space and control
information necessary for the execution of a set of thread objects, but you
don’t care about stuffy definitions in awesome thought-provoking books! You care about the Narayans!! So let’s rewrite this definition in terms of
the gang.
Process: the neighborhood limits (virtual address space) and
detailed report (control information) necessary for individual gang members to
operate (execution of a set of thread objects).
"Windows Internals" describes a process as containing:
- A private virtual address
space
- An executable program that is
mapped into the process' virtual address space
- A list of open handles to
system resources that are accessible to all threads in the process
- An access token (a security
context that identifies the user, security groups, and privileges
associated with the process)
- A process ID (unique identifier)
Likewise,
in our analogy, a process contains:
- The neighborhood, bounded by its limits (A
private virtual address space)
- The drug den conversion
operation (An executable program that is mapped into the process' virtual
address space)
- The tally of all the stores
the Narayans now run (A list of open handles to system resources that are
accessible to all threads in the process)
- Information on the outsiders0
[An access token (a security context that identifies the user, security
groups, and privileges associated with the process)]
- The stain-encrusted hoodie [A
process ID (unique identifier)]
- At least one gang member (At
least one thread of execution) – after all, how do you have a drug den
conversion operation without anyone working towards it?
Now, a thread, on the other
hand, is described as being an entity *within* a process
that Windows schedules for execution.
Thread: An entity within a process that Windows schedules for execution
(in our case, a Narayan gang member).
A Windows thread includes the
following essential components:
- The contents of a set of CPU
registers representing the state of the processor.
- Two stacks, one for executing
in kernel mode and one for executing in user mode
- A private storage area called
thread-localed storage for use by subsystems, run-time libraries and DLLs.
- A unique identifier called a
thread ID.
In our case, reworking this to describe a Narayan gang
member:
- The Blackberry with the city
status [The CPU register contents]
- The grenade launcher and the
pen knife [Two stacks, one for executing in kernel mode and one for
executing in user mode]
- The storage locker [A private
storage area called thread-localed storage for use by subsystems, run-time
libraries and DLLs]
- The name and number on the
back of the hoodie [A unique identifier called a thread ID]
So,
a process is actually a larger, more encompassing set of information, of which
a thread (or Narayan gang member) is only a part.
Threads can only operate within their process and on the space
available to their process - they cannot operate on memory that is
exclusive to another process (hence, why the Narayans can't operate in
the Fedirko gang territory).
Don’t
worry, Granny! One day we’ll get you to
that Corillian interview!
(*) My grandmother does not have a computer;
however, she reads this blog by my printing out the entries and passing them
along. I haven’t asked her opinion of
the MSDN conference but I know she agrees that Biztalk needed better representation!
Other entries in the "My grandmother and" series: