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

Chapter2-Process Managment(Threads)-partII

The document provides a comprehensive 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 responsiveness, resource sharing, and efficiency, along with threading issues like cancellation and signal handling. Additionally, it outlines different threading models like many-to-one, one-to-one, and many-to-many, detailing their implementations and use cases.

Uploaded by

fikadu.meu.edu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter2-Process Managment(Threads)-partII

The document provides a comprehensive 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 responsiveness, resource sharing, and efficiency, along with threading issues like cancellation and signal handling. Additionally, it outlines different threading models like many-to-one, one-to-one, and many-to-many, detailing their implementations and use cases.

Uploaded by

fikadu.meu.edu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

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 for 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.
o The unit of dispatching is usually referred to a Thread or a Light-Weight
Process (LWP).
3. .

Sem II,2011-IT3106 for 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 on
computer systems that have multiple CPUs.
Sem II,2011-IT3106 for groups G5-G8 3
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 for 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 for groups G5-G8 5
Thread Execution Time line

Pa
g
in

ra
lex

lle
tip

Exl
ul

Thread #2

ec
m

uti
n

Thread #1

on
io

Thread #3
s
ivi
eD
Tim

a)
Thread execution Vs Timeline. b)
a) Uniprocessor system
b)multiprocessor system
Sem II,2011-IT3106 for 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 for 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.
 Threaded applications often run faster than non-threaded
applications (as context-switches between kernel and user-
space are avoided).
Sem II,2011-IT3106 for groups G5-G8 8
Examples of benefits of threads

• 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.

Sem II,2011-IT3106 for groups G5-G8 9


Thread usage example: Web Server

Sem II,2011-IT3106 for groups G5-G8 10


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

(a) return_page(&page);
}
(b)

(a) Dispatcher thread


(b) Worker thread
Sem II,2011-IT3106 for groups G5-G8 11
Single and Multithreaded Processes

Sem II,2011-IT3106 for 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.

h re ad ed
t
Multi ecture
t
archi
Sem II,2011-IT3106 for groups G5-G8 13
Combinations of Threads and Processes

Sem II,2011-IT3106 for 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.
 Threads of a process may be run on different processors -> real
parallelism.
o Utilization of networked/distributed systems
Sem II,2011-IT3106 for 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 for 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 for groups G5-G8
Implementing Threads in User Space

Sem II,2011-IT3106 for 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 for groups G5-G8


Advantages and inconveniences of ULT

Advantages Inconveniences
 Thread switching does  Most system calls are
not involve the kernel: blocking and the kernel
no mode switching. blocks processes. So all
 Scheduling can be threads within the process
application specific: will be blocked.
choose the best  The kernel can only assign

algorithm. processes to processors.


Two threads within the
 ULTs can run on any OS.
same process cannot run
Only needs a thread
simultaneously on two
library. Sem II,2011-IT3106 for 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 for groups G5-G8


Implementing Threads in the Kernel

The following are some of the OSs which support kernel-Level threads:
Windows XP/2000 Tru64 UNIX
Solaris Mac OS X
Linux
Sem II,2011-IT3106 for groups G5-G8 22
Advantages and inconveniences of KLT

Advantages Inconveniences
 The kernel can  thread switching
simultaneously schedule
within the same
many threads of the same
process on many
process involves the
processors. kernel. We have 2
 blocking is done on a mode switches per
thread level. thread switch.
 kernel routines can be  This results in a
multithreaded. significant slow
Sem II,2011-IT3106 for groups G5-G8 23
down.
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 for groups G5-G8
Multithreading Models

 Many-to-One
 One-to-One
 Many-to-Many
 Two-level Model

Sem II,2011-IT3106 for 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 for 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 for 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 for groups G5-G8 28
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 for 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 for groups G5-G8 30


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 for groups G5-G8 31


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 for groups G5-G8 32
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:
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 for groups G5-G8 33
Scheduler Activations
 Both Many-to-Many and Two-level models require communication to maintain
the appropriate number of kernel threads allocated to the application.
 Scheduler activations 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 for groups G5-G8 34


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 for groups G5-G8 35


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 for groups G5-G8 36

You might also like