Threads - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Threads

Description:

University of Karlsruhe. 1. Threads. Threads (1) ... Clans and Chiefs. system. system wide management. clan 2. Clan management. clan 1. clan management ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 45
Provided by: liefl
Category:
Tags: clan | threads

less

Transcript and Presenter's Notes

Title: Threads


1
Threads
Threads (1)
Recommended Reading Bacon, J. Concurrent
Systems (6, 7) Stallings, W. Operating Systems
(3, 4) Silberschatz, A. Operating Systems
Concepts (2) Tanenbaum, A. Modern Operating
Systems (2) Wettstein, H. Systemarchitektur (4,
5, 6)
2
Threads
Overview Motivation Notions Thread
Control Thread Representation
3
Threads
Motivation
Why do we need threads?
Suppose your system offers an interesting
software tool enhancing program editing and
testing. Thus you like it.
If your system supports concurrent editing,
compiling and testing, this could reduce your
programming work a great deal.
? Threads enable concurrency
4
Motivation
Two Main Challenges within Systems
1. How to install information processing,
i.e. when to execute which code activity
? thread
2. How to establish data depositories, i.e.
where to store which data entity
? address space
5
Motivation
System design parameters concerning threads
  • Number of activities (static/dynamic)
  • Types of activities (for-/background)
  • Priority of activities (real time, interactive,
    batch processing)
  • Required resources for activities (only CPU,
    I/O ...)
  • ...

6
Motivation
System design parameters concerning address
spaces
  • Number of data entities
  • Types of buffers (stack, heap, file, )
  • Security of data entity (protection domain, )
  • Duration of data entity (volatile/persistent)
  • ...

7
Threads
Notion Thread abstraction of an
activity (being executed on a
CPU) ? Thread includes code and data
(mostly a stack).
Preview Thread may also need some environment
(e.g. (e.g. address space, files, I/O-devices
and others). It may even share this environment
with other threads.
Example A file server may consist of t identical
threads, each thread serving one clients
request.
8
Threads
  • First glance on following notions
  • Thread
  • Process
  • Task

9
Threads
First glance on following notions Thread Entity
of activity, object for dispatching (scheduling)
Process Task
time
Threads may be executed on a fitting hardware
(e.g. CPU), thus we need some control on
concurrent threads
10
Threads
First glance on following notions Thread Entity
of activity, object for dispatching (scheduling)
Process Single Thread Address Space (Unix
Terminology) Task
Address space protected area
11
Threads
First glance on following notions Thread Entity
of activity, object for dispatching (scheduling)
Process Single Thread Address Space (Unix
Terminology) Task Entity of Application
consisting of t gt 1 Thread(s) Address Space
(later) ...
1 or more threads
12
Threads
First glance on following notions Thread Entity
of activity, object for dispatching (scheduling)
Process Single Thread Address Space (Unix
Terminology) Task Entity of Application
consisting of t gt 1 Thread(s) Address Space
(later) ...
Remark Stallings process is our
task, Wettsteins process is our
thread. Threads are also called lightweight
processes!
13
Threads


Degree of Dependency
Threads are independent of each other if they
do not work together, do not communicate or
influence each other.
Threads are dependent of each other if
they cooperate on a specific common mission,
e.g. they are part of a parallel algorithm.
14
Threads

Examples
Given a introductory course in programming (i.e.
C or Pascal) All exercise solutions of the
students are executed as independent
threads. Though they might compete for hardware
as well as for software resources, their results
should not depend on any other execution sequence.
However, a student in an advanced course (e.g.
Concurrent Pascal) may deliver one specific
solution consisting of several dependent threads,
e.g. solving a parallel numeric algorithm.
15
Threads
Scopes and Implementations
system wide management
Well have to discuss what management means
(later).
16
Threads
Clans and Chiefs
clan 2
clan 1
system
Clan management
clan management
system wide management
Discuss Pros and Cons of a chief restricted
interelationship
17
Threads
Thread versus Task (Process)
18
Threads
Need for thread control with independent threads
Program bugs Endless loop gt reducing
system performance and clients budget Endless
production of threads within an application gt
wasting system capacity
Conclusion We need something to abort faulty
programs (either explicitly by hand or
automatically)
Any additional control problems with independent
threads?
19
Threads
Additional control problems with independent
threads
  • Deadlock, i.e. at last two threads are blocking
    each other
  • due to competing request on resources or due
  • to other mutual blocking events.
  • Starvation, i.e. a thread can never proceed
    because,
  • a system component (e.g. a device) does not
    respond
  • there are always active threads with higher
    priority
  • there is a deadlock.

Conclusion We need something helping us to
navigate around deadlocks and to detect
starvations and we need a timeout mechanism.
20
Threads
Additional control problems with dependent
threads
  • Data (In)consistency
  • Race Conditions
  • If the result of an execution of multiple threads
    depends on some unpredictable timing behavior,
    there are so called race conditions.
  • Race conditions occur if shared data is accessed
    unsynchronized.

Conclusion We need mechanisms to synchronize
threads, I.e. synchronization is required for
cooperation of concurrent threads
21
Threads
Example Race Condition
3 variables A, B, C which are shared by thread
T1 and thread T2
  • T1 computes C A B
  • T2 transfers amount X from A to B
  • A A - X and B B X (AB remains
    unchanged)

Possible execution sequences A A - X C
A B B B X result C ! A B
C A B A A - X B B X C A B
22
Threads
Another thread-control problem
In order to be executed each thread needs at
least a processing unit
Conclusion No problem if enough processors
Remark However, in practice only few systems
have this nice property.
23
Threads
Another thread-control problem
In order to be executed each thread needs at
least a processor.
Observation Due to lack of processors (many
system have only 1 CPU) we have to switch the
processor amongst the set of threads.
Conclusion We need a mechanism enabling thread
switching.
24
Threads
Thread Switching
Virtual Processor.
How can we name the underlying principle?
Looking at the above scene with a coarser measure
of time any thread seems to have a processor of
its own.
Mechanism enabling virtual processor is called
multiplexing Component implementing
multiplexing is the dispatcher
Who knows some multiplexing analogies?
25
Threads
Multiplexing Examples
26
Threads
Thread Representation
To implement threads we need something,
representing each thread towards the system TID
thread identifier (see our identity card or
passport towards the system FRG)
Why and when do we need a TID? Hint Compare
with your passport.
  • Crossing borders, controlling and arresting
    suspicious people
  • find appropriate analogies in systems and other
    examples

27
Threads
Thread Representation
The TID all together with other useful
information describing and characterizing a
thread, respectively reflecting its past,
current, and future state is collected within
the thread control block TCB
Definition A thread attribute describes or
characterizes a thread
Example A thread intensively using I/O is called
I/O-bound
Remark Different systems may need different
attributes within their TCBs. Thus TCBs may
vary in size and structure from one system to
another one.
What are the minimal contents of a TCB?
28
Threads
Minimal TCB
Thread Identifier (TID)
Stack Pointer (SP)
Status Flags (SF)
ltSP, SFgt describe the minimal context of a thread.
29
Threads
Thread Control Block
The following attributes may belong to a TCB
  • Thread Identification (TID)
  • Thread State Information (later)
  • Thread Control Information

30
Threads
Thread Identifiers
Which property should a (numeric) thread
identifier have?
It should be unique in time
Discuss Pros and Cons
31
Threads
Thread Control Information
  • User-Visible Registers
  • - Stack-Related
  • - General Purpose
  • - Floating-Point
  • - Index
  • Control and Status Registers

Stacks are used to support procedure and system
calls (establishing local variables, transfer of
parameters etc.) A stack pointer points to the
top of the stack.
32
Threads
Visualization of a Stack (Our view)!
4000
Top Stack Element
4711
Question What would you provide in case of stack
overflow?
33
Threads
Thread Control Information
  • User-Visible Registers
  • - Stack-Related
  • - General Purpose
  • - Floating-Point
  • - Index
  • Control and Status Registers

Instruction Pointer ( address of next
instruction, the notion Program Counter is
misleading.) Condition Codes ( mirroring
results of a previous instruction, e.g. equal
bit, overflow bit) Status Information ( execution
mode etc.)
34
Threads
Further Thread Control Information
Events related to thread execution, e.g. waiting
for I/O (? scheduling and thread
states) Priorities Inter-Process Communication
(IPC) Mapping Information (mostly task
specific) Resource Ownership and Utilization
(mostly task specific)
Remark Well discuss these topics later
35
Threads
Data Structures for Set of TCBs
  • 2 major implementation techniques
  • Table of TCBs (thread table)
  • Structured list of TCBs

Concerning different system constraints Reason
about appropriate data structures for
implementing a structured list of TCBs
36
Threads
Conventional Location of system wide TCBs
Kernel
respectively in a
µ-Kernel
37
Threads
Other possible locations for TCBs?
TCB of a subsystem-wide thread can be placed
within the appropriate subsystem itself.
Threads only visible within a subsystem are
called green threads or user-level threads.
38
Threads
First View on the Execution of 2 Concurrent
Threads
Assumption 2 independent threads A and B, each
one consisting of 8 instructions. Thread A
contains an I/O-Instruction (say instruction
a2), whereas B contains no I/O. (I/O lasts 12
instructions). Any dispatching between the
threads lasts 3 instructions!
39
Threads
Mapping of Threads
0 d a b
d is Starting Address of Dispatcher a is
Starting Address of thread A b is Starting
Address of thread B
Dispatcher
Thread A
Instruction Pointer
Thread B
Main Memory
CPU
Assumption For ease of complexity no virtual
memory!
40
Threads
Mapping of Threads
0 d a b
Dispatcher
Thread A
Instruction Pointer
Thread B
Main Memory
CPU
Assumption Thread A starts executing
What will happen then? Is the
answer unambiguous?
41
Threads
Execution sequence depends on the dispatching
rule and on the coding of the threads (contents
of a i respectively b j)
Example A common Dispatching Rule (round robin
discipline) Execute a thread for a maximum of 5
instructions, only.
Afterwards the dispatcher switches control to
the other thread (if there is another one).
42
Threads
Resulting Execution Trace
d 0 d 1 d 2 a 3 a 4 a 5 a 6 a
7 (Termination of A) d 0 d 1 d 2
a 0 a 1 a 2 (Start I/O) gt d 0 d 1 d
2 b 0 b 1 b 2 b 3 b 4 (End of Time
Slice) d 0 d 1 d 2 (only B is
runnable) b 5 b 6 b 7 (Termination of B)
What to do next?
43
Threads
Summary of Example
Up to now 2 reasons for dispatching activities
  • Internal behavior of a thread
  • (e.g. I/O-instruction)
  • System objective (fair scheduling)
  • (No thread phase longer than i instructions)

Further reasons for dispatching activities?
Think over it.
44
Threads
Preview
  • Thread Control
  • Thread Representation
  • Thread Switch (very exiting)
  • Thread States
  • Dispatching of Threads
Write a Comment
User Comments (0)
About PowerShow.com