windows threading

32
Windows Threading Colin Roby Jaewook Kim [CMSC 621] Advanced Operating Systems

Upload: derick

Post on 31-Jan-2016

58 views

Category:

Documents


1 download

DESCRIPTION

[CMSC 621] Advanced Operating Systems. Windows Threading. Colin Roby Jaewook Kim. Threads Interface. Microkernel. Multi-Processor Computing System. . . P. P. P. P. P. P. P. Processor. Process. Thread. OS, Process, and Thread for Windows OS. Applications. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Windows Threading

Windows ThreadingWindows Threading

Colin RobyJaewook KimColin RobyJaewook Kim

[CMSC 621] Advanced Operating Systems[CMSC 621] Advanced Operating Systems

Page 2: Windows Threading

P PP P P PMicrokernelMicrokernel

Multi-Processor Computing System

Threads InterfaceThreads Interface

Hardware

Operating System

ProcessProcessor ThreadPP

Applications

Programming paradigms

OS, Process, and Thread for Windows OSOS, Process, and Thread for Windows OS

Page 3: Windows Threading

Legacy Window Threading Model (Co-operative Threading – Windows 3.1 and 95)

Legacy Window Threading Model (Co-operative Threading – Windows 3.1 and 95)

Page 4: Windows Threading

Co-operative ThreadingCo-operative ThreadingUsed by old 16-bit Window Platform

Invented to overcome the lacking of a hardware timer

Thread continues execution untilThread terminatesThread executes an instruction causing wait (e.g., IO)Thread volunteers to stop (invoking yield or sleep)

Used by old 16-bit Window PlatformInvented to overcome the lacking of a hardware timer

Thread continues execution untilThread terminatesThread executes an instruction causing wait (e.g., IO)Thread volunteers to stop (invoking yield or sleep)

Ready

Running

Exited

Blocked

Terminate(call scheduler)

Create

Block for resource(call scheduler)

Yield(call scheduler)

Resource becomes available(move to ready queue)

Schedulerdispatch

Page 5: Windows Threading

Architecture for Cooperative Threading ModelArchitecture for Cooperative Threading Model

Use serialized message queueAll user input from keyboard & mouse are queuedNext message is not sent to program until current message is fully processed

Message based program interaction Prior to receiving message, program stays dormant in memoryMessage queue sends message to programProgram starts processing messageProgram returns control back to window

Use serialized message queueAll user input from keyboard & mouse are queuedNext message is not sent to program until current message is fully processed

Message based program interaction Prior to receiving message, program stays dormant in memoryMessage queue sends message to programProgram starts processing messageProgram returns control back to window

Page 6: Windows Threading

Advantages & DisadvantagesAdvantages & Disadvantages

AdvantageSafe 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 activeThreads depend on each other to yield control, results in performance decrease in heavily loaded systems.

AdvantageSafe 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 activeThreads depend on each other to yield control, results in performance decrease in heavily loaded systems.

Page 7: Windows Threading

Threading Models from Windows NT to 2003(Preemptive Threading)

Threading Models from Windows NT to 2003(Preemptive Threading)

Page 8: Windows Threading

Preemptive MultiprocessingPreemptive Multiprocessing

Preemptive multi-processing operating system

The OS schedules the CPU timeThe application can be preempted by OS scheduler

Preemptive multi-processing operating system

The OS schedules the CPU timeThe application can be preempted by OS scheduler

Ready

Running

Exited

Blocked

Terminate(call scheduler)

Create

Block for resource(call scheduler)

Yield, Interrupt(call scheduler)

Resource free, I/O completion interrupt(move to ready queue)

Schedulerdispatch

* Kai Li – Non-Preemptive and Preemptive Threads

Page 9: Windows Threading

Windows ThreadWindows Thread

The unit of execution (in UNIX, Process is the unit)Basically one-to-one mapping

Fiber Library for the M:M Model

Each thread containsA thread idRegister setSeparate user and kernel stacksPrivate data storage area

The register set, stacks, and private storage area are known as the context of the threadsThe primary data structures of a thread include:

ETHREAD (executive thread block)KTHREAD (kernel thread block)TEB (thread environment block)

The unit of execution (in UNIX, Process is the unit)Basically one-to-one mapping

Fiber Library for the M:M Model

Each thread containsA thread idRegister setSeparate user and kernel stacksPrivate data storage area

The register set, stacks, and private storage area are known as the context of the threadsThe primary data structures of a thread include:

ETHREAD (executive thread block)KTHREAD (kernel thread block)TEB (thread environment block)

Page 10: Windows Threading

Windows Thread TypesWindows Thread Types

Single ThreadingEach process is started with a single thread

Multiple ThreadingA thread can be created by Win32 Pthread or Windows Thread API

Hyper ThreadingSimultaneous multithreading technology on the Pentium 4 microarchitecture by IntelSupported by Windows 2000 or more

Single ThreadingEach process is started with a single thread

Multiple ThreadingA thread can be created by Win32 Pthread or Windows Thread API

Hyper ThreadingSimultaneous multithreading technology on the Pentium 4 microarchitecture by IntelSupported by Windows 2000 or more

Page 11: Windows Threading

Windows Threading ModelsWindows Threading Models

Win32 Threading ModelWin32 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)

Win32 Threading ModelWin32 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)

Page 12: Windows Threading

Win32 Threading API CallsWin32 Threading API Calls

12Some of Win32 calls for managing processes, threads and fibersSome of Win32 calls for managing processes, threads and fibers

Page 13: Windows Threading

Win32 Threading ExampleWin32 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, // argument0, // creation flags&id); // thread ID

WaitForSingleObject(thread, INFINITE);...

}

DWORD WINAPI server(void *arg) {while(TRUE)

// get and handle requestreturn(0);

}

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, // argument0, // creation flags&id); // thread ID

WaitForSingleObject(thread, INFINITE);...

}

DWORD WINAPI server(void *arg) {while(TRUE)

// get and handle requestreturn(0);

}

Page 14: Windows Threading

Win32 Threading Example cont.Win32 Threading Example cont.rlogind(int r_in, int r_out, int l_in, int l_out) {

HANDLE in_thread, out_thread;two_ints_t in={r_in, l_out}, out={l_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);

}

rlogind(int r_in, int r_out, int l_in, int l_out) {HANDLE in_thread, out_thread;two_ints_t in={r_in, l_out}, out={l_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);

}

Page 15: Windows Threading

Win32 Threading Example cont.Win32 Threading Example cont.ExitThread((DWORD) value);

return((DWORD) value);

WaitForSingleObject(thread, timeOutValue);

GetExitCodeThread(thread, &value);

CloseHandle(thread);

ExitThread((DWORD) value);

return((DWORD) value);

WaitForSingleObject(thread, timeOutValue);

GetExitCodeThread(thread, &value);

CloseHandle(thread);

Page 16: Windows Threading

COM ThreadingCOM Threading

Components don’t live on threadsAn instance is a ‘chunk’ of memory associated with an apartmentApartments determine which threads can call the componentThread switch is decided by the proxy based on apartment and threading model

Components don’t live on threadsAn instance is a ‘chunk’ of memory associated with an apartmentApartments determine which threads can call the componentThread switch is decided by the proxy based on apartment and threading model

16

Page 17: Windows Threading

COM Threading (STA vs. MTA)COM Threading (STA vs. MTA)

COM Object

COM Object

Page 18: Windows Threading

COM Threading ExampleCOM Threading Example

int main()

{

/* ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); for STA */

::CoInitializeEx(NULL, COINIT_MULTITHREADED); /* for MTA */

DisplayCurrentThreadId();

ILegacyCOMObject1Ptr spILegacyCOMObject1;

spILegacyCOMObject1.CreateInstance(__uuidof(LegacyCOMObject1));

spILegacyCOMObject1 -> TestMethod1();

::CoUninitialize();

return 0;

}

int main()

{

/* ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); for STA */

::CoInitializeEx(NULL, COINIT_MULTITHREADED); /* for MTA */

DisplayCurrentThreadId();

ILegacyCOMObject1Ptr spILegacyCOMObject1;

spILegacyCOMObject1.CreateInstance(__uuidof(LegacyCOMObject1));

spILegacyCOMObject1 -> TestMethod1();

::CoUninitialize();

return 0;

}

Page 19: Windows Threading

Threading Model for Multicore SystemThreading Model for Multicore System

Page 20: Windows Threading

Thread ManagementThread 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.

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.

Page 21: Windows Threading

Hardware Design VarianceHardware Design Variance

Two hardware thread share one coreThis 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.

Two hardware thread share one coreThis 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.

Page 22: Windows Threading

Detecting multicore cpu and hardware threadDetecting 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 existDetect the relationship between the hardware threads such as sharing data caches or sharing instructions set

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 existDetect the relationship between the hardware threads such as sharing data caches or sharing instructions set

Page 23: Windows Threading

Mechanics of Window SchedulerMechanics of Window Scheduler

Preemptive, time slicing based Using system clock to interrupt each threadEach thread is allocated a fixed amount of time - quantum

Priority Driven Highest priority ready thread always run firstHigher 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

Preemptive, time slicing based Using system clock to interrupt each threadEach thread is allocated a fixed amount of time - quantum

Priority Driven Highest priority ready thread always run firstHigher 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

Page 24: Windows Threading

Processor Preference for Window SchedulerProcessor 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 processorPick the last processor if it is idlePick the executing processor – where the current scheduling code is runningScan all the cpu from highest cpu number to lowest cpu number.

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 processorPick the last processor if it is idlePick the executing processor – where the current scheduling code is runningScan all the cpu from highest cpu number to lowest cpu number.

Page 25: Windows Threading

Additional SlidesAdditional Slides

Page 26: Windows Threading

26

Basic concepts used for CPU and resource management

Basic concepts used for CPU and resource management

Processes and Threads (1)Processes and Threads (1)

Page 27: Windows Threading

27

Relationship between jobs, processes, threads, and fibers

Relationship between jobs, processes, threads, and fibers

Processes and Threads (2)Processes and Threads (2)

Page 28: Windows Threading

Windows Threading ArchitectureWindows Threading Architecture

Page 29: Windows Threading

One-to-one Threading ModelOne-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.

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.

Page 30: Windows Threading

Fibers vs. ThreadsFibers vs. Threads

Fibers vs. ThreadsFibers 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 interfaceConvertThreadToFiber() 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.

Fibers vs. ThreadsFibers 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 interfaceConvertThreadToFiber() 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.

Page 31: Windows Threading

Thread CancellationThread Cancellation

Terminating a thread before it has finishedTwo general approaches:

Asynchronous cancellation terminates the target thread immediatelyDeferred cancellation allows the target thread to periodically check if it should be cancelled

Terminating a thread before it has finishedTwo general approaches:

Asynchronous cancellation terminates the target thread immediatelyDeferred cancellation allows the target thread to periodically check if it should be cancelled

Page 32: Windows Threading

References

1. 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. Russinovich

3. Programming Windows 95 by Charles Petzold - Microsoft Press.

References

1. 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. Russinovich

3. Programming Windows 95 by Charles Petzold - Microsoft Press.