Beruflich Dokumente
Kultur Dokumente
김도형
Table of Contents
What is a Threads?
Designing Threaded Programs
Synchronizing Threads
Managing Threads
2
What is a Thread?
Relationship between a process and a thread.
A process is created by the operating system. Processes contain
information about program resources and program execution
state, including:
Process ID, process group ID, user ID, and group ID
Environment
Working directory.
Program instructions
Registers
Stack
Heap
File descriptors
Signal actions
Shared libraries
Inter-process communication tools (such as message queues, pipes,
semaphores, or shared memory).
3
A Process
4
A Thread
Threads use and exist within these process resources,
yet are able to be scheduled by the operating system
and run as independent entities within a process.
A thread can possess an independent flow of control
and be schedulable because it maintains its own:
Stack pointer
Registers
Scheduling properties (such as policy or priority)
Set of pending and blocked signals
Thread specific data.
5
Threads
6
Threads
A process can have multiple threads, all of which
share the resources within a process and all of
which execute within the same address space.
Within a multi-threaded program, there are at any
time multiple points of execution.
Because threads within the same process share
resources:
Changes made by one thread to shared system resources (such
as closing a file) will be seen by all other threads.
Two pointers having the same value point to the same data.
Reading and writing to the same memory locations is possible,
and therefore requires explicit synchronization by the
programmer.
7
User Level Thread
Usually run on top of operating system
The threads within the process are invisible to the
kernel
Visible only from within the process
These threads compete among themselves for the resources
allocated to a process
Scheduled by a thread runtime system which is part of the
process code
Advantage
Inexpensive, low overhead
Need not create their own address space
Switching between thread does not involve switching address
space
8
Kernel Level Thread
Kernel is aware of each thread as a schedulable
entity
Threads compete for processor resources on a
system wide basis
Advantage
Multiple processor
9
Hybrid Thread
Task advantage of both user-level and kernel-level
thread
User-level threads are mapped into the kernel-
schedulable entities as the process is running to
achieve parallelism
Sun Solaris
The user-level thread called thread
Kernel schedulable entities are called lightweight processes
User can specify that a particular thread have a dedicated
lightweight process
10
Why Use Threads Over Processes?
11
Benefits of Thread Programming
12
What are Pthreads?
IEEE POSIX Section 1003.1c
IEEE ( Institute of Electric and Electronic Engineering )
POSIX ( Portable Operating System Interface )
Pthread is a standardized model for dividing a program into
subtasks whose execution can be interleaved or run in parallel
Specifics are different for each implementation
Mach Threads and NT Threads
13
Process/Thread Creation Overhead
fork() pthread_create()
Platform
real user sys real user sys
IBM 332 MHz 604e 92.4 2.7 105.3 8.7 4.9 3.9
4 CPUs/node
512 MB Memory
AIX 4.3
IBM 375 MHz POWER3 173.6 13.9 172.1 9.6 3.8 6.7
16 CPUs/node
16 GB Memory
AIX 5.1
INTEL 2.2 GHz Xeon 17.4 3.9 13.5 5.9 0.8 5.3
2 CPU/node
2 GB Memory
RedHat Linux 7.3
fork_vs_thread.txt
14
The Pthreads API
15
The Pthreads API
16
Creating Threads
Returns:
the new thread ID via the thread argument. This ID should be
checked to ensure that the thread was successfully created.
17
Creating Threads
attr :
is used to set thread attributes. You can specify a thread
attributes object, or NULL for the default values. Thread
attributes are discussed later.
start_routine:
is the C routine that the thread will execute once it is created.
arg :
An argument may be passed to start_routine via arg. It must be
passed by reference as a pointer cast of type void.
18
Terminating Thread Execution
19
Terminating Thread Execution
20
Pthread Creation and Termination
#include <pthread.h>
#define NUM_THREADS 5
21
Passing Arguments to Threads
22
Designing Threaded Programs
Designing Threaded Programs
24
Designing Threaded Programs
25
Designing Threaded Programs
Splitting
CPU-based (e.g., computation)
I/O-based (e.g., read data block from a file on disk)
Potential parallelism
The property that statements can be executed in any order
without changing the result
26
Potential Parallelism
27
Designing Threaded Programs
28
Manager/Worker Model
Program
Workers
Files
task
X
Databases
Boss
M ain ( ) task
Input (stream) Y
Disks
ta sk
Z
Special
Devices
29
Manager/Worker Model
30
Manager/Worker Model
Thread pool
A variant of the Manager/worker model
The manager could save some run-time overhead by creating all worker threads up
front
example 2 (manager/worker model with a thread pool )
Main(void) /* manager */
{
for the number of workers
pthread_create( ….pool_base );
forever {
get a request;
place request in work queue;
signal sleeping threads that work is available;
}
}
31
Manager/Worker Model
Example 2 (cont’d)
pool_base() /* All workers */
{
forever {
sleep until awoken by boss;
dequeue a work request;
switch {
case request X : taskX();
case request Y : taskY();
….
}
}
}
32
Pipeline Model
Program
Filter Threads
Resources
Files Files Files
33
Pipeline Model
Example (pipeline model program)
Main (void)
{
pthread_create( …stage1 );
pthread_create( …stage2);
…
wait for all pipeline threads to finish;
do any clean up;
}
Stage1 ()
{
forever {
get next input for the program;
do stage1 processing of the input;
pass result to next thread in pipeline;
}
}
34
Pipeline Model
Example (cont’d)
Stage2 ()
{
forever {
get input from previous thread in pipeline;
do stage2 processing of the input;
pass result to next thread in pipeline;
}
}
stageN ()
{
forever {
get input from previous thread in pipeline;
do stageN processing of the input;
pass result to program output;
}
}
35
Peer Model
Program
Workers
Files
task
Input X
(static)
------------ Databases
------------
------------
------------ task
------------ Y
------------
------------ Disks
------------
------------
------------
task
------------
Z
Special
Devices
36
Peer Model
Example (peer model program)
Main (void)
{
pthread_create ( …thread1 … task1 );
pthread_create ( …thread2 … task2 );
…
signal all workers to start;
wait for all workers to finish;
do any clean up;
}
task1 ()
{
wait for start;
perform task, synchronize as needed if accessing shared resources;
done;
}
37
To be continue…