0% found this document useful (0 votes)
6 views28 pages

Session 10 Threads (Autosaved)

Uploaded by

juma
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)
6 views28 pages

Session 10 Threads (Autosaved)

Uploaded by

juma
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/ 28

TVET – CDACC

COMPETENCY BASED EDUCATION AND


TRAINING.

COURSE: COMPUTER SCIENCE LEVEL 6


UNIT: OPERATING SYSTEMS

THREADS

Trainer: Geoffrey Juma


08/12/2024 04:05 PM 1
Developing Character, Skills and Competence Compil
ed by G.B.Juma
Threads

Introduction
• Despite of the fact that a thread must execute in
process, the process and its associated threads are
different concept. Processes are used to group
resources together and threads are the entities
scheduled for execution on the CPU.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 2


Definition
• A thread is a single sequence stream within in a process.
Because threads have some of the properties of
processes, they are sometimes called lightweight
processes.
• It is a low of execution through the process code with
its own program counter that keeps track of which
execution to execute next, system register , which
register, its current working variable and stack which
contain execution history.
• The CPU switches rapidly back and forth among the
threads giving illusion that the threads are running in
parallel. Like a traditional process i.e., process with one
thread, a thread can be in any of several states
(Running, Blocked, Ready or Terminated).
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 3
Definition(CONT,D..)
• Each thread has its own stack. Since thread will
generally call different procedures and thus a
different execution history. This is why thread needs
its own stack. An operating system that has thread
facility, the basic unit of CPU utilization is a thread.
A thread has or consists of a program counter (PC),
a register set, and a stack space. Threads are not
independent of one other like processes as a result
threads shares with other threads their code section,
data section, OS resources also known as task, such
as open files and signals.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 4


Threads vs processes
• As we mentioned earlier that in many respect
threads operate in the same way as that of
processes. Some of the similarities and
differences are:
Similarities
• Like processes threads share CPU and only one
thread active (running) at a time.
• Like processes, threads within a processes, threads
execute sequentially.
• Like processes, thread can create children.
• And like process, if one thread is blocked, another
thread can run.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 5


Threads vs processes –differences.
Process thread
Heavy weight/resource intensive Lightweight i.e takes lesser resources
than process
Process switching require interaction Process switching does not require
with the cpu interaction with the cpu
If one process is blocked no other While a thread is blocked and waiting
process can execute until process is the second thread in the same task can
unblocked run.
Multiple processes use more Multiple threads use few resources
resources
In multiple processes each process One thread can lead or change another
operate independently of each other threads data
Each process execute the same code All threads share the same set of open
but has its own memory files, child processes
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 6
Advantages of threads
• Minimize context switch
• Provide efficient communication
• Allow utilization of multiple thread architecture
• Use of thread provide concurrency
• It is economical to create and context switch a
thread

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 7


Why Threads?
Following are some reasons why we use
threads in designing operating systems:
• A process with multiple threads make a
great server for example printer server.
• Because threads can share common
data, they do not need to use
interprocess communication.
• Because of the very nature, threads can
take advantage of multiprocessors.
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 8
Threads are cheap in the sense that:
• They only need a stack and storage for
registers
• Threads use very little resources of an
operating system in which they are
working.
• Context switching are fast when working
with threads.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 9


Types of threads

Threads can be implemented in two ways:


1. User level thred: user managed.
2. Kernel level thread: os manage thread acting
on kernel and operating system.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 10


User-Level Threads
• They are implemented in user-level libraries, rather
than via systems calls, so thread switching does
not need to call operating system and to cause
interrupt to the kernel.
• Library contain the code for creating and
destroying threads, for passing messages abnd
data between threads, for scheduling thread
execution and for saving and restoring thread
context.
• Thread management kernel is not aware of the
existence of the thread.
• Application start with a single thread
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 11
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 12
Advantages and disadvantages

advantages Disadvantages
• Thread switching does • In typical operating
not require kernel most system calls are
privileges blocking
• Can run on any • Multithreaded
operating system application cannot
take advantage of
• Fast to create and multiprocessing
manage.

Developing Character, Skills and Competence Compiled by 08/12/2024 04:05 PM 13


G.B.Juma
Kernel-Level Threads

• Thread management done by kernel


• No thread management code in the application
• Supported directly by Operating system
• Kernel maintain context information for the
process as a whole and individual threads within
a process.
• Kernel performs thread creation, scheduling and
management in kernel space

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 14


Advantages and disadvantages
Advantages Disadvantages

• Kernel can • Slower to create and


simultaneously schedule manage
multiple threads from • Transfer of thread from one
the same process. thread to another within the
same process require mode
• If one thread in a process switching to the kernel.
is blocked the kernel can
schedule another thread
of the same process
• Kernel routines can be
multithreaded

Developing Character, Skills and Competence Compiled by 08/12/2024 04:05 PM 15


G.B.Juma
Multithreading Models

Some operating system provide a combined user


level thread and Kernel level thread facility. Solaris is
a good example of this combined approach. In a
combined system, multiple threads within the same
application can run in parallel on multiple processors
and a blocking system call need not block the entire
process. Multithreading models are three types
1. Many to many relationship.
2. Many to one relationship.
3. One to one relationship.
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 16
Many to Many Model

• Multiplexes any number of user threads onto an


equal or smaller number of kernel threads.
• Diagram on next page shows the many-to-many
threading model where 6 user level threads are
multiplexing with 6 kernel level threads.
• Developers can create as many user threads as
necessary and the corresponding Kernel threads
can run in parallel on a multiprocessor machine.
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 17
• This model provides the best accuracy on concurrency
and when a thread performs a blocking system call,
the kernel can schedule another thread for execution.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 18


Many to One Model
• This model maps many user level threads to
one Kernel-level thread. Thread management is
done in user space by the thread library. When
thread makes a blocking system call, the entire
process will be blocked. Only one thread can
access the Kernel at a time, so multiple threads
are unable to run in parallel on multiprocessors.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 19


Many to One Model-continued

• If the user-level thread libraries are implemented


in the operating system in such a way that the
system does not support them, then the Kernel
threads use the many-to-one relationship modes.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 20


One to One Model

• There is one-to-one relationship of user-level


thread to the kernel-level thread. This model
provides more concurrency than the many-to-one
model. It also allows another thread to run when
a thread makes a blocking system call. It
supports multiple threads to execute in parallel
on microprocessors.
Disadvantage
• Creating user thread requires the corresponding
Kernel thread. OS/2, windows NT and windows
2000 use one to one relationship model.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 21


One to one thread model Diagram

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 22


Advantages of Threads over Multiple Processes

• Context Switching Threads are very inexpensive to


create and destroy, and they are inexpensive to represent.
• Sharing Threads allow the sharing of a lot resources
that cannot be shared in process, for example, sharing code
section, data section, Operating System resources like open
file etc.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 23


Disadvantages of Threads over Multiprocesses

 Blocking: If the kernel is single threaded, a


system call of one thread will block the whole
process and CPU may be idle during the
blocking period.
 Security Since there is, an extensive sharing
among threads there is a potential problem of
security.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 24


Application that Benefits from Threads
•A proxy server satisfying the requests for a
number of computers on a LAN would be
benefited by a multi-threaded process. In
general, any program that has to do more than
one task at a time could benefit from
multitasking. For example, a program that reads
input, process it, and outputs could have three
threads, one for each task.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 25


Application that cannot benefit from Threads
• Any sequential process that cannot be divided
into parallel task will not benefit from thread, as
they would block until the previous one
completes. For example, a program that displays
the time of the day would not benefit from
multiple threads.

Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 26


Resources used in Thread Creation and Process Creation
• When a new thread is created it shares its code section,
data section and operating system resources like open files
with other threads. But it is allocated its own stack,
register set and a program counter.
• The creation of a new process differs from that of a thread
mainly in the fact that all the shared resources of a thread
are needed explicitly for each process. So though two
processes may be running the same piece of code they need
to have their own copy of the code in the main memory to
be able to run. Two processes also do not share other
resources with each other.
Developing Character, Skills and Competence Compiled by G.B.Juma 08/12/2024 04:05 PM 27
END OF THREADS

Developing Character, Skills and Competence


08/12/2024 04:05 PM 28
Compiled by G.B.Juma

You might also like