Threads - efreidoc.fr

Download Report

Transcript Threads - efreidoc.fr

CE01000-3 Operating Systems
Lecture 11
Windows – Object manager and
process management
Overview of lecture

In this lecture we will be looking at:






Windows object management
Threads and processes in Windows
Thread priority and scheduling
Thread synchronisation
Interprocess communication
Internal process and thread representation
Windows structure overview
USER APPLICATION
SUBSYSTEM DLL
SYSTEM
SUPPORT
ENVIRONMENT SUBSYSTEM
NTDLL.DLL
USER MODE
KERNEL MODE
EXECUTIVE
API
I/O
MANAGER
CACHE
MANAGER
PROCESS &
THREAD
MANAGER
SECURITY
REFERENCE
MONITOR
FILE
SYSTEMS
PLUG & PLAY
MANAGER
POWER
MANAGER
CONFIGUARI
ON
MANAGER??
OBJECT
MANAGER /
LOCAL
PROCEDURE
CALL
DEVICE
DRIVERS
HARDWARE
VIRTUAL
MEMORY
MANAGER
Graphics drivers
KERNEL
ABSTRACTION
Win32 USER and
GDI (Graphics
engine)
LAYER
(HAL)
SYSTEM
SERVICES
System resources and data



In any operating system the system needs to
keep track of all the different resources that
exist in the system e.g. files, I/O devices,
memory, system processes
It needs to keep track of all the various
processes (running programs) that use those
resources
To do this it needs to keep information about
these resources and their use.
System resources and data
(Cont.)


In many operating systems the information is
maintained in various data structures which
the operating system code manipulates
However in Windows the code that
manipulates the data is kept together with the
data in objects i.e. it uses an object oriented
approach to managing the system resources
and information about them
System resources and data
(Cont.)


ALL entities and services on the system are
represented internally by objects e.g. files are
represented by objects, as are user programs
when they are running on the computer,
hardware resources like printers,
communication ports, etc.
Motivation behind this is to provide a very
modular structure which it is easy to update,
extend, etc.
System resources and data (Cont.)


By hiding details of implementation of each
object’s class, changes to internal
implementation of an object's class can occur
without impacting upon the code
implementation of other object classes in the
system provided the interface remains
unchanged
It also makes it easier to add new features
and facilities to the system in the form of
new object classes
Object manager


The function of the object manager is to
provide services that manage all the objects in
the system
It provides mechanisms by which the various
other components in the Windows Executive
and the environmental subsystems can create,
use, and destroy objects
Object manager (Cont.)

Object manager provides a uniform set of
services to the other components of the
Executive - thus it simplifies object access,
etc, because the same mechanism is used in
each case
Object Handles and Handle Table



When a process (running program) wants to
use an object it calls the object manager open
service to get a handle to that object
handles are like pointers in C that allow you
to access the methods and data of an object
Processes must have an object handle for
every resource/item that it uses e.g. files, I/O
devices, etc.
Object Handles and Handle Table
(Cont.)

Each process has an Object Handle Table
that contains all the handles to the various
objects that the process has opened
Object Handles and Handle Table
(Cont.)
Process
Object Handle Table
......
Objects
Object structure
Object
Header
Object
Body
Object name
Object directory
Security descriptor
Quotas
Open handle count
Open handle list
Object type
Object specific data
List of processes
with handles
to object
Type
Object
Object structure (Cont.)


Objects in the system have object headers and
object bodies
Object header contains information such as
the object's name, the hierarchy to which it
belongs, security information (we will discuss
that in more detail later), quota information,
number of handles opened on object, pointer
to a list of processes that have handles opened
on object, pointer to an object that contains
information about the object's type
Object structure (Cont.)


Object body contains information that is
specific to the object
Type object contains information that is
common to all objects of a particular type
(class) and includes the code for all the
methods that belong to the object class
Object services

The object manager provides a set of services
that are common to all objects:






create - creates object and returns a handle to it
open - returns an object handle to calling process
close - destroys handle
delete - deletes object
duplicate handle
query information held in object
Processes and threads


In Windows a process consists of program
code, execution context ( the address space of
the process plus such things as the access
token) resources allocated to the process i.e.
handles, one or more threads
Threads are the units of execution – they
execute program code using the processes
context and resources
Processes and threads (Cont.)


A thread has its own context (register values
including instruction pointer, execution stack,
and such things as scheduling priority)
Threads are scheduled onto the processor, not
a process – this is different from Linux which
for scheduling purposes treats threads as a
separate process that happens to share address
space with another thread [Note – change to
handout]
Processes and threads (Cont.)
Processes and threads are managed by 2
components in Windows:
 the process and thread manager
and
 the kernel

kernel

The kernel is responsible for:




Thread scheduling
Interrupt and exception handling
Low level processor synchronisation
Recovery after power failure
kernel (Cont.)


In normal Operating Systems the kernel refers
to all the operating systems components that
run in kernel mode. In Windows as we have
seen this applies to all the Windows Executive
components (everything below the line in the
diagram).
However, perversely, Windows applies the
name kernel to just one component - a low
level layer of the OS that manages much of
the execution of threads within processes
kernel (Cont.)



The kernel uses objects to represent and
manage low level threads and processes
However these kernel objects are different
from those managed by the Object Manager
They are lower level and provide support for
the higher level objects used by the Object
Manager
Process and thread manager

Process manager is part of the Windows
Executive and implements the representation of
processes and threads that are available to other
parts of the Executive and provides high level
services to manage and control processes and
threads
Process and thread manager
(Cont.)

Process and thread manager provides
functions to




Create and destroy processes
Control resource allocation to processes
Keep track of information about processes and
threads
Processes are created by other processes by
making a CreateProcess system call
Process and thread manager
(Cont.)



Unlike Unix/Linux the parent and child
process are independent of each other –
remember fork in Unix/Linux creates the child
as a copy of the parent and hence has a copy
of the parents address space
In Windows child process has completely
separate address space, etc.
Hence Windows does not keep track of
process hierarchies
Thread scheduling


Windows maintains a list of threads that are in
the system
Threads may be in one of several different
states





Ready – thread can execute but waiting for a
processor
Running – running on a processor
Standby - selected to run next on a processor
Waiting – unable to execute until some event
occurs (typically I/O)
Terminated
Thread scheduling (Cont.)

All processes have at least one thread known
as the base thread – created when the
process is created
Thread scheduling (Cont.)



The kernel implements the dispatcher code,
which determines which thread to run next
The dispatcher implements pre-emptive
scheduling among threads
The dispatcher schedules threads without
reference to the process they belong to –
hence a process that has more threads will if
everything else is equal have a greater share
of the processor
Thread scheduling (Cont.)



The scheduling algorithm is based on a
multilevel priority queue approach with
each thread having a priority and hence
belonging to a given queue
A ready thread is placed in the queue which
represents its assigned priority
There are 32 priority queue levels designated
by numbers with 31 highest priority and 0
lowest
Thread scheduling (Cont.)


Dispatcher starts with highest priority queue
and schedules threads in order in queue in
round robin fashion
Each thread is given a time quantum in which
to execute and it either blocks itself waiting on
some I/O event or synchronisation event or the
quantum expires
Thread scheduling (Cont.)

Once a given queue is empty, the dispatcher
then proceeds to the next lowest priority queue
and so on until the queues are all empty or a
higher level priority thread enters a ready state
i.e. one of the higher level queues is now no
longer empty – and dispatcher pre-empts
lower priority running thread
Thread scheduling (Cont.)



Highest priority levels (16-31) is for real-time
threads (needing immediate responsiveness) –
the real-time priorities are static
Lower priority levels (0-15) are for dynamic
priority threads
A processes base thread is given a base
priority – which is the minimum priority a
thread can have
Thread scheduling (Cont.)



Each process has:
a base priority class (a range of priority
levels which the define the possible range of
base priorities) and
a base priority level which specifies the
relative base priority a threads should have in
the base priority class
Thread scheduling (Cont.)



Each thread then takes on priority values
dynamically i.e. it changes over time
e.g. if the thread is delayed waiting on an I/O
event, when the I/O event occurs and the
thread becomes ready again, its priority is
increased temporarily.
The size of the increase depends on the length
of the wait – the longer the wait the greater the
increase in priority
Traps and exception handling


Kernel implements a trap handler which
deals with hardware interrupts and processor
exceptions
The trap handler disables interrupts,
determines the cause of the interrupt, saves
processor state, re-enables interrupts and
dispatches code to deal with type of
interrupt/exception found (Interrupt service
routine)
Thread synchronisation


Windows provides a set of dispatcher objects
which can be used for synchronisation of
thread behaviour
These dispatcher objects can be in a signalled
state (when the event that the thread is waiting
for occurs) or a unsignaled state (when the
event has not yet occurred)
Thread synchronisation (Cont.)



Event objects represent events such as I/O events
– when the relevant event occurs the object
manager sets the event object to the signalled state
Mutex objects provide mutual exclusion – only
one thread can have a mutex object – it does this
by setting the object into an unsignaled state.
Once the thread completes its activity it sets the
mutex object to the signalled state
Waitable timer objects – these objects remain
unsignaled until a given time has elapsed
Interprocess communication

Threads (and hence also processes) can
communicate using a variety of methods




Pipes – work very similar to Unix – unidirectional
communication via a shared buffer
Sockets – similar to pipes but usually connect
processes and threads on different machines
Remote procedure calls – allows a thread in one
process to invoke the execution of code in different
processes address space
File sharing is also implemented
Internal process and thread
representation


Processes are represented within the NT
Executive by a process control block
(Executive Process Control or EPROCESS
block).
It stores information that other executive
components use when manipulating a process
– process id, pointer to processes handle table,
pointer to processes access token (used to
determine security in access), etc.
Internal process and thread
representation (Cont.)



Also contains a pointer to a kernel level
process block (KPROCESS block). This
contains information when used in scheduling
a process, such as priority, time quantum to be
used, etc.
EPROCESS blocks are held in a linked list
Thread information at the NT Executive level
is held in an Executive Thread block
(ETHREAD block)
Internal process and thread
representation (Cont.)

As with processes the ETHREAD block
includes a pointer to a kernel level kernel
thread block (KTHREAD block)
References

Operating System Concepts. Chapter 22.