0% found this document useful (0 votes)
11 views

Chapter 2 Part II Threads

The document provides an overview of threads, their characteristics, benefits, and various multithreading models, including user-level and kernel-level threads. It discusses the advantages of multithreading, such as improved responsiveness and resource sharing, and highlights examples of applications that benefit from multithreading. Additionally, it addresses threading issues and the semantics of system calls related to threads.

Uploaded by

fikadu.meu.edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Chapter 2 Part II Threads

The document provides an overview of threads, their characteristics, benefits, and various multithreading models, including user-level and kernel-level threads. It discusses the advantages of multithreading, such as improved responsiveness and resource sharing, and highlights examples of applications that benefit from multithreading. Additionally, it addresses threading issues and the semantics of system calls related to threads.

Uploaded by

fikadu.meu.edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Threads

o Overview
o Thread Characteristics and Benefits
o Multithreading Models
o Threading Issues
o Pthreads
o Windows XP Threads
o Linux Threads
o Java Threads

Sem II,2011-IT3106 groups G5-G8 1


Overview
Process Characteristics
 Each process has the following two characteristics:
1. Unit of resource ownership:- When a new process is
created , an address space 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 :- The other concept a process has
is a thread of execution, usually shortened to just
thread.
o The thread has a program counter that keeps track of
which instruction to execute next.
o It has registers, which hold its current working
variables.
o It has a stack, which contains the execution history,
with one frame for each procedure called but not yet
returned from.
Sem II,2011-IT3106 groups G5-G8 2
Overview(cont)
o A thread of execution is the smallest unit of
processing that can be scheduled by an
operating system.
o A thread is the basic unit of CPU utilization.
o Multiple threads can exist within the same process
and share resources such as memory, while
different processes do not share these resources.
o A process having multiple threads of control can
perform more than one task at a time.
o On a single processor, multithreading generally
occurs by time-division multiplexing .
o On a multiprocessor or multi-core system, the
threads or tasks will actually run at the same time,
with each processor or core running a particular
thread or task.
o a multithreaded program allows it to operate faster
Sem II,2011-IT3106 groups G5-G8 3
on computer systems that have multiple CPUs.
Contd.
 Several threads can exist in the same process.
 A traditional Heavy-Weight Process is equal to a task with
one thread.
 Task and Process terms are used interchangeably.
 Process Items (shared by all threads of a process):
― address space which holds the process image.
― global variables.
― Child Processes
― Pending alarms
― Signals and signal Handlers
― protected access to Open files, I/O and other resources.
― Accounting Information
 Thread Items:
― execution state (running, ready, etc.)
― program counter
― Register set
― execution stack

Sem II,2011-IT3106 groups G5-G8 4


The Process vs. Thread Model

(a) Three processes each with one thread.


(b) One process with three threads.

Sem II,2011-IT3106 groups G5-G8 5


Thread Execution Time line

Pa
ra
ll e
lE
xe
Thread
ng n
xi io

cu
#2
le is

Thread

ti
Thread
tip iv

on
#1
ul D

#3
m me
Ti

a)
Thread execution Vs Timeline. b)
a) Uniprocessor system
b)multiprocessor system
Sem II,2011-IT3106 groups G5-G8 6
Thread Characteristics
 Like processes each thread has an execution context/state.
 Has an execution stack and some per-thread static storage
for local variables.
 Has access to the memory address space and resources of
its task:
 All threads of a task share the same memory.
 When one thread alters a (non-private) memory item, all
other threads (of the task) see that.
 A file open with one thread, is available to others.
 Threads has three key states: running, ready, blocked
 They have no suspend state because all threads within
same task share the same address space.
 Indeed: suspending (i.e., swapping) a single thread
involves suspending all threads of the same task.
 Termination of a task, terminates all threads within the
task.
Sem II,2011-IT3106 groups G5-G8 7
Benefits of Threads
o Less time to create and terminate a thread than
a process (because we do not need another
address space).
o Less time to switch between two threads than
between processes.
o Inter-thread communication and synchronization
is very fast.
o Since threads within the same process share
memory and files, they can communicate with
each other without invoking the kernel.
 Threads allows parallel activity inside a single
address space:
 While one server thread is blocked and
waiting, a second thread in the same task can
run. Sem II,2011-IT3106 groups G5-G8 8
Examples showing multi-threading is giving improved performance
over single threading solution

 Example 1: Word Processor:


 one thread displays menu and reads user input while
another thread executes user commands and a third
one writes to disk – the application is more
responsive.
 Example 2: a File/Web server on a LAN:
 It needs to handle several files/pages requests over a
short period.
 Hence more efficient way is to create (and destroy) a
single thread for each request.
 On a SMP machine: multiple threads can possibly be
executing simultaneously on different processors.
 Example 3:-A parallelized application such as matrix
multiplication where different parts of the matrix may be
worked on in parallel.
 Example4:-An Seminteractive
II,2011- IT3106 GUI
groups program
G5-G8 such as a 9
Thread usage example: Web Server

Sem II,2011-IT3106 groups G5-G8 10


Web Server Threads
while(true)
while(true)
{
{
wait_for_work(&buf);
look_for_page_in_cache(&buf,
get_next_request(&buf
&page);
);
if(page_not_in_cache(&page)
handoff_work(&buf);
(a) read_page_from_disk(&buf, &page);
}
return_page(&page);
(b)
}

(a) Dispatcher thread


(b) Worker thread

Sem II,2011-IT3106 groups G5-G8 11


Single and Multithreaded Processes
each thread has its own program counter, registers,
stack, and state; but all threads of a process share
address space, global variables and other resources
such as open files, etc

Sem II,2011-IT3106 groups G5-G8 12


Multithreading vs. Single threading
 Single threading: when the OS does not recognize
the concept of thread.
 Multithreading: when the OS supports multiple
threads of execution within a single process.
 MS-DOS supports a single user process and a single
thread.
 Older UNIXs supports multiple user processes but only
support one thread per process.
 Solaris and Windows NT support multiple threads.

r eaded
ultith ture
M itec
ar c h
Sem II,2011-IT3106 groups G5-G8 13
Combinations of Threads and Processes

Sem II,2011-IT3106 groups G5-G8 14


Benefits of Multithreading
o Responsiveness:-multithreading an interactive
application may allow a program to continue running even
if part of it is blocked or is performing a lengthy operation,
thereby increasing responsiveness to the user. For
instance, a web browser could still allow user interaction in
one thread while an image was being loaded in another
thread.
o Resource Sharing:-threads share memory and
resources of a process in which they belong. Sharing
allows an application to have several different threads
activity within the same address space.
o Economy:-allocating memory and resources for process
creation is costly. But because thread shares resources it
reduce resource consumption. It is much more time
consuming , for creating and managing resources than
treads.
o Maximizes utilization of multiprocessor
architectures.Sem II,2011-IT3106 groups G5-G8 15
Multithreading Levels
o Thread library provides programmer with API for
creating and managing threads.
o Three multithreading levels:
1) User-Level Threads (ULT)
 Library entirely in user space.
2) Kernel-Level Threads (KLT)
 Kernel-level library supported by the OS.
3) Hybrid ULT/KLT Approach
oThree primary thread libraries are in use today:
 POSIX Pthreads
 Win32 threads
 Java threads
Sem II,2011-IT3106 groups G5-G8
1) User-Level Threads (ULT)
 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.
Sem II,2011-IT3106 groups G5-G8
Implementing Threads in User Space

Sem II,2011-IT3106 groups G5-G8 18


ULT Idea

 Thread management done by user-level threads library.


 Threads library contains code for:
 creating and destroying threads.
 passing messages and data between threads.
 scheduling thread execution.
 saving and restoring thread contexts.

Kernel activity for ULTs


 The kernel is not aware of thread activity but it is still
managing process activity.
 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.
 So thread states are independent of process states.

Sem II,2011-IT3106 groups G5-G8


Advantages and inconveniences of ULT
Advantages Inconveniences
 Thread switching  Most system calls are blocking and

does not involve the the kernel blocks processes. So all


threads within the process will be
kernel: no mode
blocked.
switching.  The kernel can only assign
 Scheduling can be processes to processors. Two
threads within the same process
application specific: cannot run simultaneously on two
choose the best processors( a multithreaded
algorithm. application cannot take advantage
of multiprocessing)
 ULTs can run on any
OS. Only needs a
thread library.
Sem II,2011-IT3106 groups G5-G8 20
2) Kernel-Level Threads (KLT)
 All thread management is done
by kernel.
 No thread library but an API to
the kernel thread facility.
 Kernel maintains context
information for the process and
the thread.
 Switching between threads
requires the kernel.
 Threads are supported by
kernel.
 Scheduling on a thread basis.

Sem II,2011-IT3106 groups G5-G8


Implementing Threads in the Kernel

The following are some of the OSs which support kernel-


Level threads: XP/2000
Windows Tru64 UNIX
Solaris Mac OS X
Linux
Sem II,2011-IT3106 groups G5-G8 22
Advantages and inconveniences of KLT

Advantages Inconveniences
 The kernel can  thread switching
simultaneously within the same
schedule many process involves
threads of the same the kernel. We have
process on many
2 mode switches
processors.
per thread switch.
 blocking is done on a
 This results in a
thread level.
 kernel routines can be
significant slow
multithreaded.
down.

Sem II,2011-IT3106 groups G5-G8 23


3) Hybrid ULT/KLT Approaches
Thread creation done in
the user space.
Bulk of scheduling and
synchronization of threads
done in the user space.
The programmer may
adjust the number of KLTs.
May combine the best of
both approaches.
Example is Solaris prior to
version 9.
Sem II,2011-IT3106 groups G5-G8
 The differences between user-level threads and kernel-level
threads are :
 User-level threads are unknown by the kernel, whereas
the kernel is aware of kernel threads.
 User threads are scheduled by the thread library and
the kernel schedules kernel threads.
 Kernel threads need not be associated with a process
whereas every user thread belongs to a process.
 What resources are used when a thread is created? How do
they differ from those used when a process is created?
 Because a thread is smaller than a process, thread
creation typically uses fewer resources than process
creation. Creating a process requires allocating a
process control block (PCB), a rather large data
structure. The PCB includes a memory map, list of open
files, and environment variables. Allocating and
managing the memory map is typically the most time-
consuming activity. Creating either a user or kernel
thread involves allocating
Sem II,2011-IT3106 a small
groups G5-G8 data structure to hold
Multithreading Models
user
w do el
Ho kern ap
and ads m
e
 Many-to-One thr each
into r?
 One-to-One e
oth
 Many-to-Many

 Two-level Model

Sem II,2011-IT3106 groups G5-G8


Many-to-One Model
 Many user-level threads
mapped to single kernel-level
thread.
 Thread management is done
in user space, so it’s efficient.
 Because only one thread can
access the kernel at a time,
multiple threads are unable to
run in parallel on
multiprocessors.
 Used by ULT Libraries on
systems that do not support
kernel threads (KLT).
 Examples:
 Solaris Green Threads
 GNU Portable Threads

Sem II,2011-IT3106 groups G5-G8


One-to-One Model
 Each user-level thread maps
to kernel-level thread
 Provides more concurrency
than many-to-one model:
Allows another thread to run
when a thread is blocked.
Allows multiple threads to
run in parallel on
multiprocessors.
 Creating a user thread
requires though creation of a
corresponding kernel thread.
 Examples:
 Windows NT/XP/2000
 Linux
 Solaris 9 and later

Sem II,2011-IT3106 groups G5-G8


Many-to-Many Model
 Allows many user-level threads
to be mapped to a smaller or
equal number of kernel-level
threads.
 Allows the operating system to
create a sufficient number of
kernel threads (specific to
either a particular application
or a particular machine) – most
flexible.
 Examples:
 Solaris prior to version 9
 Windows NT/2000 with the
ThreadFiber package

Sem II,2011-IT3106 groups G5-G8 29


Two-level Model
 Similar to Many-to-Many model, except that it
allows a user thread to be bound to kernel
thread
 Examples:
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier

Sem II,2011-IT3106 groups G5-G8


Threading Issues
o Semantics of fork() and exec() system
calls
o Thread cancellation
o Signal handling
o Thread pools
o Thread specific data
o Scheduler activations

Sem II,2011-IT3106 groups G5-G8 31


Semantics of fork() and exec() system calls

o If one thread in a program call the fork() system


call, does fork() duplicate only the calling thread
or all threads?
o Two approaches in designing fork
― the first type duplicates only the calling thread
―The second type duplicates all treads in a process
o The exec() system call works in the usual way as
presented previously
o If a thread invokes the exec() system call , the
program specified in the parameter to exec() will
replace the entire process-including all treads

Sem II,2011-IT3106 groups G5-G8 32


Thread Cancellation
 Terminating a thread before it has finished.
 Two general approaches:
 Asynchronous cancellation terminates the target thread
immediately.
 Deferred cancellation allows the target thread to
periodically check if it should be cancelled.
Thread Pools
 Create a number of threads in a pool where they await work.

 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.
Sem II,2011-IT3106 groups G5-G8 33
Signal Handling
 Signals are used in UNIX systems to notify a process
that a particular event has occurred.
 A signal handler is used to process signals:Steps
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
 Options:
 Deliver the signal to the thread to which the signal
applies
 Deliver the signal to every thread in the process
 Deliver the signal to certain threads in the process
 Assign a specific thread to receive all signals for the
process
Sem II,2011-IT3106 groups G5-G8 34
Scheduler Activations
 Both Many-to-Many and Two-level models require
communication to maintain the appropriate number of kernel
threads allocated to the application.
 Scheduleractivations provide upcalls – a communication
mechanism from the kernel to the thread library.
 This communication allows an application to maintain the
correct number kernel threads.

Pthreads
• A POSIX standard (IEEE 1003.1c) API for thread
creation and synchronization
• API specifies behavior of the thread library,
implementation is up to development of the library
• Common in UNIX operating systems (Solaris, Linux,
Mac OS X)
Sem II,2011-IT3106 groups G5-G8 35
Windows XP Threads
Implements the one-to-one mapping
Each thread contains
A thread id
Register set
Separate user and kernel stacks
Private data storage area
The register set, stacks, and private storage area
are known as the context of the threads
The primary data structures of a thread include:
ETHREAD (executive thread block)
KTHREAD (kernel thread block)
TEB (thread environment block)

Sem II,2011-IT3106 groups G5-G8 36


Linux Threads
o Linux refers to them as tasks rather than threads
o Thread creation is done through clone() system
call
o clone() allows a child task to share the address
space of the parent task (process)
Java Threads
 Java threads are managed by the JVM.
 Typically implemented using the threads model
provided by underlying OS.
 Java threads may be created by:
 Extending Thread class (at language-level)
 Implementing the Runnable interface

Sem II,2011-IT3106 groups G5-G8 37

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy