Win32 Programming - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Win32 Programming

Description:

Suspending and Resuming Threads. DWORD ResumeThread(HANDLE hThread) ... more than once, it must be resumed more than once before it becomes schedulable ... – PowerPoint PPT presentation

Number of Views:100
Avg rating:3.0/5.0
Slides: 22
Provided by: richar134
Category:

less

Transcript and Presenter's Notes

Title: Win32 Programming


1
Win32 Programming
  • Lesson 10 Thread Scheduling and Priorities

2
Where are we?
  • Weve got thread basics worked out
  • But its very helpful to understand how the OS
    deals with thread scheduling
  • This lesson, well work on scheduling threads
    understanding how each one gets executed on the
    machine

3
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

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

5
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

6
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, cant be done easily. Windows is not a
    Real-time operating system

7
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.
    Dont do this unless you know exactly what the
    thread is doing!

8
Example Suspending a Process
  • Why is the idea of suspending a process
    meaningless?
  • What do we mean when we do this?
  • Lets look at an example from VS2008

9
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

10
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

11
Aside ThreadExecution Times
  • Naively, most simply take the time at the start
    of a code block and the end, and subtract
  • No guarantee there werent thread switches in
    there!
  • Instead, can use
  • BOOL GetThreadTimes(     HANDLE hThread,    
    PFILETIME pftCreationTime,     
    PFILETIME pftExitTime,     PFILETIME pftKernelT
    ime,        PFILETIME pftUserTime)

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

13
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

14
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
    cant talk to the scheduler directly

15
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

16
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?

17
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

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

19
Tweaking the Scheduler
  • Can optimize the foreground or background
    processes

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

21
Assignment
  • Threading is a very useful technique
  • Write a network server which listens on Port
    31337
  • 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
Write a Comment
User Comments (0)
About PowerShow.com