Threads: Tevfik Koşar
Threads: Tevfik Koşar
Fall 2014
Lecture - IV
Threads
Tevfik Koar
University at Buffalo
September 4th, 2014
Roadmap
Threads
Thread cancellation
Signal handling
Thread pools
Thread specific data
Concurrent Programming
In certain cases, a single application may need to run
several tasks at the same time
1
1
3
4
concurrent
4
5
sequential
Motivation
Increase the performance by running more than one
tasks at a time.
divide the program to n smaller pieces, and run it n times
faster using n processors
Serial vs Parallel
COUNTER 2
COUNTER
COUNTER 1
Please
1:
2:
3:
4:
5:
6:
7:
x1
x1
x1
x1
x1
x1
x1
+
+
+
+
+
+
+
x2
x2
x2
x2
x2
x2
x2
+
+
+
+
+
+
x3
x3
x3
x3
x3
x3
+
+
+
+
+
x4
x4
x4
x4
x4
+
+
+
+
x5
x5 + x6
x5 + x6 + x7
x5 + x6 + x7 + x8
6
Step 2: parallelism = 2
Step 3: parallelism = 1
Concurrent Programming
Implementation of concurrent tasks:
as separate programs
as a set of processes or threads created by a single program
Why Threads?
In certain cases, a single application may need to run
several tasks at the same time
Creating a new process for each task is time consuming
Use a single process with multiple threads
faster
less overhead for creation, switching, and termination
share the same address space
10
Threading Benefits
Patterns of multithreading usage across applications
perform foreground and background work in parallel
illusion of full-time interactivity toward the user while
performing other tasks (same principle as time-sharing)
allow asynchronous processing
separate and desynchronize the execution streams of
independent tasks that dont need to communicate
handle external, surprise events such as client requests
increase speed of execution
stagger and overlap CPU execution time and I/O wait
time (same principle as multiprogramming)
11
Ownership vs Execution
A process embodies two independent concepts:
1.
2.
resource ownership
execution & scheduling
1. Resource ownership
Multi-threading
other thread
CPU
input data
Process
13
14
block (PCB)
stack
thread 1 control
block (TCB 1)
data
thread 1 stack
thread 2 control
block (TCB 2)
program
code
thread 2 stack
data
program
code
Multi-process model
Process Spawning:
Process creation involves the following four main actions:
setting up the process control block,
allocation of an address space and
loading the program into the allocated address space and
passing on the process control block to the scheduler
17
Multi-thread model
Thread Spawning:
Threads are created within and belonging to processes
All the threads created within one process share the resources of the
process including the address space
Scheduling is performed on a per-thread basis.
The thread model is a finer grain scheduling model than the process
model
Threads have a similar lifecycle as the processes and will be managed
mainly in the same way as processes are
18
Threads vs Processes
A common terminology:
Heavyweight Process = Process
Lightweight Process = Thread
Advantages (Thread vs. Process):
Much quicker to create a thread than a process
spawning a new thread only involves allocating a new stack and a new
CPU state block
Much quicker to switch between threads than to switch between processes
Threads share data easily
Disadvantages (Thread vs. Process):
Processes are more flexible
Thread Creation
pthread_create
// creates a new thread executing start_routine
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg);
pthread_join
// suspends execution of the calling thread until the target
// thread terminates
int pthread_join(pthread_t thread, void **value_ptr);
20
Thread Example
int main()
{
pthread_t thread1, thread2;
/* thread variables */
21
Exercise
Consider a process with two concurrent threads T1 and T2. The code being
executed by T1 and T2 is as follows:
Shared Data:
X:= 5; Y:=10;
T1:
Y = X+1;
X = Y;
Write X;
T2:
U = Y-1;
Y = U;
Write Y;
22
Solution
All six statements can be executed in any order. Possible outputs are:
1) 65
2) 56
3) 55
4) 99
5) 66
6) 69
7) 96
23
Threading Examples
Web server
as each new request comes in, a dispatcher thread spawns a
new worker thread to read the requested file (worker threads
may be discarded or recycled in a thread pool)
Tanenbaum, A. S. (2001)
Modern Operating Systems (2nd Edition).
Threading Examples
Word processor
one thread listens continuously to keyboard and mouse events
to refresh the GUI; a second thread reformats the document (to
prepare page 600); a third thread writes to disk periodically
Tanenbaum, A. S. (2001)
Modern Operating Systems (2nd Edition).
Thread Implementation
Two broad categories of thread implementation
User-Level Threads (ULTs)
Kernel-Level Threads (KLTs)
Pure user-level (ULT), pure kernel-level (KLT) and combined-level (ULT/KLT) threads
26
Thread Implementation
User-Level Threads (ULTs)
the kernel is not aware of the existence of threads, it knows
only processes with one thread of execution (one PC)
each user process manages its own private thread table
C
Thread Implementation
Kernel-Level Threads
the kernel knows about and manages the threads: creating and
destroying threads are system calls
C
Many-to-One
One-to-One
Many-to-Many
Hybrid
29
Many-to-One Model
Several user-level threads
mapped to single kernel
thread
Thread management in
user space efficient
If a thread blocks, entire
process blocks
One thread can access the
kernel at a time limits
parallelism
Examples:
Solaris Green Threads
GNU Portable Threads
30
One-to-One Model
31
Many-to-Many Model
Allows many user level threads to
be mapped to a smaller number
of kernel threads
Allows the operating system to
create a sufficient number of
kernel threads
Increased parallelism as well as
efficiency
Solaris prior to version 9
Windows NT/2000 with the
ThreadFiber package
32
Two-level Model
Similar to M:M, except that it allows a user thread to be
bound to kernel thread
Examples: IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier
33
Threading Issues
Thread pools
Thread specific data
Semantics of fork() and exec() system calls
Thread cancellation
Signal handling
34
Thread Pools
Threads come with some overhead as well
Unlimited threads can exhaust system resources, such as CPU
or memory
Create a number of threads at process startup) and put them
in a pool, where they await work
When a server receives a request, it awakens a thread from
this pool
Advantages:
Usually faster to service a request with an existing thread than
create a new thread
Allows the number of threads in the application(s) to be bound
to the size of the pool
35
36
Thread Cancellation
Terminating a thread before it has finished
If one thread finishes searching a database,
others may be terminated
If user presses a button on a web browser, web
page can be stopped from loading further
37
Signal Handling
Signals are used in UNIX systems to notify a
process that a particular event has occurred
All signals follow this pattern:
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Once delivered, a signal must be handled
Summary
Hmm.
.
Thread cancellation
Signal handling
Thread pools
Thread specific data
39
Acknowledgements
Operating Systems Concepts book and supplementary
material by A. Silberschatz, P. Galvin and G. Gagne
Operating Systems: Internals and Design Principles
book and supplementary material by W. Stallings
Modern Operating Systems book and supplementary
material by A. Tanenbaum
R. Doursat and M. Yuksel from UNR
40