Win32 Programming Lesson 10: Thread Scheduling and Priorities.

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

CMPT 401 Dr. Alexandra Fedorova Lecture III: OS Support.
Real-Time Library: RTX
IT Systems Multiprocessor System EN230-1 Justin Champion C208 –
 A quantum is the amount of time a thread gets to run before Windows checks.  Length: Windows 2000 / XP: 2 clock intervals Windows Server systems: 12.
Chapter 5 CPU Scheduling. CPU Scheduling Topics: Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling.
Lecture 10: Processes II-A Process States Process vs. Thread Background/Foreground.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
5: CPU-Scheduling1 Jerry Breecher OPERATING SYSTEMS SCHEDULING.
CS533 - Concepts of Operating Systems
Operating Systems Process Scheduling (Ch 4.2, )
1Chapter 05, Fall 2008 CPU Scheduling The CPU scheduler (sometimes called the dispatcher or short-term scheduler): Selects a process from the ready queue.
Scheduler Activations Jeff Chase. Threads in a Process Threads are useful at user-level – Parallelism, hide I/O latency, interactivity Option A (early.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Win32 Programming Lesson 9: Jobs & Thread Basics.
Chapter 5: CPU Scheduling (Continuation). 5.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 2, 2005 Determining Length.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 6: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms.
Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the.
Chapter 6: CPU Scheduling
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 5 Operating Systems.
Windows 2000 Scheduling Computing Department, Lancaster University, UK.
Nachos Phase 1 Code -Hints and Comments
Operating System Examples - Scheduling
Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Win32 Programming Lesson 1: Why We’re All Here. Why We’re Here…  Okay, maybe that’s too grandiose  Windows – in particular Win32 Thirty-what?  What.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CPU Scheduling CSCI 444/544 Operating Systems Fall 2008.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Web Based Programming Section 8 James King 12 August 2003.
Dynamic Architectures (Component Reconfiguration) with Fractal.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
Lecture 7: Scheduling preemptive/non-preemptive scheduler CPU bursts
Traditional UNIX Scheduling Scheduling algorithm objectives Provide good response time for interactive users Ensure that low-priority background jobs do.
Java Thread and Memory Model
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
I/O Software CS 537 – Introduction to Operating Systems.
1 Uniprocessor Scheduling Chapter 3. 2 Alternating Sequence of CPU And I/O Bursts.
Chapter 4 CPU Scheduling. 2 Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation.
Lecture 4 Page 1 CS 111 Summer 2013 Scheduling CS 111 Operating Systems Peter Reiher.
Lecture 4 CPU scheduling. Basic Concepts Single Process  one process at a time Maximum CPU utilization obtained with multiprogramming CPU idle :waiting.
Operating System Examples - Scheduling. References r er/ch10.html r bangalore.org/blug/meetings/200401/scheduler-
Process Scheduling. Scheduling Strategies Scheduling strategies can broadly fall into two categories  Co-operative scheduling is where the currently.
CPU scheduling.  Single Process  one process at a time  Maximum CPU utilization obtained with multiprogramming  CPU idle :waiting time is wasted 2.
CPU Scheduling Scheduling processes (or kernel-level threads) onto the cpu is one of the most important OS functions. The cpu is an expensive resource.
PROCESS MANAGEMENT IN MACH
Multi Threading.
Thread Fundamentals Header Advanced .NET Threading, Part 1
Lecture 21 Concurrency Introduction
Chapter 6: CPU Scheduling
Chapter 6: CPU Scheduling
Chapter 5: CPU Scheduling
Multithreading.
Chapter 05. Multithread.
Multithreaded Programming
Outline Scheduling algorithms Multi-processor scheduling
Fast Communication and User Level Parallelism
CSE 451 Autumn 2003 Section 3 October 16.
Multithreaded Programming
Process Scheduling Decide which process should run and for how long
Realizing Concurrency using the thread model
Shortest-Job-First (SJR) Scheduling
Presentation transcript:

Win32 Programming Lesson 10: Thread Scheduling and Priorities

Where are we?  We’ve got thread basics worked out…  But it’s very helpful to understand how the OS deals with thread scheduling  This lesson, we’ll work on scheduling threads – understanding how each one gets executed on the machine

Thread Context  Remember the kernel keeps a copy of the thread context which contains important runtime information Such as?  Every 20ms or so, Windows looks at the thread contexts on the machine and decided which to allow to run This is known as a context switch – see a tool like Spy++

Context Switch (cntd)  After about 20ms, the Operating System saves the processor’s internal state to thread’s context and looks for the next thread to execute  This cycle continues until the system shuts down  Only “schedulable” threads can be run

Example: Notepad  When Notepad is just sitting about as a Window with nothing to do, it is not marked schedulable  When the OS sees that the Windows has been moved or typed in, the thread is marked as schedulable NB: This is not the same as actually running the thread – that still depends on the OS scheduler

Ensuring you get run  Often, want to make sure that there is no latency between a particular action (maybe data arriving on a port) and response  How?  Sorry, can’t be done easily. Windows is not a Real-time operating system

Suspending and Resuming Threads  DWORD ResumeThread(HANDLE hThread)  DWORD SuspendThread(HANDLE hThread)  NB: A thread can be suspended multiple times. If a thread is suspended more than once, it must be resumed more than once before it becomes schedulable  Danger, Will Robinson! Randomly suspending a thread can cause deadlocks. Don’t do this unless you know exactly what the thread is doing!

Example: Suspending a Process  Why is the idea of suspending a process meaningless?  What do we mean when we do this?  Let’s look at an example from VS2013

Sleeping  A thread can tell the OS that it wants to go to sleep (that is, be unscheduled for a certain amount of time)  VOID Sleep(DWORD dwMilliseconds) Calling Sleep automatically gives up the rest of this time slice The time to sleep is approximate – remember, Windows is not a real-time OS You can call Sleep with the parameter INFINITE. This is not useful. You can call Sleep with the parameter 0 to give up the remainder of this timeslice

Switching to another Thread  Imagine you have a low-priority thread locking a resource…  Can use BOOL SwitchToThread() See if another thread is CPU starved and waiting Returns FALSE if no other thread can run Similar to Sleep except lower-priority threads also execute

Aside: ThreadExecution Times  Naively, most simply take the time at the start of a code block and the end, and subtract No guarantee there weren’t thread switches in there!  Instead, can use BOOL GetThreadTimes( HANDLE hThread, PFILETIME pftCreationTime, PFILETIME pftExitTime, PFILETIME pftKernelTime, PFILETIME pftUserTime);

Thread Context Revisited  There is actually a structure for the thread context documented by Microsoft… ooh!  Declared in winnt.h (see here for details)here  So, if we wanted to we could stop a running thread and modify its context… well, let’s look at the example in the book…

Thread Priorities  Each thread can execute at a different priority  Priorities are assigned from 0 (lowest) to 31 (highest)  When the scheduler assigns a thread it always passes control to a priority 31 thread if there are any available to run  And so on, down the priorities…

Caveat Emptor  Microsoft does not fully document the behavior of the scheduler  Microsoft tells you the scheduler is subject to change  Microsoft provides an abstraction layer – you can’t talk to the scheduler directly

Windows Priorities  Six process levels Real-time: respond immediately to time-critical messages. This priority is higher than task manager – it you use it, you can hang the machine! BE CAREFUL! High: threads which must respond. This is how task manager runs Above normal: between high and normal Normal: No special scheduling needs Below normal: between normal and idle Idle: Only run when the system is basically idle

And then threads  You can set the relative thread priorities too…  This sets the overall priority  So, this all sounds good, but how do you do it?

Programming Priorities  Parameter to CreateProcess  REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS

DIY  A process can also call BOOL SetPriorityClass(HANDLE hProcess, DWORD fwdPriority)  Example: SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS)  Also DWORD GetPriorityClass(HANDLE hProcess)  Similarly SetThreadPriority(HANDLE hThread, int nPriority)

Tweaking the Scheduler  Can optimize the foreground or background processes

Affinities  Can control which processor a thread executes on, on a multi-processor machine Why does this matter?

Assignment  Threading is a very useful technique  Write a network server which listens on Port  The server should handle multiple clients, creating a new thread for each client  The server simply echoes back what you type (but waits for a newline)  A session is closed when the string “close” is typed to the server  The console should provide a simple output which details the number of threads in use when asked  Also, the console should remain responsive to a “shutdown” request