Window Threads Chapter 7 Windows Thread Management.

Slides:



Advertisements
Similar presentations
Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Advertisements

Lesson 12: Kernel-mode Thread Sync (aka: Why I love Gentoo)
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
1 3. Controlling Robot Car by Wireless Sensor The ultimate objective of the project is to control the car motion using the steering wheel The only connection.
Project 2 教學. Synchronization Functions in Windows HANDLE WINAPI CreateSemaphore ( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount,
1 JMH Associates © 2004, All rights reserved Chapter 6 Process Management.
CS 284a, 8 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 8 October, 1997.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
ISP – 5 th Recitation Mutexes Code example. Mutex Wikipedia definition: Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent.
ISP – 6 th Recitation What’s void? Pointer to pointer/Array of pointers/Two dimensional array “Random” numbers Semaphores Code examples.
7-1 JMH Associates © 2003, All rights reserved Designing and Developing Reliable, Scaleable Multithreaded Windows Applications Chapter 10 Supplement Advanced.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
1 JMH Associates © 2004, All rights reserved Chapter 15 Asynchronous Input/Output.
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
Win32 Programming Lesson 9: Jobs & Thread Basics.
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
Windows Programming Using C# Threading. 2 Contents Threading Thread class Interlocked class Monitor class Semaphore class Thread Pools.
Multithreading 1 Multithreading (Java, C#, C++) DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY 2007, 2009 by Emil Vassev.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS4: Scheduling and Dispatch 4.2. Windows Processes.
Multi-core Programming Programming with Windows Threads.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1-1 © 2004 JMH Associates. All rights reserved. Windows Application Development Chapter 7 Windows Thread Management.
Threads and Thread Synchronization Advanced Windows Programming Series 1.
Windows thread programming
Multithreaded Programming With the Win32 API Andrew Tucker Andrew Tucker Debugger Development Lead March 13, 1998.
Multithreading GSP-420.
System calls for Process management
Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,
Programming with Windows* Threads Intel Software College.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Windows Thread Management
Practical Sockets and Threads Derek Weitzel. Windows Threads Concurrent processing Concurrent processing Windows Create Thread Windows Create Thread HANDLE.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Windows CE Portable Modular Real-time Small footprint Embedded market.
Windows Threading Colin Roby Jaewook Kim.
System calls for Process management Process creation, termination, waiting.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
Chapter 7: Semaphore Management Seulki Lee 1. Semaphore?  A protocol mechanism offered by most multitasking kernels  Control access to a shared resource.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
Tutorial 4. In this tutorial session we’ll see Threads.
Lecture Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management.
1.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 1: Introduction What Operating Systems Do √ Computer-System Organization.
操作系统原理及应用 董恺 计算机科学与工程学院、软件学院 江苏省网络与信息安全重点实验室.
Lesson One – Creating a thread
Lecture 7 : Multithread programming
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Using Processes.
Threads and Thread Synchronization
Principles of Operating Systems Lecture 8
Windows Concurrency Concepts and APIs
Threads and Locks.
This pointer, Dynamic memory allocation, Constructors and Destructor
O.S. Programming Assignment
Windows APIs File Processing Copyright © 2016 Curt Hill.
Waiting and Synchronization
Chapter 05. Multithread.
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/49.
Windows APIs Some odds and ends Copyright © 1997 – 2016 Curt Hill.
26.
Window Application Development
Presentation transcript:

Window Threads Chapter 7 Windows Thread Management

Thread Management ● Creating a Thread ● The Thread Function ● Thread Termination ● Thread Exit Codes ● Thread Identities ● Suspending and Resuming Threads

Creating a Thread ● Specify the thread’s start address within the process’ code ● Specify the stack size, and the stack consumes space within the process’ address space  The stack cannot be expanded

Creating a Thread ● Specify a pointer to an argument for the thread  Can be nearly anything  Interpreted by the thread itself ● CreateThread returns a thread’s ID value and its handle  A NULL handle value indicates failure

Creating a Thread HANDLE CreateThread ( LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack, LPTHREAD_START_ROUTINE lpStartAddr, LPVOID lpvThreadParm, DWORD dwCreate, LPDWORD lpIDThread )

Creating a Thread ● Parameters lpsa  Security attributes structure (use NULL ) cbStack  Byte size for the new thread’s stack  Use 0 to default to the primary thread’s stack size (1 MB)

Creating a Thread lpStartAddr  Points to the function (within the calling process) to be executed  Accepts a single pointer argument and returns a 32-bit DWORD exit code  The thread can interpret the argument as a DWORD or a pointer lpThreadParm  The pointer passed as the thread argument

Creating a Thread ● Threads are terminated by ExitProcess  The process and all its threads terminate  The exit code returned by the thread start function same as the process exit code  Or a thread can simply return with its exit code

The Thread Function DWORD WINAPI MyThreadFunc ( PVOID pThParam ) {... ExitThread (ExitCode); /* OR */ return ExitCode; }

Thread Terminaton ● Threads are terminated by ExitProcess  The process and all its threads terminate  The exit code returned by the thread start function same as the process exit code  Or a thread can simply return with its exit code

Thread Terminaton ● ExitThread is the preferred technique  The thread’s stack is deallocated on termination VOID ExitThread (DWORD (dwExitCode) ● When the last thread in a process terminates, so does the process itself

Thread Terminaton ● You can terminate a different thread with TerminateThread  Dangerous: The thread’s stack and other resources will not be deallocated  Better to let the thread terminate itself ● A thread will remain in the system until the last handle to it is closed (using CloseHandle )  Then the thread will be deleted ● Any other thread can retrieve the exit code

Thread Exit Codes BOOL GetExitCodeThread ( HANDLE hThread, LPDWORD lpdwExitCode ) lpdwExitCode  Contains the thread’s exit code  It could be STILL_ACTIVE

Thread Identities ● A thread has a permanent “ ThreadId ” ● A thread is usually accessed by HANDLE ● An ID can be converted to a HANDLE

Thread Identities HANDLE GetCurrentThread (VOID); DWORD GetCurrentThreadId (VOID); HANDLE OpenThread ( DWORD dwDesiredAccess, BOOL InheritableHandle, DWORD ThreadId );

Suspend and Resume Threads ● Every thread has a suspend count  A thread can execute only if this count is zero ● A thread can be created in the suspended state ● One thread can increment or decrement the suspend count of another:

Suspend and Resume Threads DWORD ResumeThread (HANDLE hThread) DWORD SuspendThread (HANDLE hThread) ● Both functions return previous suspend count ● 0xFFFFFFFF indicates failure ● Useful in preventing “race conditions”  Do not allow threads to start until initialization is complete ● Unsafe for general synchronization

Waiting for Thread Termination ● Wait for a thread to terminate using general purpose wait functions ● WaitForSingleObject or WaitForMultipleObjects  Using thread handles ● The wait functions wait for the thread handle to become signaled  Thread handle is signaled when thread terminates

Waiting for Thread Termination ● ExitThread and TerminateThread set the object to the signaled state  Releasing all other threads waiting on the object ● ExitProcess sets the process’ state and all its threads’ states to signaled

Waiting for Thread Termination DWORD WaitForSingleObject ( HANDLE hObject, DWORD dwTimeOut ) DWORD WaitForMultipleObjects ( DWORD cObjects, LPHANDLE lphObjects, BOOL fWaitAll, DWORD dwTimeOut ) ● Return: The cause of the wait completion

Wait Options ● Specify either a single handle hObject ● Or an array of cObjects referenced by lphObjects ● cObjects should not exceed MAXIMUM_WAIT_OBJECTS - 64

Wait Options ● dwTimeOut is in milliseconds  0 means the function returns immediately after testing the state of the specified objects  Use INFINITE for no timeout  Wait forever for a thread to terminate ● GetExitCodeThread  Returns the thread exit code

Wait Return Values ● fWaitAll  If TRUE, wait for all threads to terminate Possible return values are:  WAIT_OBJECT_0  The thread terminated (if calling WaitForMultipleObjects ; fWaitAll set)

Wait Return Values  WAIT_OBJECT_0 + n where 0 <= n < cObjects  Subtract WAIT_OBJECT_0 from the return value to determine which thread terminated when calling WaitForMultipleObjects with fWaitAll set  WAIT_TIMEOUT  Timeout period elapsed

Wait Return Values  WAIT_ABANDONED  Not possible with thread handles  WAIT_FAILED  Call GetLastError for thread-specific error code