Windows Threading

Post on 03-Jan-2016

51 views 0 download

Tags:

description

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. Applications. Programming paradigms. Operating System. Hardware. - PowerPoint PPT Presentation

transcript

Windows ThreadingWindows Threading

Colin RobyJaewook KimColin RobyJaewook Kim

P PP P P PMicrokernelMicrokernel

Multi-Processor Computing System

Threads InterfaceThreads Interface

Hardware

Operating System

ProcessProcessor ThreadPP

Applications

Programming paradigms

OS, Process, and ThreadOS, Process, and Thread

Legacy Window Threading Model (Co-operative Threading)

Legacy Window Threading Model (Co-operative Threading)

Co-operative ThreadingCo-operative Threading

Used by Old 16-bit Window PlatformThread continue execution until

Thread terminatesExecutes instruction causing wait (e.g., IO)Thread volunteering to stop (invoking yield or sleep)

Used by Old 16-bit Window PlatformThread continue execution until

Thread terminatesExecutes instruction causing wait (e.g., IO)Thread volunteering to stop (invoking yield or sleep)

Architecture for Cooperative Threading Model

Architecture for Cooperative Threading Model

Advantages & DisadvantagesAdvantages & Disadvantages

Threading Models from Windows NT to 2003Threading Models from Windows NT to 2003

Windows NT~2003 OSWindows NT~2003 OS

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

Windows ThreadWindows Thread

The unit of execution (in UNIX, Process is the unit)Implements the one-to-one mappingEach thread contains

A 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)Implements the one-to-one mappingEach thread contains

A 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)

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

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)

STA & MTASTA & MTA

COM Object

COM Object

Thread SynchronizationThread Synchronization

Win32 Threading ExampleWin32 Threading Example

Creating a ThreadCreating a Threadstart_servers( ) {

HANDLE thread; DWORD id; int i;for (i=0; i<nr_of_server_threads; i++)

thread = CreateThread(0, // security attributes0, // default # of stack pages allocated(LPTHREAD_START_ROUTINE) server, // start

routine(LPVOID)0, // argument0, // creation flags&id); // thread ID

...}

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

// get and handle requestreturn(0);

}

start_servers( ) {HANDLE thread; DWORD id; int i;for (i=0; i<nr_of_server_threads; i++)

thread = CreateThread(0, // security attributes0, // default # of stack pages allocated(LPTHREAD_START_ROUTINE) server, // start

routine(LPVOID)0, // argument0, // creation flags&id); // thread ID

...}

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

// get and handle requestreturn(0);

}

When is it done?When is it done?

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);

}

TerminationTermination

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);

Threading Model for Multicore SystemThreading Model for Multicore System

Additional SlidesAdditional Slides

21

Basic concepts used for CPU and resource management

Basic concepts used for CPU and resource management

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

22

Relationship between jobs, processes, threads, and fibers

Relationship between jobs, processes, threads, and fibers

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

23Some of Win32 calls for managing processes, threads and

fibersSome of Win32 calls for managing processes, threads and

fibers

Job, Process, Thread & Fiber Mgmt. API CallsJob, Process, Thread & Fiber Mgmt. API Calls

Windows ThreadingWindows Threading

One-to-one modelOne-to-one model

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

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

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.

Stack PagesStack Pages

HANDLE thread;

thread = CreateThread(0, 16*1024, startroutine, arg, 0, &id);

HANDLE thread;

thread = CreateThread(0, 16*1024, startroutine, arg, 0, &id);

Client Script CallbacksClient Script Callbacks