Professional Documents
Culture Documents
06 Threads
06 Threads
06 Threads
Most modern applications are multithreaded
Threads run within application
Operating Systems I
Multiple tasks with the application can be implemented
by separate threads
Update display
Fetch data
Unit 6 – Threads
Spell checking
Answer a network request
Prof. Dr. Alejandro Zunino
Process creation is heavy-weight while thread creation
is light-weight
ISISTAN - CONICET
Can simplify code, increase efficiency
Kernels are generally multithreaded
Overview
Multithreading Models
Threading Issues
Thread programming
Protected access to processors, other
processes, files, and I/O resources
Thread Threads (Cont.)
In a multiple threaded task, while one server thread is
An execution state (running, ready, etc.) blocked and waiting, a second thread in the same task can
run.
Saved thread context when not running
Cooperation of multiple threads in same job confers higher
throughput and improved performance.
Has an execution stack
Applications that require sharing a common buffer (i.e.,
producer-consumer) benefit from thread utilization.
Some per-thread static storage for local
Threads provide a mechanism that allows sequential
variables processes to make blocking system calls while also
achieving parallelism.
Access to the memory and resources of its
Kernel-supported threads (Mach, Solaris 2, Linux, NT).
process
User-level threads; supported above the kernel, via a set of
library calls at the user level (Project Andrew from CMU).
all threads of a process share this
Hybrid approach implements both user-level and kernel-
supported threads (Solaris 2, Linux).
Multicore or multiprocessor systems putting pressure on
A thread (or lightweight process) is a basic
programmers, challenges include: unit of CPU utilization; it consists of:
Dividing activities
program counter
Balance
register set
Data splitting
stack space
Data dependency
Testing and debugging
A thread shares with its peer threads its:
Parallelism implies a system can perform more than
code section
one task simultaneously
data section
Concurrency supports more than one task making
operating-system resources
progress collectively know as a task.
Single processor / core, scheduler providing
A traditional or heavyweight process is equal
concurrency to a task with one thread
Many-to-One
Thread management done by user-level
threads library
One-to-One
The kernel is not aware of the existence of
threads
Many-to-Many
Thread creation done in the user space
Many user-level threads mapped to single
kernel thread.
One thread blocking causes all to block
Bulk of scheduling and synchronization of
Used on systems that do not support kernel
threads done in the user space threads or for specific needs
One-to-One Thread Programming
Each user-level thread maps to kernel
Usually through a thread library:
thread
provides programmer with API for creating and
More concurrency than many-to-one managing threads
Number of threads per process sometimes
Two primary ways of implementing
restricted due to overhead
Library entirely in user space
Examples:
Kernel-level library supported by the OS
Windows
Linux
Allows many user level threads to be
mapped to many kernel threads.
Allows the operating system to create a
sufficient number of kernel threads.
Hard to implement
Requires a user level thread scheduler
May be provided either as user-level or
Java threads are managed by the JVM
kernel-level
Typically implemented using the threads
A POSIX standard (IEEE 1003.1c) API for model provided by underlying OS
thread creation and synchronization.
Java threads may be created by:
Extending Thread class
API specifies behavior of the thread library,
Implementing the Runnable interface
implementation is up to development of
the library.
Common in UNIX operating systems.
Java Threads A simple Web server
Runnable
Implementing Runnable <<interface>>