Title: Windows Threading
1Windows Threading
CMSC 621 Advanced Operating Systems
Colin Roby Jaewook Kim
2OS, Process, and Thread for Windows OS
Applications
Programming paradigms
Operating System
Hardware
3Legacy Window Threading Model (Co-operative
Threading Windows 3.1 and 95)
4Co-operative Threading
- Used by old 16-bit Window Platform
- Invented to overcome the lacking of a hardware
timer - Thread continues execution until
- Thread terminates
- Thread executes an instruction causing wait
(e.g., IO) - Thread volunteers to stop (invoking yield or
sleep)
5Architecture for Cooperative Threading Model
- Use serialized message queue
- All user input from keyboard mouse are queued
- Next message is not sent to program until current
message is fully processed - Message based program interaction
- Prior to receiving message, program stays dormant
in memory - Message queue sends message to program
- Program starts processing message
- Program returns control back to window
6Advantages Disadvantages
- Advantage
- Safe and easy to use.
- No need to worry about other threads changing
shared variables due to its exclusive nature - Disadvantage
- Only one thread can be active
- Threads depend on each other to yield control,
results in performance decrease in heavily loaded
systems.
7Threading Models from Windows NT to
2003(Preemptive Threading)
8Preemptive Multiprocessing
- Preemptive multi-processing operating system
- The OS schedules the CPU time
- The application can be preempted by OS scheduler
Terminate (call scheduler)
Exited
Running
Scheduler dispatch
Block for resource (call scheduler)
Yield, Interrupt (call scheduler)
Create
Ready
Blocked
Resource free, I/O completion interrupt (move to
ready queue)
Kai Li Non-Preemptive and Preemptive Threads
9Windows Thread
- The unit of execution (in UNIX, Process is the
unit) - Basically one-to-one mapping
- Fiber Library for the MM Model
- Each thread contains
- A thread id
- Register set
- Separate user and kernel stacks
- Private data storage area
- The register set, stacks, and private storage
area are known as the context of the threads - The primary data structures of a thread include
- ETHREAD (executive thread block)
- KTHREAD (kernel thread block)
- TEB (thread environment block)
10Windows Thread Types
- Single Threading
- Each process is started with a single thread
- Multiple Threading
- A thread can be created by Win32 Pthread or
Windows Thread API - Hyper Threading
- Simultaneous multithreading technology on the
Pentium 4 microarchitecture by Intel - Supported by Windows 2000 or more
11Windows Threading Models
- Win32 Threading Model
- Win32 Pthread or Windows Thread API
- COM (Component Object Model) Threading Model
- Single Threaded Apartments (STA)
- Multi Threaded Apartments (MTA)
- Both Threading Model (STA or MTA)
12Win32 Threading API Calls
- Some of Win32 calls for managing processes,
threads and fibers
13Win32 Threading Example
- start_servers( )
- HANDLE thread DWORD id
- thread CreateThread(0, // security attributes
- 0, // default of stack pages allocated
- (LPTHREAD_START_ROUTINE) server, // start
routine - (LPVOID)0, // argument
- 0, // creation flags
- id) // thread ID
- WaitForSingleObject(thread, INFINITE)
- ...
-
- DWORD WINAPI server(void arg)
- while(TRUE)
- // get and handle request
- return(0)
14Win32 Threading Example cont.
- rlogind(int r_in, int r_out, int l_in, int l_out)
- HANDLE in_thread, out_thread
- two_ints_t inr_in, l_out, outl_in, r_out
- in_thread CreateThread(0, 0, incoming, in, 0,
id) - out_thread CreateThread(0, 0, outgoing, out,
0, id) - WaitForSingleObject(in_thread, INFINITE)
- CloseHandle(in_thread)
- WaitForSingleObject(out_thread, INFINITE)
- CloseHandle(out_thread)
15Win32 Threading Example cont.
- ExitThread((DWORD) value)
- return((DWORD) value)
- WaitForSingleObject(thread, timeOutValue)
- GetExitCodeThread(thread, value)
- CloseHandle(thread)
16COM Threading
- Components dont live on threads
- An instance is a chunk of memory associated
with an apartment - Apartments determine which threads can call the
component - Thread switch is decided by the proxy based on
apartment and threading model
17COM Threading (STA vs. MTA)
COM Object
COM Object
18COM Threading Example
- int main()
-
- / CoInitializeEx(NULL, COINIT_APARTMENTTHREAD
ED) for STA / - CoInitializeEx(NULL, COINIT_MULTITHREADED)
/ for MTA / - DisplayCurrentThreadId()
- ILegacyCOMObject1Ptr spILegacyCOMObject1
- spILegacyCOMObject1.CreateInstance(__uuidof(Le
gacyCOMObject1)) - spILegacyCOMObject1 -gt TestMethod1()
- CoUninitialize()
- return 0
-
19Threading Model for Multicore System
20Thread Management
- Program actively assigns software thread to
hardware thread. - Assign thread strongly suggests which hardware
thread should the software thread run on - Program passively relies on window scheduler to
assign software thread to hardware thread - Efficiency of the threading is dependent upon the
scheduling algorithm.
21Hardware Design Variance
- Two hardware thread share one core
- This is known as simultaneous multi-threading
(aka Hyper-Threading) - Multiple cores within the same cpu, one or more
hardware thread on each core - Existing architecture includes dual-core, quad
core.
22Detecting multicore cpu and hardware thread
- Window relied on threading packages provided by
processor manufactures to detect the number of
cpu cores and available hardware - Detect the cpu core topology how many real
hardware threads exist - Detect the relationship between the hardware
threads such as sharing data caches or sharing
instructions set
23Mechanics of Window Scheduler
- Preemptive, time slicing based
- Using system clock to interrupt each thread
- Each thread is allocated a fixed amount of time -
quantum - Priority Driven
- Highest priority ready thread always run first
- Higher priority thread will interrupt lower
priority thread before its time slicing is used
up, or even before it starts its quantum - Manages processor affinity
- Assign a thread to a particular processor
24Processor Preference for Window Scheduler
- Each thread maintains two CPU numbers stored in
the kernel thread block - Ideal processor the preferred processor the
thread should run on (often specified by
programmer) - Last processor the processor on which the
thread last ran - Scheduler processor assignment preference
- If the ideal processor is idle, pick the ideal
processor - Pick the last processor if it is idle
- Pick the executing processor where the current
scheduling code is running - Scan all the cpu from highest cpu number to
lowest cpu number.
25Additional Slides
26Processes and Threads (1)
- Basic concepts used for CPU and resource
management
27Processes and Threads (2)
- Relationship between jobs, processes, threads,
and fibers
28Windows Threading Architecture
29One-to-one Threading Model
- A process in Windows XP is inert it executes
nothing - A process simply owns a 4GB address space that
contains code and data for an application. - In addition, a process owns other resources, such
as files, memory allocations, and threads. - Every process in Windows XP has a primary thread.
- Threads in Windows XP are kernel-level threads.
- Per-thread data structures
- Total user/kernel time, kernel stack,
thread-scheduling info., - Thread-local storage array, thread environment
block (TEB), - List of objects thread is waiting on,
synchronization info. Etc.
30Fibers vs. Threads
- Fibers vs. Threads
- Fibers are often called lightweight threads.
- They allow an application to schedule its own
threads of execution. - Fibers are invisible to the kernel.
- They are implemented in user-mode in Kernel32.dll
- Fibers interface
- ConvertThreadToFiber() converts a thread to a
running fiber. - A new fiber can be created using CreateFiber().
- The new fiber runs until it exits or until it
calls SwitchToFiber(). - Fibers provide a functionality of the
many-to-many model.
31Thread Cancellation
- Terminating a thread before it has finished
- Two general approaches
- Asynchronous cancellation terminates the target
thread immediately - Deferred cancellation allows the target thread to
periodically check if it should be cancelled
32References1. Detecting Multi-Core Processor
Topology in an IA-32 Platform by Khang Nguyen and
shiHjon Kuo (Intel software network)2. Inside
Microsoft Windows 2000 by David A Solomon and
Mark E. Russinovich3. Programming Windows 95 by
Charles Petzold - Microsoft Press.