Chapter 2 Process Management Part 2 Threads and Multithreading
Chapter 2 Process Management Part 2 Threads and Multithreading
Part Two
Threads and Multithreading
Operating Systems
1 (SEng 2043)
2043)
Objective
At the end of this session students will be able to:
computer systems.
Models
Threading Issues
If there are many processes sharing many resources, then the mechanism
becomes cumbersome- difficult to handle.
Threads are created to make this kind of resource sharing simple & efficient
Each process has the following two characteristics:
containing program text and data , as well as other resources(files, child processes,
pending alarms, signal handlers, accounting information) is allocated.
The unit of resource ownership is usually referred to as a Task or a Process .
2. Unit of dispatching:- is a thread of execution, usually shortened to just thread.
The thread has a program counter, that keeps track of which instruction to
execute next.
3
It has registers, which hold its current working variables.
Contd.
It has a stack, which contains the execution history, with one frame for each
procedure called but not yet returned from.
The unit of dispatching is usually referred to a Thread or a Light-Weight Process
(LWP).
A thread is a basic unit of CPU utilization that consists of:
Thread id
Execution State
Program counter
Register set
Stack
Threads belonging to the same process share:
its code
its data section
4
other OS resources such as open files and
Process Vs. Thread
A thread of execution is the smallest unit of processing that can be scheduled by an OS.
The implementation of threads and processes differs from one OS to another, but in
most cases, a thread is contained inside a process.
Multiple threads can exist within the same process and share resources such as
memory, while different processes do not share these resources.
Like process states, threads also have states:
New, Ready, Running, Waiting and Terminated
Like processes, the OS will switch between threads (even though they belong to a
single process) for CPU usage.
Like process creation, thread creation is supported by APIs
Creating threads is inexpensive (cheaper) compared to processes
They do not need new address space, global data, program code or
operating system resources
Context switching is faster as the only things to save/restore are program
5
counters, registers and stacks
Contd.
Similarities Difference
Both share CPU and only one Unlike processes, threads are not
1. POSIX pthreads
They may be provided as either a user or kernel library, as an extension to
the POSIX standard
Systems like Solaris, Linux and Mac Oss implement pthreads
specifications
2. WIN32 threads
These are provided as a kernel-level library on Windows systems.
3. Java threads
Since Java generally runs on a Java Virtual Machine (JVM), the
implementation of threads is based upon whatever OS and hardware the
JVM is running on i.e. either Pthreads or Win32 threads depending on the
system.
On Windows systems, Java threads are typically implemented using the
9
Win32 API whereas UNIX and Linux systems often use Pthreads.
Examples of Threads
In a word processor,
A background thread may check spelling and grammar, while a
foreground thread processes user input (keystrokes), while yet a third
thread loads images from the hard drive, and a fourth does periodic
automatic backups of the file being edited
In a spreadsheet program,
one thread could display menus and read user input, while another thread
executes user commands and updates the spreadsheet
In a web server,
Multiple threads allow for multiple requests to be satisfied simultaneously,
without having to service requests sequentially or to fork off separate
processes for every incoming request
10
Thread usage example: Web Server
handoff_work(&buf); if(page_not_in_cache(&page)
read_page_from_disk(&buf, &page);
}
return_page(&page);
}
There’s one program counter and a set of instructions carried out at a time
If a process has multiple thread of control, it can perform more than one task
at a time
Each threads have their own program counter, stacks and registers
But they share common code, data and some operating system data
13
structures like files
Multitasking Vs. Multithreading
Multitasking is the ability of an OS to execute more than one program
simultaneously.
Though we say so but in reality no two programs on a single processor
machine can be executed at the same time.
The CPU switches from one program to the next so quickly that appears as if all of
the programs are executing at the same time.
Multithreading is the ability of an OS to execute the different parts of the
program, called threads, simultaneously.
The program has to be designed well so that the different threads do not interfere
with each other.
Individual programs are all isolated from each other in terms of their memory and
data, but individual threads are not as they all share the same memory and data
variables.
Hence, implementing multitasking is relatively easier in an operating
14
system than implementing multithreading.
Contd.
Traditionally there is a single thread of execution per process.
Older UNIX supports multiple user processes but only support one thread
per process.
Multithreading
multiple threads.
and OS/2
15
Contd.
its PCB, user address space, as well as user and kernel stacks
that process, and the contents of these registers are saved when the process is
not running.
In a multithreaded environment
However, there are separate stacks for each thread as well as separate
control blocks for each thread containing register values, priority, and
17
other thread related state information.
Contd.
Concurrent execution
on a single-core system
Parallel execution on a
multicore system
20 Fig. 2.2.5 Concurrent Execution of threads of a processes on Single and Multiple processors
Challenges in programming for multicore systems
divided into separate, concurrent tasks and thus can run in parallel on
individual cores
2. Balancing:- While identifying tasks that can run in parallel, programmers must
also ensure that the tasks perform equal work of equal value.
In some instances, a certain task may not contribute as much value to the
overall process as other tasks; using a separate execution core to run that
task may not be worth the cost.
3. Data Splitting:- Just as applications are divided into separate tasks, the data
21 accessed and manipulated by the tasks must be divided to run on separate cores.
Contd.
4. Data Dependency:- The data accessed by the tasks must be examined for
22
difficult than testing and debugging single-threaded applications.
Multithreading Models
There are three types of multithreading models in modern operating systems:
1. Kernel Level threads:- are supported by the OS kernel itself.
All modern OS support kernel threads
Need user/kernel mode switch to change threads
2. User Level threads:- are threads application programmers put in their programs.
They are managed without the kernel support
Has problems with blocking system calls
Cannot support multiprocessing
2. Hybrid Level threads:- the combination of both Kernel and User level threads.
There must be a relationship between the kernel threads and the user threads.
There are three common ways to establish this relationship:
A. Many-to-One
B. One-to-One
23
C. Many-to-Many
1. User level Threads (ULTs)
The kernel is not aware of the existence of threads.
All thread management is done by the application by using a thread library.
Thread switching does not require kernel mode privileges (no mode switch).
Scheduling is application specific.
When a thread makes a system call, the whole task will be blocked.
But for the thread library that thread is still in the running state.
25
So thread states are independent of process states.
Advantages and inconveniences of ULT
Advantages Inconveniences
Most system calls are blocking
Thread switching does not involve the
and the kernel blocks processes.
kernel: no mode switching. So all threads within the process
will be blocked.
Scheduling can be application specific: The kernel can only assign
processes to processors.
choose the best algorithm.
Two threads within the same
Advantages Inconveniences
The kernel can simultaneously schedule
thread switching within the same
space.
number of KLTs.
approaches.
of kernel threads
Reading Assignment
42 Windows XP Threads Vs. Linux Threads