Win32 Programming Lesson 9: Jobs & Thread Basics.

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

Chapter 3 Process Description and Control
Win32 Programming Lesson 8: Processes. Where are we?  We’re starting to have some foundational understanding of Windows  But really, we don’t know how.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Race Conditions Critical Sections Deker’s Algorithm.
Processes CSCI 444/544 Operating Systems Fall 2008.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
Introduction to Processes CS Intoduction to Operating Systems.
Win32 Programming Lesson 16: Virtual Memory. Where are we?  We’ve covered the theory of Windows memory, and poked around some  Now let’s use how to.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Win32 Programming Lesson 10: Thread Scheduling and Priorities.
Win32 Programming Lesson 22: DLL Magic Part Deux All your base are belong to us…
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 2 Foundations and Definitions Processes/Threads.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 Confidential Enterprise Solutions Group Process and Threads.
Win32 Programming Lesson 18: More Memory Mapped Files and the HEAP (Finally, cool stuff!)
Process by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
Win32 Programming Lesson 8a: Jobs. Where are we?  Grouping processes into logical groups: Jobs  However, processes don’t retain a parent- child relationship.
Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,
Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Win32 Programming Lesson 17: Memory Mapped Files (Finally, cool stuff again, all this work is getting tedious!)
Win32 Programming Lesson 11: User-mode Thread Sync (aka: How to crash your machine without really trying…)
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Lecture 20 Threads Richard Gesick. Threads Makes use of multiple processors/cores if available Share memory within the program (as opposed to processes.
Windows Threading Colin Roby Jaewook Kim.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Window Threads Chapter 7 Windows Thread Management.
Tutorial 2: Homework 1 and Project 1
Threads Overview Benefits, User and Kernel Threads.
Threads & Multithreading
Processes and threads.
Process concept.
Lesson One – Creating a thread
Day 12 Threads.
Intro to Processes CSSE 332 Operating Systems
Chapter 4: Threads.
Processes in Unix, Linux, and Windows
Processor Management Damian Gordon.
More examples How many processes does this piece of code create?
Processes in Unix, Linux, and Windows
System Structure and Process Model
Unix System Calls and Posix Threads
Process Description and Control
Chapter 05. Multithread.
Lecture Topics: 11/1 General Operating System Concepts Processes
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
Processes in Unix, Linux, and Windows
CS510 Operating System Foundations
Process.
Foundations and Definitions
Processor Management Damian Gordon.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Win32 Programming Lesson 9: Jobs & Thread Basics

Where are we?  We’ve got processes sort of worked out…  But every process must have a thread associated with it  This lesson, we’ll work on threads – understanding threads is critical to understanding Windows programming  Also, a little bit on jobs…

What is a Job  A collection of processes  For example, imagine interrupting the build in Visual Studio  Windows 2000 offered a new kernel object to allow this

Basic Idea  Create a Job Kernel Object CreateJobObject  Place restrictions on the job object SetInformationJobObject  Start up some processes… CreateProcess But, set CREATE_SUSPENDED flag Assign processes to job Start the primary thread of each process

Termination  You can stop all processes in a job simply by terminating the Job Object TerminateJobObject(HANDLE hJob, UInt uExitCode)

Threads  Each thread is made up of two objects A kernel object used by the Operating System to manage the thread A thread stack that maintains local variables and function parameters as the thread executes

Threads v. Processes  Processes take up a lot more system resources than threads  Processes are inert – they are simply a container for one or more threads  Always solve a problem by adding threads not processes if you possibly can!

When to Create Threads  Many applications only have one thread  The process terminates when the primary thread finishes  However, processes can have as many threads are you like… and there’s no reason for the CPU to be idle (unless you’re on a laptop!) Example: Web browsers have separate threads for IO so the UI remains responsive

When Not to Create Threads  Some things really do need to happen in sequence Word processor, with its own thread for printing… why not? UI’s – have one GetMessage loop, with multiple worker threads  Moral: Don’t use multiple threads just because you can

Creating Threads  Once you’ve got one thread running (i.e. you’ve executed your program) you can start more  See MSDN for an example applicationMSDN  Based upon a thread function which gets called, of form: DWORD WINAPI ThreadFunc(PVOID pvParam)

Caveat Emptor  Use Local variables in threads wherever you can – static and global variables can be modified at any time by other threads, leading to all sorts of interesting race conditions  Your thread function must return a DWORD value

CreateThread  HANDLE CreateThread( PSECURITY_ATTRIBUTES psa, DWORD cbStack, PTHREAD_START_ROUTINE pfnStartA ddr, PVOID pvParam, DWORD fdwCreate, PDWORD pdwThreadID);

Parms  psa: Security attributes – usually NULL if you want the default  cbStack: The amount of stack space to reserve; 0 gets you the default  pfnStartAddr: Pointer to the function to call to start the thread  pvParam: Pointer to any parameters you wish to pass

Bugs  DWORD WINAPI FirstThread(PVOID pvParam) { // Initialize a stack-based variable int x = 0; DWORD dwThreadID; // Create a new thread. HANDLE hThread = CreateThread(NULL, 0, SecondThread, (PVOID) &x, 0, &d wThreadId); // We don't reference the new thread anymore, // so close our handle to it. CloseHandle(hThread); // Our thread is done. // BUG: our stack will be destroyed, but // SecondThread might try to access it. return(0); } DWORD WINAPI SecondThread(PVOID pvParam) { // Do some lengthy processing here. // Attempt to access the variable on FirstThread's stack. // NOTE: This may cause an access violation _ it depends on timing! * ((int *) pvParam) = 5; return(0); }

How to Solve this Problem  Hokey: create a static variable so the memory is allocated away from the thread stack  Better: use proper thread synchronization techniques – but that’s another story

Parms (cntd)  fdwCreate: 0 (get on with it) and CREATE_SUSPENDED (create it paused). See JobLab example for how to use this flag  pdwThreadID: the address of a DWORD in which CreateThread stores the ID assigned to the new thread

Terminating a Thread  Four ways: The thread function returns (good) The thread kills itself by calling ExitThread (bad) Another thread calls TerminateThread (bad) The process containing the thread terminates (bad)  Can use function to get exit code: BOOL GetExitCodeThread( HANDLE hThread, PDWORD pdwExitCode);

Thread Startup

Thread Context  Each thread has its own set of CPU registers  Saved in a CONTEXT structure contained in the thread’s kernel object  IP and SP are the most important  BaseThreadStart is called internally by the OS

BaseThreadStart  Sets up default SEH  System calls the function pointed to in CreateThread, passing in pvParam  On Thread exit, return return code  If an exception is caught, handle it; this involves terminating the entire process not just the offending thread

C/C++ Considerations  Beware: You’re reading about how the OS handles threads. Missing logical “sugar” when considering the C/C++ RTLs  Read the MSDN section on threads – it’s very useful!  Also: _beginthreadex

A Sense of Self  Threads can learn about themselves via: HANDLE GetCurrentProcess() HANDLE GetCurrentThread() Return pseudo-handles not true unique identifiers See also GetProcessTimes and GetThreadTimes Can use DuplicateHandle to get a real handle to the thread