ACSC 271 Operating Systems Threads Objectives Overview
ACSC 271 Operating Systems Threads Objectives Overview
ACSC 271 Operating Systems Threads Objectives Overview
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Threads</strong><br />
<strong>ACSC</strong> <strong>271</strong><br />
<strong>Operating</strong> <strong>Systems</strong><br />
Lecture 7:<br />
<strong>Threads</strong><br />
• <strong>Overview</strong><br />
• Multithreading Models<br />
• Thread Libraries<br />
• Threading Issues<br />
• <strong>Operating</strong> System Examples<br />
• Windows XP <strong>Threads</strong><br />
• Linux <strong>Threads</strong><br />
Slide 1 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Slide 2 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
<strong>Objectives</strong><br />
• To introduce the notion of a thread — a<br />
fundamental unit of CPU utilization that<br />
forms the basis of multithreaded computer<br />
systems<br />
• To discuss the APIs for the Pthreads,<br />
Win32, and Java thread libraries<br />
• To examine issues related to multithreaded<br />
programming<br />
Slide 3 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
<strong>Overview</strong><br />
• A thread or a lightweight process (LWP) is a<br />
basic unit of CPU utilisation, it comprises a thread<br />
ID, a program counter, a register set, and a stack.<br />
• A thread shares the same code section, data<br />
section, and resources (files, and signals) as other<br />
threads that belong to the same process.<br />
• If a process has multiple threads of control, it can<br />
do more than one task at a a time.<br />
• A traditional heavyweight process has a single<br />
thread of control.<br />
Slide 4 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014
Web browser<br />
In General<br />
Images<br />
Text<br />
Process Control Block (PCB)<br />
Process ID (PID)<br />
Parent PID<br />
…<br />
• This is an abstract view<br />
• Windows implementation of PCB is<br />
split in multiple data structures<br />
Network data<br />
Images<br />
Text<br />
Spelling<br />
Request<br />
Listening<br />
New Thread<br />
Reply<br />
Slide 5 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Next Process Block<br />
List of open files<br />
Image File Name<br />
List of Thread<br />
Control Blocks<br />
…<br />
PCB<br />
Handle Table<br />
Thread Control Block (TCB)<br />
Next TCB<br />
Program Counter<br />
Registers<br />
Slide 6 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
…<br />
CPU Switch from Thread to Thread<br />
Multithreaded Server Architecture<br />
Thread T 1<br />
Interrupt or system call Thread T 2<br />
executing<br />
Save state into TCB 1<br />
Reload state from TCB 2<br />
ready or<br />
waiting<br />
ready or<br />
waiting<br />
Interrupt or system call<br />
Save state into TCB 2<br />
executing<br />
executing<br />
Reload state from TCB 1<br />
ready or<br />
waiting<br />
Slide 7 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Slide 8 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014
Benefits<br />
• Responsiveness – may allow continued execution<br />
if part of process is blocked, especially important<br />
for user interfaces<br />
• Resource Sharing – threads share resources of<br />
process, easier than shared memory or message<br />
passing<br />
• Economy – cheaper than process creation, thread<br />
switching lower overhead than context switching<br />
• Scalability – process can take advantage of<br />
multiprocessor architectures<br />
Slide 9 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Multicore Programming<br />
• Multicore or multiprocessor systems putting pressure on<br />
programmers, challenges include:<br />
– Dividing activities<br />
– Balance<br />
– Data splitting<br />
– Data dependency<br />
– Testing and debugging<br />
• Parallelism implies a system can perform more than one<br />
task simultaneously<br />
• Concurrency supports more than one task making progress<br />
– Single processor / core, scheduler providing concurrency<br />
Slide 10 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Parallelism<br />
Single and Multithreaded<br />
Processes<br />
• Types of parallelism<br />
– Data parallelism – distributes subsets of the same data<br />
across multiple cores, same operation on each<br />
– Task parallelism – distributing threads across cores,<br />
each thread performing unique operation<br />
• As # of threads grows, so does architectural<br />
support for threading<br />
– CPUs have cores as well as hardware threads<br />
• Consider Oracle SPARC T4 with 8 cores, and 8 hardware<br />
threads per core<br />
Slide 11 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Slide 12 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014
Thread Execution<br />
Concurrent Execution – Single Core<br />
Parallel Execution – Multicore<br />
Slide 13 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
User and Kernel <strong>Threads</strong><br />
• Support for threads can be provided at user level<br />
and at kernel level.<br />
• Thread management done by user-level threads<br />
library, for example:<br />
– POSIX Pthreads. – Win32 threads.<br />
– Mach C-threads. – Java threads.<br />
– Solaris threads.<br />
• And supported by the Kernel, for example:<br />
– Windows 95/98/NT/2000. – Solaris.<br />
– Tru64 UNIX. – BeOS.<br />
– Linux.<br />
Slide 14 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Multithreading Models<br />
• Many-to-One: Maps any user-level threads to one<br />
kernel thread. Only one thread can access the<br />
kernel at a time. No multiprocessor support.<br />
• One-to-One: Maps each user thread to a kernel<br />
thread. It provides more concurrency, by allowing<br />
other threads to run in parallel. It offers<br />
multiprocessor support.<br />
• Many-to-Many: Multiplexes many user-level<br />
threads to a smaller or equal number of kernel<br />
threads. It does not suffer from scheduling or<br />
concurrency issues than the other models.<br />
• Two-level: Variation of M-2-M, but also allows a<br />
user-level thread to be bound to kernel-level<br />
thread.<br />
Slide 15 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Many-to-One Model<br />
Examples:<br />
Solaris Green <strong>Threads</strong><br />
GNU Portable <strong>Threads</strong><br />
Slide 16 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014
Examples:<br />
Windows NT/XP/2000<br />
Linux<br />
Solaris 9 and later<br />
One-to-One Model<br />
Many-to-Many Model<br />
Examples:<br />
Solaris prior to version 9<br />
Windows NT/2000 with<br />
the ThreadFiber package<br />
Slide 17 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Slide 18 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Two-level Model<br />
IRIX<br />
HP-UX<br />
Tru64 UNIX<br />
Solaris 8 and earlier<br />
Slide 19 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Threading Issues<br />
• The fork() and exec() system calls are used to create a<br />
separate and duplicate process. In terms of threads the<br />
behave according to how they are called.<br />
• Thread cancellation is the task of terminating a thread<br />
before it has completed. For example a user pressing the<br />
Stop button to stop the web page loading.<br />
• Signal handling: A signal is used in UNIX to notify a<br />
process that a particular event has occurred. For example<br />
illegal memory access or division by zero.<br />
• Thread pools: The idea is to create a number of threads at<br />
process startup, and place them into a pool, where they sit<br />
and wait for work.<br />
• Each thread might need its own copy of certain data in<br />
some cases. This is called thread-specific data.<br />
Slide 20 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014
Thread Cancellation<br />
• Terminating a thread before it has finished<br />
• Two general approaches:<br />
– Asynchronous cancellation terminates the<br />
target thread immediately.<br />
– Deferred cancellation allows the target thread<br />
to periodically check if it should be cancelled.<br />
Signal Handling<br />
• A signal handler is used to process signals<br />
– Signal is generated by particular event<br />
– Signal is delivered to a process Remember:<br />
Ctrl+C<br />
– Signal is handled<br />
Or Ctrl+W<br />
• Options:<br />
Or Alt+F4<br />
– Deliver the signal to the thread to which the signal<br />
applies<br />
– Deliver the signal to every thread in the process<br />
– Deliver the signal to certain threads in the process<br />
– Assign a specific thread to receive all signals for the<br />
process<br />
Slide 21 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Slide 22 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Thread Pools<br />
• The idea of thread pools is to create a number of threads<br />
at process start-up and place them in a pool where they<br />
await work.<br />
• Example: If a server receives a request, it awakens a<br />
thread from the thread pool, if available and passes the<br />
request for service.<br />
• Advantages:<br />
– Usually slightly faster to service a request with an existing thread<br />
than create a new thread<br />
– Allows the number of threads in the application(s) to be bound to<br />
the size of the pool<br />
• Number of threads are based on CPUs, physical memory,<br />
expected number of clients.<br />
Slide 23 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Scheduler Activations<br />
• Both M:M and Two-level models require<br />
communication to maintain the appropriate<br />
number of kernel threads allocated to the<br />
application<br />
• Typically use an intermediate data structure<br />
between user and kernel threads – lightweight<br />
process (LWP)<br />
– Appears to be a virtual processor on which<br />
process can schedule user thread to run<br />
– Each LWP attached to kernel thread<br />
– How many LWPs to create?<br />
• Scheduler activations provide upcalls - a<br />
communication mechanism from the kernel to<br />
the upcall handler in the thread library<br />
• This communication allows an application to<br />
maintain the correct number kernel threads.<br />
Slide 24 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014
Pthreads<br />
• A Portable <strong>Operating</strong> System Interface part<br />
X (POSIX) standard (IEEE 1003.1c)<br />
Application Programming Interface (API)<br />
for thread creation and synchronization.<br />
• API specifies behaviour of the thread<br />
library, implementation is up to<br />
development of the library.<br />
• Common in UNIX operating systems.<br />
• Windows does not support Pthreads.<br />
Slide 25 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Windows <strong>Threads</strong><br />
• What is a thread?<br />
– An execution context within a process<br />
– Unit of scheduling (threads run, processes don’t run)<br />
– All threads in a process share the same per-process<br />
address space<br />
• Services provided so that threads can synchronize access to<br />
shared resources (critical sections, mutexes, events, semaphores)<br />
– All threads in the system are scheduled as peers to all<br />
others, without regard to their “parent” process<br />
• System calls<br />
– Primary argument to CreateProcess()<br />
is image file name (or command line)<br />
– Primary argument to CreateThread()<br />
is a function entry point address<br />
Slide 26 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Windows <strong>Threads</strong><br />
• Windows implements the Windows API – primary API for<br />
Win 98, Win NT, Win 2000, Win XP, and Win 7<br />
• Implements the one-to-one mapping, kernel-level<br />
• Each thread contains<br />
– A thread id<br />
– Register set representing state of processor<br />
– Separate user and kernel stacks for when thread runs in user<br />
mode or kernel mode<br />
– Private data storage area used by run-time libraries and dynamic<br />
link libraries (DLLs)<br />
• The register set, stacks, and private storage area are<br />
known as the context of the thread<br />
Slide 27 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Windows <strong>Threads</strong><br />
• Implements the one-to-one mapping.<br />
• Each thread contains:<br />
– A thread id.<br />
– Register set.<br />
– Separate user and kernel stacks.<br />
– Private data storage area.<br />
• The register set, stacks, and private storage area<br />
are known as the context of the threads<br />
• The primary data structures of a thread include:<br />
– ETHREAD (executive thread block)<br />
– KTHREAD (kernel thread block)<br />
– TEB (thread environment block)<br />
Slide 28 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014
Windows <strong>Threads</strong><br />
Linux <strong>Threads</strong><br />
• Linux refers to them as tasks rather than<br />
threads.<br />
• Thread creation is done through clone()<br />
system call.<br />
• clone() allows a child task to share the<br />
address space of the parent task (process).<br />
Slide 29 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Slide 30 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Linux <strong>Threads</strong><br />
Java <strong>Threads</strong><br />
• Java threads are managed by the JVM<br />
• Java threads may be created by:<br />
– Extending Thread class.<br />
– Implementing the Runnable interface.<br />
Slide 31 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014<br />
Slide 32 C.Markides <strong>ACSC</strong> <strong>271</strong>: <strong>Operating</strong> <strong>Systems</strong> 09/11/2014