lecture 8 processes, threads, and jobs (2)

48
计计计计•计计计计计计计 Lecture 8 Processes, Threads, and Jobs (2) xlanchen@04/08/2005

Upload: phuc

Post on 10-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

Lecture 8 Processes, Threads, and Jobs (2). xlanchen@04/08/2005. Contents. The internal structures of process How to create a process The internal structures of thread How to create a thread Thread Scheduling Job Objects. Thread structures. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 8  Processes, Threads, and Jobs (2)

计算机系•信息处理实验室

Lecture 8 Processes, Threads, and Jobs (2)

xlanchen@04/08/2005

Page 2: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

2计算机系信息处理实验室

Contents

The internal structures of process

How to create a process

The internal structures of thread

How to create a thread

Thread Scheduling

Job Objects

Page 3: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

3计算机系信息处理实验室

Thread structures

a Windows 2000 thread is represented by ETHREAD block

in the system address spaceexcept TEB

How is the thread related to Csrss and Win32k.sys

Page 4: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

4计算机系信息处理实验室

Fibers vs. Threads

Fibers

"lightweight" threads

implemented in user mode in Kernel32.dll

allow an App. to schedule its own "threads" outside the priority-based scheduling mechanism of 2K

ConvertThreadToFiber, win32 function

CreateFiber

SwitchToFiber

Page 5: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

5计算机系信息处理实验室

Structure of the kernel thread block

Page 6: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

6计算机系信息处理实验室

EXPERIMENT

Displaying ETHREAD and KTHREAD Structures

Page 7: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

7计算机系信息处理实验室

Fields of the TEB

Page 8: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

8计算机系信息处理实验室

EXPERIMENT

Examining the TEB

Page 9: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

9计算机系信息处理实验室

Related Kernel Variables

PspCreateThreadNotifyRoutine

PspCreateThreadNotifyRoutineCount

PspCreateProcessNotifyRoutine

PspCreateProcessNotifyRoutineCount

PspCidTable

Page 10: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

10计算机系信息处理实验室

Related Performance Counters

Process:

Priority Base

Thread:

% Privileged Time

% Processor Time

% User Time

Context Switches/Sec

Elapsed Time

ID Process

ID Thread

Priority Base

Priority Current

Start Address

Thread State

Thread Wait Reason

Page 11: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

11计算机系信息处理实验室

Win32 Thread Functions

CreateThread

CreateRemoteThread

ExitThread

TerminateThread

GetExitCodeThread

GetThreadTimes

Get/SetThreadContext

GetThreadSelectorEntry

Page 12: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

12计算机系信息处理实验室

EXPERIMENT

Using the Kernel Debugger !thread Command

Page 13: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

13计算机系信息处理实验室

EXPERIMENT

Viewing Thread Information

Page 14: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

14计算机系信息处理实验室

Create a thread

CreateThread to create a Win32 thread

In Kernel32.dll

1. Creates a user-mode stack

2. Initializes the thread's hardware context

3. Calles NtCreateThread to create the executive thread object in the suspended state

4. Notifies the Win32 subsystem about the new thread

5. Return the thread handle and its ID to the caller

Page 15: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

15计算机系信息处理实验室

the thread starts running

KiThreadStartup

PspUserThreadStartup

LdrInitializeThunk (in Ntdll.dll)

Page 16: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

16计算机系信息处理实验室

Flow of CreateThread

Page 17: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

17计算机系信息处理实验室

2K Scheduling priority-driven, preemptive

the highest-priority runnable (ready) thread always runs,

processor affinity

quantum

the length of time a thread is allowed to run before Windows 2000 interrupts the thread to find out whether another thread at the same priority level or higher is waiting to run or whether the thread's priority needs to be reduced

Page 18: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

18计算机系信息处理实验室

EXPERIMENT

Viewing Ready Threads

Page 19: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

19计算机系信息处理实验室

EXPERIMENT

Thread-Scheduling State Changes

Page 20: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

20计算机系信息处理实验室

Thread priority levels

Page 21: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

21计算机系信息处理实验室

Kernel prioritiesWin32 vs. Windows 2000

Page 22: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

22计算机系信息处理实验室

kernel's dispatcher

dispatching occurs at DPC/dispatch level and is triggered by any of the following events

A thread becomes ready to execute

A thread leaves the running state

because its time quantum ends, it terminates, or it enters a wait state

A thread's priority changes

The processor affinity of a running thread changes.

Page 23: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

23计算机系信息处理实验室

Win32 Scheduling APIs

Suspend/ResumeThread

Suspends or resumes a paused thread from execution.

Get/SetPriorityClass

Returns or sets a process's priority class (base priority).

Get/SetThreadPriority

Returns or sets a thread's priority (relative to its process base priority).

Page 24: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

24计算机系信息处理实验室

Get/SetProcessAffinityMask

Returns or sets a process's affinity mask.

SetThreadAffinityMask

Sets a thread's affinity mask (must be a subset of the process's affinity mask) for a particular set of processors, restricting it to running on those processors.

Page 25: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

25计算机系信息处理实验室

Get/SetThreadPriorityBoost

Returns or sets the ability for Windows 2000 to boost the priority of a thread temporarily (applies only to threads in the dynamic range).

SetThreadIdealProcessor

Establishes a preferred processor for a particular thread but doesn't restrict the thread to that processor.

Get/SetProcessPriorityBoost

Returns or sets the default priority boost control state of the current process. (This function is used to set the thread priority boost control state when a thread is created.)

Page 26: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

26计算机系信息处理实验室

SwitchToThread

Yields execution for one or more quantums.

Sleep

Puts the current thread into a wait state for a specified time interval (figured in milliseconds [msec]). A zero value relinquishes the rest of the thread's quantum.

SleepEx

Causes the current thread to go into a wait state until either an I/O completion callback is completed, an APC is queued to the thread, or the specified time interval ends.

Page 27: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

27计算机系信息处理实验室

EXPERIMENT

Examining and Specifying Process and Thread Priorities

Page 28: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

28计算机系信息处理实验室

Real-Time Priorities

Increase scheduling priority privilege

2K is not a true real-time operating system

Page 29: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

29计算机系信息处理实验室

Interrupt Levels vs. Priority Levels

Page 30: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

30计算机系信息处理实验室

Thread states

Page 31: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

31计算机系信息处理实验室

Quantum

Quantum Accounting

Default: 6

Per clock interrupt: -3 ???

The clock interval is determined by hardware platform

Controlling the Quantum

Page 32: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

32计算机系信息处理实验室

EXPERIMENT

Determining the Clock Interval Frequency

Page 33: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

33计算机系信息处理实验室

Scheduling Data Structures

Page 34: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

34计算机系信息处理实验室

Scheduling Scenarios

Voluntary Switch

Preemption

Quantum End

Termination

Page 35: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

35计算机系信息处理实验室

Voluntary switching

Page 36: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

36计算机系信息处理实验室

Preemptive thread scheduling

Page 37: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

37计算机系信息处理实验室

Quantum End

Page 38: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

38计算机系信息处理实验室

Context Switching

A thread's context and the procedure for context switching vary depending on the processor's architecture

A typical context switch requires saving and reloading the following data:

Program counter

Processor status register

Other register contents

User and kernel stack pointers

A pointer to the address space in which the thread runs (the process's page table directory)

Page 39: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

39计算机系信息处理实验室

Idle Thread Dispatched when no runnable thread exists on a CPU

Each CPU is allotted one idle thread

the basic flow

Enables and disables interrupts (allowing any pending interrupts to be delivered).

Checks whether any DPCs are pending on the processor. If DPCs are pending, clears the pending software interrupt and delivers them.

Checks whether a thread has been selected to run next on the processor, and if so, dispatches that thread.

Calls the HAL processor idle routine (in case any power management functions need to be performed).

Page 40: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

40计算机系信息处理实验室

Priority Boosts

In five cases, Windows 2000 can boost (increase) the current priority value of threads

On completion of I/O operations

After waiting on executive events or semaphores

After threads in the foreground process complete a wait operation

When GUI threads wake up because of windowing activity

When a thread that's ready to run hasn't been running for some time (CPU starvation)

Page 41: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

41计算机系信息处理实验室

Priority boosting and decay

Page 42: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

42计算机系信息处理实验室

EXPERIMENT

Watching Foreground Priority Boosts and Decays

Page 43: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

43计算机系信息处理实验室

EXPERIMENT

Watching Priority Boosts on GUI Threads

Page 44: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

44计算机系信息处理实验室

EXPERIMENT

Watching Priority Boosts for CPU Starvation

Page 45: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

45计算机系信息处理实验室

Scheduling on SMP Systems

Affinity

Ideal and Last Processor

Choosing a Processor for a Ready Thread

Selecting a Thread to Run on a Specific CPU

When the Highest-Priority Ready Threads Are Not Running ?

Page 46: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

46计算机系信息处理实验室

Job Objects

A job object is a nameable, securable, shareable kernel object that allows control of one or more processes as a group.

basic function

to allow groups of processes to be managed and manipulated as a unit

Page 47: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

47计算机系信息处理实验室

A process can be a member of only one job object

Win32 API Functions for Jobs

CreateJobObject

OpenJobObject

AssignProcessToJobObject

TerminateJobObject

SetInformationJobObject

QueryInformationJobObject

Page 48: Lecture 8  Processes, Threads, and Jobs (2)

xlanchen@04/08/2005 Understanding the Inside of Windows2000

48计算机系信息处理实验室

EXPERIMENT

Viewing the Job Object