Transcript Slide 1

Threads
Chapter 11 from the book: Inter-process Communications in Linux:
The Nooks & Crannies
by John Shapley Gray
Publisher: Prentice Hall
Pub Date: January 13, 2003
1
THREADS
Topics




2
Thread Definition
Creating a Thread
Exiting a Thread
Basic Thread Management
THREADS
Threads
3
THREADS
Thread Definition
 One common programming methodology is to
divide the problem into smaller specific
subtasks.
 When each of the subtasks (processes) is
finished, its solved portion of the problem is
integrated into the whole to provide an overall
solution.
 This approach is the basis for distributed
computation.
4
THREADS
Thread Definition
 Generating and managing individual processes
consume system resources.
 During process execution lifetime, the system
must keep track of the current state, program
counter, memory usage, file descriptors, signal
tables, and other details of the process.
 When the process exits, the operating system
must remove the process-associated data
structures and return the process's status
information to its parent process.
5
THREADS
Thread Definition
 Conceptually, a thread is a distinct sequence of
execution steps performed within a process.
 Threads have the ability to simultaneously take
different execution paths through their process
space.
6
THREADS
Thread Definition
 In a traditional setting, there is a single thread of
control.
 The thread starts at the first statement and
continues through the program logic in a serial
manner until the process finishes.
 In a multithreading (MT) setting, there can be
more than one thread of control active within a
process, each progressing concurrently.
7
THREADS
Thread Definition
 There are certain problem types that can have a
multithreaded solution.
 Problems that inherently consist of multiple,
independent tasks are prime candidates. For
example:
8
 monitor programs that manage a large number of
simultaneous connections and concurrent window
displays
 Producer—Consumer type problems
 Client—Server type problems
 Some numerical computations (such as matrix
multiplication) that can be divided into separate
subtasks
THREADS
Thread Definition
 Each thread has its own







stack
register set
program counter
thread-specific data
thread-local variables
thread-specific signal mask
state information
 However, all such threads share the same
address space, general signal handling, virtual
memory, data, and I/O with the other threads
9
within the same process.
THREADS
Thread Definition
 In a multithreaded process each thread executes
independently and asynchronously.
 However, many tasks handled by threads have
sequences that must be done serially. When
these sequences are encountered,
synchronization problems concerning data
access—updating—can arise.
 The term sibling is sometimes used to denote
other peer threads, as there is no inherent
parent/child relationship with threads.
10
THREADS
Thread Definition
 Figure 11.1 compares communications of single
thread processes versus multiple threads
process.
11
THREADS
Thread Definition
12
Figure 11.1. Conceptual communications—multiple single-threaded
processes versus a single process with multiple threads.
THREADS
Thread Definition
 At system implementation level there are two
traditional approaches or models used to
generate and manage threads.
 User-level model
 Kernel-level model
13
THREADS
Thread Definition
 The user-level model, runs on top of the existing
operating system and is transparent to it.
 Library functions and system calls made within
the thread are wrapped in special code to allow
for thread run-time management.
 Threads implemented in this manner have low
system overhead and are easily extensible.
 More importantly, user-level threads are
designed to share resources with other threads
within their process space when running on a
single processor.
14
THREADS
Thread Definition
 In the kernel-level model, the operating system
is aware of each thread.
 While the management of kernel-level threads is
less intensive than that of individual processes, it
is still more expensive than user-level-based
threads.
 The kernel-level threads are run as lightweight
processes (LWP) and are scheduled and
maintained by the operating system
15
THREADS
Creating a Thread
 Every process contains at least one main or
initial thread of control created by the operating
system when the process begins to execute.
 The library function pthread_create is used to
add a new thread of control to the current
process. The new thread executes with the other
threads within the process and, if directed, with
other threads of control in other processes.
(Table 11.1.)
16
THREADS
Creating a Thread
Include File(s) <pthread.h>
Summary
Return
17
Manual Section
3
int pthread_create(pthread_t *thread,
pthread_attr_t *attr,
void *(*start_routine)(void *),
void *arg);
Success
Failure
0
Nonzero
Sets errno
Table 11.1. The pthread_create Library Function.
THREADS
Creating a Thread
 Once a thread is created, it has its own set of
attributes and an execution stack.
 It inherits its signal mask (which it then can
alter) and scheduling priority from the calling
program (the initiating thread).
 It does not inherit any pending signals. If
needed, a thread can allocate its own storage for
thread-specific data.
18
THREADS
Creating a Thread
 The thread continues to execute until
 The function completes (implicitly or explicitly).
 A call is made to pthread_exit.
 The thread is canceled with a call to
pthread_cancel.
 The process that created the thread exits
(implicitly or explicitly).
 One of the threads performs an exec.
19
THREADS
Exiting a Thread
 The pthread_exit library call terminates a thread
in much the same manner as a call to exit
terminates a process.
(Table 11.2.)
20
THREADS
Exiting a Thread
Include File(s) <pthread.h>
Summary
Return
Manual Section
3
void pthread_exit (void * retval);
Success
Failure
Sets errno
This call does not return
21
Table 11.2. The pthread_exit Library Function.
THREADS
Basic Thread Management
 Once a thread is created, we can direct the
calling process to wait until the thread is finished
(it calls pthread_exit or is cancelled). This is
accomplished with a call to the pthread_join
library function.
(Table 11.3.)
22
THREADS
Basic Thread Management
Include File(s) <pthread.h>
Summary
Return
23
Manual Section
3
int pthread_join( pthread_t target_thread,
void **status );
Success
Failure
0
Nonzero
Sets errno
Table 11.3. The pthread_join Library Function.
THREADS
Basic Thread Management
 There are some caveats associated with joining
threads.
24
 A thread should be waited upon (joined) by only
one other thread.
 The thread issuing the join does not need to be
the initiating thread.
 If multiple threads wait for the same thread to
complete, only one will receive the correct status
information.
 The joins in competing threads will return an error.
 Should the thread initiating the join be canceled,
the waited upon thread can be joined by another
thread.
THREADS
Basic Thread Management
 If the targeted thread has terminated prior to the
issuing of the call to pthread_join, the call will
return immediately and will not block.
 A non detached thread (which is the default) that
is not joined will not release its resources when
the thread finishes and will only release its
resources when its creating process terminates.
 Such threads can be the source of memory
leaks.
25
THREADS
Basic Thread Management
 The process of joining a thread is somewhat
analogous to a process waiting on a forked child
process.
 Unlike a forked child process, a thread can
become detached with a single library call.
 When a detached thread finishes, its resources
are automatically returned to the system.
 The pthread_detach library call is used to
dynamically detach a joinable thread.
(Table 11.4)
26
THREADS
Basic Thread Management
Include File(s) <pthread.h>
Summary
Return
27
Manual Section
3
int pthread_detach (pthread_t threadID);
Success
Failure
0
Nonzero
Sets errno
Table 11.4. The pthread_detach Library Function.
THREADS
Basic Thread Management
 Once a thread is detached, other threads can no
longer synchronize their activities based on its
termination.
28