1
Part II Processes and Threads
Threads Basics
Fall 2015
You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program. Alan J. Perlis
Part II Processes and Threads Threads Basics You think you know - - PowerPoint PPT Presentation
Part II Processes and Threads Threads Basics You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program. 1 Fall 2015 Alan J. Perlis Wh What at Is a s a Th Threa
1
Fall 2015
You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program. Alan J. Perlis
2
thread, also known as ligh ghtw twei eigh ght t process process (LWP), is a basic unit of CPU execution, and is created by a process.
register set, and a stack. Thus, it is similar to a process.
the same process its code section, data section, and other OS resources (e.g., files and signals).
thread of control.
3
process control block user address space user stack system stack process control block user address space user stack system stack user stack system stack user stack system stack thread control block thread control block thread control block
Single-threaded process Multithreaded process
Single gle Threaded aded and Multithreaded ithreaded Proces ess
4
program may still be running even if one part (e.g., a thread) is blocked.
default, share many system resources (e.g., files and memory).
allocating memory and resources, and context switching processes are very time consuming.
Multiple CPUs may run multiple threads of the same process. No program change is necessary.
5
ser T r Thr hrea eads ds: User threads are supported at the user level. The kernel is not aware of user threads. A library provides all support for thread creation, termination, joining, and scheduling. Since there is no kernel intervention, user threads are usually more efficient. Unfortunately, since the kernel only recognizes the containing process (of the threads), if on
e th thre read ad i is bl s bloc
ked, d, a all thr threa eads ds of
the sa same pr e proc
ess s ar are e al also so b bloc
ked because the containing process is blocked.
6
erne nel thr threa eads ds: Kernel threads are supported by the kernel. The kernel does thread creation, termination, joining, and scheduling in kernel space. Kernel threads are usually slower than user threads due to system overhead. However, bl bloc
king ng o
ne th thre read ad will no not t ca caus use e ot
her r th thre read ads s of
the sa same e process to block process to block. The kernel simply runs
In a multiprocessor environment, the kernel may run threads on different processors.
7
8
models: Man any-to to-One ne Mod
el: One kernel thread (or process) has multiple user threads. Thus, this is a user thread model. One ne-to to-One ne Mod
el: One user thread maps to one kernel thread (e.g., old Unix/Linux and Windows systems). Man any-to to-Man any y Mod
el: Multiple user threads map to a number of kernel threads.
9
Each ch process ess has mu multipl tiple e user threads eads that at are associa
ted with th one kernel el threads.
ess is blocked, ed, all user r threads eads of that at process ess are blocked. ed.
10
An Extreme reme Case: e: Traditional ditional Un Unix
Each ch proces ess s has only one user r thread ad that at is associa
ed with th exactl tly one kernel el thread ad
11
Each ch process ess has mu multiple tiple user thread eads s each ch of which h is associa ciated ted with h one kernel el thread.
nel thread ad is blocked, ed, the associa
ed user thread ead is blocked. ed.
12
Each ch process ess has mu multipl tiple e threads ads that at are associa
ed with mu mult ltiple iple kernel el threads ads. . If a kernel el thread ad is blocked, ed, all user r thread eads associa ciated ted with h that at kernel el thread ad are blocked. ed.
13
by a scheduler and can only run one at a time.
run at the same time, one on each core.
complex than one may expect.
dividing activities, balance, data splitting, data dependency, and testing and debugging.
14
viding ding Activ ivit itie ies: Since each thread can run on a core, one must study the problem in hand so that program activities can be divided and run concurrently.
good example.
problems are inherently sequential (e.g., DFS).
C A B
i j i k k j k n , , ,
1
We may create a thread for each Cij
15
alan ance ce: Make sure that each thread has equal contribution, if possible, to the whole computation.
would have less chance to run.
16
ata Sp Splitting tting: Data may also be split into different sections so that each of which can be processed separately.
a good example.
After partitioning, the two sections can be sorted separately.
After partitioning a[L..U] into a[L..M-1] and a[M+1..U], we may create two threads, one for each section. Then, each thread sorts its own section. Threads are created in a binary tree.
17
ata D a Dep epen ende denc ncy: Watch for data items that are used by different threads. For example, two threads may update a common variable at the same time.
to be sy sync nchr hron
ed so that only one thread can update a shared variable at any time.
programming.
18
esti ting ng and and Deb ebug uggi ging ng: The behavior of a threaded program is dynamic. A bug that appears in this test run may not occur in the
the life-span of a threaded program, or may appear at an unexpected time.
updating a shared resource at the same time, and system deadlock) do not have efficient solutions.
requires a careful design and planning.
19
hrea ead d ca canc ncel ellat ation
thread before its completion. The thread to be cancelled is the target thread.
Asy sync nchr hron
s Can ance cellat ation
thread terminates immediately. Def efer erre red d Can ance cellat ation
thread can periodically check if it should terminate, allowing the target thread an
a cancellation point.
20
thread owns some system-wide resources, the system may not be able to reclaim these recourses because other threads may be using them.
determines the time to terminate itself. Reclaiming resources is not a problem.
processes (e.g., system call kill) and threads.
cancellation.
21
thread-specific.
time requesting for memory from the heap? Or, two printfs are run simultaneously?
properly is a thread-safe one.
22
the very beginning of that function.
coro rout utine ne has multiple entry points and exits so that the next “call” to a coroutine resumes its execution from the statement/instruction following the previous exit point.
entry/exit execution coroutine enter/exit A B C execution flow ABCACBABC
23
scheduler does?
enter is a switching in.
entry/exit execution coroutine enter/exit
Thread 1 Thread 2 Thread 3
in in
24
ber is a lightweight thread just like a thread is a lightweight process.
with other fibers of that thread.
(or local storage) provided when it is created.
and explicitly yields its execution to another fiber with a YIELD or similar function call.
coroutines.
25