Processes, Threads, and Jobs
Processes, Threads, and Jobs
Processes, Threads, and Jobs
Process Internals
• Data Structures
• Kernel Variables
• Performance Counters
• Relevant Functions
• Flow of CreateProcess
Data Structures
•Contains an executive process
block. (EPROCESS)
What kind of
application is it?
POSIX
Run Posix.exe
Flow of CreateProcess
2. Creating the windows executive process object
Has a valid windows exe file and a section object.
1. Setting up the EPROCESS block
1. Allocate and initialize the windows EPROCESS block
2. Inherit the process affinity mask from the parent process.
3. Set working set size to PsMinimumWorkingSet and
PsMaximumWorkingSet.
4. Set the new process’s quota block to the address of its parent
process’s quota block, and increment the reference count for the
parent’s quota block.
5. Inherit the windows device name space.
6. Store the parent process’s ID in the InheritedFromUniqueProcessId
field in the new process object.
7. Create the process’s primary access token.
8. The process table handle is initialized.
9. Set the new process’s exit status to STATUS_PENDING.
Flow of CreateProcess
2. Creating the initial process address space.
Has a valid windows exe file and a section object.
1. Setting up the EPROCESS block
1. Allocate and initialize the windows EPROCESS block
2. Inherit the process affinity mask from the parent process.
3. Set working set size to PsMinimumWorkingSet and
PsMaximumWorkingSet.
4. Set the new process’s quota block to the address of its parent
process’s quota block, and increment the reference count for the
parent’s quota block.
5. Inherit the windows device name space.
6. Store the parent process’s ID in the InheritedFromUniqueProcessId
field in the new process object.
7. Create the process’s primary access token.
8. The process table handle is initialized.
9. Set the new process’s exit status to STATUS_PENDING.
Flow of CreateProcess
2. Creating the initial process address space.
1. Three pages are created:
1. Page directory
2. Hyperspace page
3. Working set list
2. Mapping of initial pages. Them no. of pages is deducted from the
kernel variable MmTotalCommittedPages and added to
MmProcessCommit.
3. PsMinimumWorkingSet size is deducted from the
MmResidentAvailablePages.
4. Page table pages for the nonpaged portion of system space and
system cache are mapped into the process.
Thread States
• The thread states are as follows:
• Ready :A thread in the ready state is waiting to execute. When looking for a
thread to execute, the dispatcher considers only the pool of threads in the ready
state.
• Standby A thread in the standby state has been selected to run next on a
particular processor. When the correct conditions exist, the dispatcher performs a
context switch to this thread. Only one thread can be in the standby state for
each processor on the system. Note that a thread can be preempted out of the
standby state before it ever executes (if, for example, a higher priority thread
becomes runnable before the standby thread begins execution).
• Running Once the dispatcher performs a context switch to a thread, the thread
enters the running state and executes. The thread's execution continues until its
quantum ends (and another thread at the same priority is ready to run), it is
preempted by a higher priority thread, it terminates, it yields execution, or it
voluntarily enters the wait state.
• Waiting A thread can enter the wait state in several ways: a thread can
voluntarily wait for an object to synchronize its execution, the operating system
can wait on the thread's behalf (such as to resolve a paging I/O), or an
environment subsystem can direct the thread to suspend itself. When the thread's
wait ends, depending on the priority, the thread either begins running
immediately or is moved back to the ready state.
Thread States
• Transition A thread enters the transition state if it is ready for execution but its
kernel stack is paged out of memory. Once its kernel stack is brought back into
memory, the thread enters the ready state.
• Terminated When a thread finishes executing, it enters the terminated state.
Once the thread is terminated, the executive thread block (the data structure in
nonpaged pool that describes the thread) might or might not be deallocated. (The
object manager sets policy regarding when to delete the object.)
• Initialized This state is used internally while a thread is being created.
Dispatcher Database
Dispatcher Database
• Variable – KiReadySummary
• Bitmask (32 bits)
• Bitmask of priority levels that have one or more ready threads
• Variable – KiDispatcherReadyListHead
• Array of 32 list entries
• List heads for the 32 ready queues
Quantum
• A quantum is the amount of time a thread gets to run before
Windows checks to see whether another thread at the same priority
is waiting to run. If a thread completes its quantum and there are no
other threads at its priority, Windows permits the thread to run for
another quantum.
• On Windows 2000 Professional and Windows XP, threads run by
default for 2 clock intervals;
• on Windows Server systems, by default, a thread runs for 12 clock
intervals.
• Quantum Accounting
• Quantum values are stored as multiple of three times
the number of clock ticks. i.e. 6 or 12.
• This is to allow for partial quantum decay on wait
completion. This is done for all threads with priority
less than 14 for above 14 before doing the reduction
the quantum is reset to full turn.
Quantum
• Controlling the Quantum
• Two settings of quantum
• Short – 2 clock ticks – windows client
• Long – 12 clock ticks – windows server
• The balance set manager doesn't actually scan all ready threads every time it runs.
To minimize the CPU time it uses, it scans only 16 ready threads; if there are
more threads at that priority level, it remembers where it left off and picks up
again on the next pass. Also, it will boost only 10 threads per pass—if it finds 10
threads meriting this particular boost (which would indicate an unusually busy
system), it stops the scan at that point and picks up again on the next pass.
Job Objects
• A job object is a nameable, securable, shareable kernel
object that allows control of one or more process as a
group.
• It allows groups of processes to be managed and
manipulated as a unit.
• a process can be a member of only one job object.
• By default, its association with the job object cant be
broken and all processes created by the process and its
descendents are associated with the same job object as
well.
• Records basic accounting information for all processes
associated with the job and for all processes that were
associated with the job but have since terminated.