Hreads: Program Counter: Registers

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

THREADS

 A thread is a basic unit of CPU utilization, consisting


of a program counter, a stack, and a set of registers,
and a thread ID.
 The thread has a Thread ID:
 program counter :keeps track of next
instruction to be executed.
 registers: which hold its current working
variables.
 Stack: Since thread will generally call different
procedures and thus a different execution history.
This is why thread needs its own stack.
THREADS
Items shared by all Items private to each
processes thread
Address space Program counter
Global variables registers
Resources: Open files stack
Child processes state
Pending alarms

Signals and signal


handlers
Accounting information
THREADS
 Thread can be in any of several states (Running,
Blocked, Ready or Terminated).
 A running thread currently has the CPU and
is active.
 A blocked thread is waiting for some event to
unblock it. For example, when a thread performs
a system call to read from the keyboard, it is
blocked until input is typed.
 A ready thread is scheduled to run and will as
soon as its turn comes up.
 Terminated:
SINGLE AND MULTITHREADED PROCESSES

 Traditional ( heavyweight ) processes have a


single thread. one program counter,
sequential execution.
multi-threaded applications :
multiple threads within a single process, having
their own program counter, each pointing to next
instruction to be executed for a given thread.
 It can perform more then one task at a time.
SINGLE AND MULTITHREADED PROCESSES
MULTITHREADED SERVER ARCHITECTURE

word processor: a thread may check


spelling and grammar while another thread
processes user input ( keystrokes ).
Web server:
Benefits of multithreaded programming

 Responsiveness : One thread may provide rapid


response while other threads are blocked or slowed
down doing intensive calculations. e.g. user responsive
application.
 Resource sharing: By default threads share common
code, data, and other resources, which allows multiple
tasks to be performed simultaneously in a single
address space.
BENEFITS OF MULTITHREADED
PROGRAMMING

Economy : Creating and managing threads


( and context switches between them ) is much
faster than performing the same tasks for
processes.
 e.g. 30% slower process creation then thread &
context switching is about 5 time slower.
Scalability: i.e. Utilization of
multiprocessor architectures - A single threaded
process can only run on one CPU, no matter how
many may be available, whereas multi-thread
may be running in parallel on different
processing cores.
MULTICORE PROGRAMMING
 A recent trend in computer architecture is to
produce chips with multiple cores, or CPUs on a
single chip.
 Concurrency: A multi-threaded application
running on a traditional single-core chip would
have to interleave the threads. On a multi-core
chip, however, the threads could be spread across
the available cores, allowing true parallel
processing. Modern Intel CPU support 2 threads
per core, oracle T4 support 8 threads per core.
Multicore
Programming

Concurrent execution on a single-core


system.

Parallel execution on a
multicore system
TYPES OF PARALLELISM
 Data parallelism : divides the data up amongst
multiple cores ( threads ), and performs the same
task on each subset of the data. E.g. summing
array size N.on dual core sum[0]….[N/2-1] 2nd
core sum [N/2]….[N-1]
 Task parallelism: divides the different tasks to
be performed among the different cores and
performs them simultaneously.
USER MODE AND KERNEL MODE
 When a computer system is executing on behalf
of user application, it is user mode.
 Mode bit=1

 When a user application request a service from


the operating system (via system call) System
must transition from user to kernel mode to
fulfill the request. Mode bit=0
e.g. As system boot, hardware start in kernel mode,
OS is then load &stars user apps in user mode.
USER-LEVEL THREADS
 User-level threads: are implemented by a thread
library at the user level. User threads are supported
above the kernel, without kernel support. These are
the threads that application programmers would put
into their programs.
 ƒExamples:

 ƒPOSIX Pthreads,

 Mach C-threads,

 Solaris 2 UI-threads

 Advantages

ƒContext switching among them is extremely fast


ƒDisadvantages
ƒBlocking of a thread in executing a system call can
block the entire process.
KERNEL-LEVEL THREADS
 Kernel-level threads: Kernel threads are supported
and manage by OS itself. All modern OS support
kernel level threads, allowing the kernel to perform
multiple simultaneous tasks and/or to service multiple
kernel system calls simultaneously.
ƒExamples
ƒWindows 2000, Solaris
 2, True64UNIX

 Advantage

ƒBlocking of a thread will not block its entire task.


 ƒDisadvantage

ƒContext switching cost is a little bit higher because the


kernel must do the switching.
MANY-TO-ONE MODEL

ƒ
Many user-level threads to one kernel thread
ƒ
Advantage:
ƒ
Thread management is handled by the thread
library in user space, which is very efficient.
ƒ
Disadvantage:
ƒ
One blocking system call blocks all.
ƒ
Because a single kernel thread can operate only
on a single CPU, the many-to-one model does not
allow individual processes to be split across
multiple CPUs.
ƒ
Example: Green threads for Solaris 2
Many-to-one model
ONE-TO-ONE MODEL

 ƒ
One user-level thread to one kernel thread
ƒAdvantage: One system call blocks one thread
and the splitting of processes across multiple
CPUs
ƒDisadvantage: Overheads in creating a kernel
thread.
ƒExample: Windows NT, Windows
 2000, OS/2
One-to-One Model
MANY-TO-MANY MODEL

 ƒ
Many user-level threads to many kernel threads
combining the best features of the one-to-one and
many-to-one models
ƒAdvantage:
Users have no restrictions on the number of
threads created.
Blocking kernel system calls do not block the entire
process.
Processes can be split across multiple processors
ƒExample: Solaris 2, IRIX, HP-UX,Tru64 UNIX
Many-to-Many Model
Two-level Model
Similar to previous Many to
Many,
except that it
allows a user thread to
bound to a kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and

You might also like