windows threading colin roby jaewook kim
DESCRIPTION
Multi-Processor Computing System OS, Process, and Thread Applications Programming paradigms P Microkernel Multi-Processor Computing System Threads Interface Operating System Hardware Process Processor Thread PTRANSCRIPT
![Page 1: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/1.jpg)
Windows Threading
Colin RobyJaewook Kim
![Page 2: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/2.jpg)
P PP P P P
MicrokernelMulti-Processor Computing
System
Threads Interface
Hardware
Operating System
ProcessProcessor ThreadP
Applications
Programming paradigms
OS, Process, and Thread
![Page 3: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/3.jpg)
Legacy Window Threading Model (Co-operative Threading)
![Page 4: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/4.jpg)
Co-operative ThreadingUsed 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)
![Page 5: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/5.jpg)
Architecture for Cooperative Threading Model
![Page 6: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/6.jpg)
Advantages & Disadvantages
![Page 7: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/7.jpg)
Threading Models from Windows NT to 2003
![Page 8: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/8.jpg)
Windows NT~2003 OSPreemptive multi-processing operating system
The OS schedules the CPU timeThe application can be preempted by OS scheduler
![Page 9: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/9.jpg)
Windows ThreadThe 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)
![Page 10: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/10.jpg)
Windows Thread TypesSingle Threading
Each 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 Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/11.jpg)
Windows 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)
![Page 12: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/12.jpg)
STA & MTA
COM Object
COM Object
![Page 13: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/13.jpg)
Thread Synchronization
![Page 14: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/14.jpg)
Win32 Threading Example
![Page 15: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/15.jpg)
Creating 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);
}
![Page 16: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/16.jpg)
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);
}
![Page 17: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/17.jpg)
TerminationExitThread((DWORD) value);
return((DWORD) value);
WaitForSingleObject(thread, timeOutValue);
GetExitCodeThread(thread, &value);
CloseHandle(thread);
![Page 18: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/18.jpg)
Threading Model for Multicore System
![Page 19: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/19.jpg)
![Page 20: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/20.jpg)
Additional Slides
![Page 21: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/21.jpg)
21
Basic concepts used for CPU and resource management
Processes and Threads (1)
![Page 22: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/22.jpg)
22
Relationship between jobs, processes, threads, and fibers
Processes and Threads (2)
![Page 23: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/23.jpg)
23Some of Win32 calls for managing processes, threads and
fibers
Job, Process, Thread & Fiber Mgmt. API Calls
![Page 24: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/24.jpg)
Windows Threading
![Page 25: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/25.jpg)
One-to-one modelOne-to-one 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.
![Page 26: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/26.jpg)
Fibers vs. ThreadsFibers 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 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 27: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/27.jpg)
Stack PagesHANDLE thread;
thread = CreateThread(0, 16*1024, startroutine, arg, 0, &id);
![Page 28: Windows Threading Colin Roby Jaewook Kim](https://reader035.vdocuments.site/reader035/viewer/2022062504/5a4d1b517f8b9ab0599a7e43/html5/thumbnails/28.jpg)
Client Script Callbacks