System Programming - II Threads
System Programming - II Threads
System Programming - II Threads
Chapter 5
Threads
Threads
• Overview
• Multithreading Models
• Thread Scheduling
• Threading Issues
• P-threads
• Solaris 2 Threads
• Windows 2000 Threads
• Linux Threads
• Java Threads
2
Overview
3
Overview
6
Single and Multithreaded Processes
7
Motivation
A Process with Several Threads of Control.
• Web Browser
– Displaying context while Retrieving Data from the network.
• Word Processor
– A thread for displaying graphics, another thread for reading
keystrokes, and a third thread for performing spelling checking.
An application performs several similar tasks efficiently.
• Web Server
– When several clients are accessing a web server, the server accepts a
client request and creates a separate thread serving the request.
– If separate processes are created instead, process creation and
management overhead follows.
• RPC
– Serving several concurrent client requests.
8
Thread Usage
12
Multicore Programming
• Multicore systems putting pressure on
programmers, challenges include
– Dividing activities
– Balance
– Data splitting
– Data dependency
– Testing and debugging
Multithreaded Server Architecture
Concurrent Execution on a Single-core System
Parallel Execution on a Multicore System
User Threads
• Supported above the kernel and are implemented by thread
library at the user level.
• Simple Representation:
Each thread is represented simply by a PC, registers, stack
and a small control block, all stored in the user process
address space.
• Simple Management:
Means that creating a thread, switching between threads
and synchronization between threads can all be done
without intervention of the kernel.
• Fast and Efficient:
Thread switching is not much more expensive than a
procedure call.
18
User Threads Disadvantages
19
Kernel Threads
• Supported directly by the OS.
• Examples
- Windows 95/98/NT/2000, Solaris, Tru64, UNIX, BeOS,
Linux 20
Kernel Threads Advantages
21
Kernel Threads Disadvantages
22
Multithreading Models
Many-to-One
24
Multithreading Models (Contd)
One-to-One
• Each user-level thread maps to kernel thread.
• Drawback:
• Creating a user thread requires creating the corresponding
kernel thread.
• Overhead of Thread Creation and Management
• Examples
- Windows 95/98/NT/2000, OS/2
25
One-to-one Model
26
Many-to-Many Model
• Multiplexes many user level threads to a smaller or equal
number of kernel threads.
• Example:
– Solaris 2, Windows NT/2000 with the ThreadFiber
package
27
Many-to-Many Model
28
Thread Scheduling
• There are two main approaches to the scheduling of threads.
• Preemptive scheduling:
– Thread may be suspended to make way for another
thread, even it is in run able condition.
• Non-preemptive scheduling:
– Coroutine scheduling, thread runs until it makes a call that
causes it to be rescheduled and another thread to be run.
30
Threading Issues (Contd)
Semantics of fork() and exec() system calls.
• If one thread calls fork( ), does the new process duplicate all
threads or is the new process single-threaded?
• Example: When a user press the stop button on a web browser, the
thread loading a web page should be cancelled.
• Asynchronous Cancellation
– One thread immediately terminates the target thread.
• Deferred Cancellation
– The target thread can periodically check if it should terminate.
32
Threading Issues (Contd)
Signal handling.
• A signal is used in UNIX systems to notify a process that a
particular event has occurred.
34
Threading Issues (Contd)
Signal handling (Contd).
35
Threading Issues (Contd)
Thread pools.
Create a number of threads in a pool where they await work
• Motivation
– A multi-threaded server may spend too much time in
creating and deleting many threads.
– A large number of (unlimited) threads could exhaust
system resource such as CPU time and memory.
• Solution
– To create a number threads at process startup and place
them into a pool, where they sit and wait for job.
– When a server receives a request, it awakens a thread
from the pool. And, the thread returns to poll upon
completing the job.
– If no available thread in the pool, the server waits until
one becomes available. 36
Threading Issues (Contd)
Thread pools (Contd).
• Benefits
– It is faster to service a request with an existing thread
than waiting to create a new thread.
– A thread pool limits the number of threads that exist at
any one point.
37
Threading Issues (Contd)
Thread pools (Contd).
• Advantages:
– Usually slightly 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
Threading Issues (Contd)
Thread - Specific Specific Data.
• Threads share the data of the process.
– One of the benefits of multi-threaded programming
42
Solaris 2 Threads
• Solaris 2 is a version of UNIX.
• It supports
– Kernel Threads,
– User level Threads, and
– An intermediate level of threads, lightweight process
(LWP).
• Each process contains at least one LWP. The thread library
multiplexes user-level threads on the pool of LWP, and only
the user-level threads currently connected to an LWP
accomplish work.
• Each LWP has a kernel thread, and some kernel threads
have no associated LWP.
• Many-to-Many Model
43
Solaris 2 Threads
44
Solaris 2 Threads
Data Structure for Implementation
• User level thread contains a thread ID, register set, stack,
and priority.
• An LWP has a register set for the user-level thread it is
running, as well as memory and accounting information. An
LWP is a kernel data structure.
• A kernel thread has only a small data structure and a stack.
The data structure includes a copy of kernel registers, a
pointer to LWP to which it is attached, and priority and
scheduling information.
• Solaris Process
Has a conventional PCB.
45
Solaris Process
46