Professional Documents
Culture Documents
Systems:
Internals
and
Design
Principles
Lecture 04
Threads
Dr. Muhammed J. Al-Muhammed
Operating Systems
Operating Systems:
Internals and Design Principles
Operating Systems
Processes and
Threads
Processes have two
Resource
characteristics:
Ownership
Process includes a
virtual address
space to hold the
process image
the OS performs a
protection function
to prevent unwanted
interference between
processes with
respect to resources
Operating Systems
Scheduling/Execut
ion
Follows an execution
path that may be
interleaved with other
processes
a process has an
execution state
(Running, Ready,
etc.) and a
dispatching priority
Processes and
Threads
The
The
Multithreading
- The ability of an OS
to support multiple, concurrent paths
of execution within a single process
4
Operating Systems
Single Threaded
Approaches
A single thread of
execution per
process, in which
the concept of a
thread is not
recognized, is
referred to as a
single-threaded
approach
MS-DOS is an
Operating Systems
Multithreaded
Approaches
A Java run-time
environment is an
example of a
system of one
process with
multiple threads
Operating Systems
Processes
The unit of resource allocation and a unit of
protection
files
I/O resources
Operating Systems
One or More
Threads
in a Process
Each thread has:
A thread execution state (Running,
Ready, etc.)
saved thread context when not
running
an execution stack
some per-thread static storage for local
variables
access to the memory and resources of
8
its process (all threads of a process
Operating Systems
Threads vs.
Processes
Contains registers
values, priority, and
thread related data.
All threads of process share the resources and state of that process
if a thread alters data item in memory all can see the change. Example of
a thread opens a file with read privilege all other threads can read the file
Operating Systems
Benefits of Threads
The key benefits
Takes less
time to create
a new thread
than a
process
(studies show
creation of a
thread ten
times faster)
10
Operating Systems
Threads
of threads derive from the performance enhance
implications
efficiency in
communication
between
programs:
communication
Less time to
between
terminate a
Switching
processes
thread than a
between two
requires kernel
process
threads takes
intervention
less time than
while
switching
communication
between
between
processes
threads does
not need kernel
invocation.
Thread Use in a
Single-User System
Foreground
Asynchronous
Speed
of execution
11 Modular
Operating Systems
program structure
Threads
In an OS that supports threads,
scheduling
dispatching
is done on a
Hence, and
most
of the state
thread
basis dealing with execution
information
is maintained in thread-level data
structures
Several actions may affect all
Suspending a process involves suspending all
threads
12
Operating Systems
Thread Execution
States
The key states for
a thread are:
Running
Ready
Blocked
13
Operating Systems
Thread
operations
associated with
a change in
thread state are:
Spawn
Block
Unblock
Finish
14
Operating Systems
15
Operating Systems
Thread
Synchronization
It
16
Operating Systems
Types of Threads
User Level
Thread (ULT)
17
Operating Systems
Kernel level
Thread (KLT)
User-Level Threads
(ULTs)
All
thread
management
is done by the
application
The
kernel is
not aware of
the existence
of threads
18
Operating Systems
19
Operating Systems
Figure 4.6 Examples of the Relationships between User-Level Thread States and
Process States
Advantages of
ULTs
Scheduling can
be application
specific
20
Thread switching
does not require
kernel mode
privileges
Operating Systems
ULTs
can
run on
any OS
Disadvantages of
ULTs
In
In
Operating Systems
Overcoming ULT
Disadvantages
(1) Jacketing -- converts a blocking
system call into a non-blocking system
call
Kernel-Level Threads
(KLTs)
Thread
management is
done by the
kernel
23
Operating Systems
no thread
management is
done by the
application
API to kernel
thread facility
Advantages of KLTs
The
If
24
Operating Systems
Disadvantage of KLTs
The transfer of control from one thread
to another within the same process
requires a mode switch to the kernel
25
Operating Systems
Combined Approaches
Thread creation is
done in the user
space
Bulk of scheduling
and synchronization
of threads is by the
application
Solaris is an
example
26
Operating Systems
Relationship Between
Threads and
Processes
27
Operating Systems
Table 4.2
Performance Effect
of Multiple Cores
28
Operating Systems
Windows Processes
29
Operating Systems
Windows Processes
Processes and services provided by the
Windows Kernel are relatively simple and
general purpose allowing each subsystem
to emulate particular process structure
and functionality.
Important characteristics of windows
process
implemented as objects
created as new process or a copy of an
existing
an executable process may contain one or
more threads
30
both processes and thread objects have
Operating Systems
31
Operating Systems
Processes
32
an entity
correspondin
g to a user
job or
application
that owns
Operating Systems
Threads
a
dispatchable
unit of work
that executes
sequentially
and is
Windows Process
and
Thread Objects
33
Operating Systems
Windows Process
Object Attributes
34
Operating Systems
35
Thread ID
Thread context
The set of register values and other volatile data that defines the
execution state of a thread.
Dynamic priority
Basepriority
Alert status
Suspension count
Impersonation token
Termination port
Operating Systems
Multithreaded
Process
Achieves concurrency
without the overhead of
using multiple
processes
36
Operating Systems
Threads in different
processes can
exchange information
through shared memory
that has been set up
between the two
processes
Thread
States
37
Operating Systems
Symmetric
Multiprocessing Support
(SMP)
Threads of
any
process
can run on
any
processor
38
Operating Systems
Soft
Affinity
the dispatcher
tries to assign a
ready thread to
the same
processor it last
ran on
helps reuse data
still in that
processors
memory caches
from the
previous
execution of the
Hard
Affinity
an application
restricts thread
execution to
certain
processors
Solaris Process
- makes use of four thread-related concepts:
Process
39
User-level
Threads
Lightweight
Processes
(LWP)
Kernel
Threads
Operating Systems
Processes and
Threads
in Solaris
40
Operating Systems
I
Y
F Traditional
Unix vs
Solaris
41
Operating Systems
A Lightweight Process
(LWP) Data Structure
Includes:
An LWP identifier
42
A signal mask
Operating Systems
Solaris Thread
States
43
Operating Systems
Interrupts as
Threads
Most
Processes (threads)
Interrupts
synchronized by
preventing their handling
for a period of time
44
Operating Systems
Solaris Solution
Solaris
Linux Tasks
A process, or
task, in Linux is
represented by a
task_struct data
structure
46
Operating Systems
This structure
contains
information in a
number of
categories
Linux
Process/Thread Model
47
Operating Systems
Linux Threads
A new
process is
created
by
copying
the
attributes
of the
current
process
Linux
does not
recognize
a
distinction
between
threads
and
processes
48
Operating Systems
User-level
threads
are
mapped
into
kernellevel
processes
The
clone()
call
creates
separate
stack
spaces for
each
process
The new
process
can be
cloned so
that it
shares
resources
CLONE_CLEARID
CLONE_DETACHED The parent does not want a SIGCHLD signal s ent on exit.
Linux
Clone ()
Flags
49
Operating Systems
CLONE_FILES
CLONE_FS
Shares the table that identifies the root directory and the current
working directory, as well as the value of the bit mask used to mask the
initial file permissions of a new file.
CLONE_IDLETASK Set PID to zero, which refers to an idle task. The idle task is employed
when all available tasks are blocked waiting for resources.
CLONE_NEWNS
CLONE_PARENT
CLONE_PTRACE
If the parent process is being traced, the child process will also be
traced.
CLONE_SETTID
CLONE_SETTLS
CLONE_SIGHAND
CLONE_SYSVSEM
CLONE_THREAD
Inserts this process into the same thread group of the parent. If this flag
is true, it implicitly enforces CLONE_ PARENT.
CLONE_VFORK
If set, the parent does not get schedu led for execution until the child
invokes the execve() system call.
CLONE_VM
Shares the address space (memory descriptor and all page tables).
Designers
Concurrency
50
Operating Systems
Block
A
Operating Systems
Summa
ry
52
User-level threads
created and managed by a threads library that runs in the user space of a
process
Kernel-level threads
Operating
Systems