0% found this document useful (0 votes)
53 views12 pages

2.2 Thread

The document discusses the concept of threads within processes, highlighting their structure, similarities, and differences with processes. It covers the usage of threads for improved performance and the various thread libraries available, such as POSIX pthreads and Java threads. Additionally, it explains the implementation of threads in user space versus kernel space, including their advantages and disadvantages.

Uploaded by

Dagim Mengesha
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)
53 views12 pages

2.2 Thread

The document discusses the concept of threads within processes, highlighting their structure, similarities, and differences with processes. It covers the usage of threads for improved performance and the various thread libraries available, such as POSIX pthreads and Java threads. Additionally, it explains the implementation of threads in user space versus kernel space, including their advantages and disadvantages.

Uploaded by

Dagim Mengesha
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

2.

2 Thread

01/23/2025 Bahir Dar University OS(CoSc 1


2034)
Contents:
Thread concept
Thread usage
Thread library
Thread implementation

01/23/2025 Bahir Dar University OS(CoSc 2


2034)
Thread concept
• process model is based on two independent concepts: resource
grouping and execution.
• One way of looking at a process is that it is a way to group
related resources together.
• A process has an address space containing program text and
data, as well as other resources. These resource may include
open files, child processes, pending alarms, signal handlers,
accounting information, and more.
• By putting them together in the form of a process, they can be
managed more easily.
• The other concept a process has 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.
01/23/2025 Bahir Dar University OS(CoSc 3
2034)
Thread concept(con’t..)

• It has registers, which hold its current working variables.


• It has a stack, which contains the execution history, with one
frame for each procedure called but not yet returned from.
• Processes are used to group resources together; threads are the
entities scheduled for execution on the CPU.
• The term multithreading is also used to describe the situation
of allowing multiple threads in the same process.

01/23/2025 Bahir Dar University OS(CoSc 4


2034)
Thread concept(con’t..)
o A thread consists of:
• thread id
• program counter
• register set
• stack
o Threads belonging to the same process share:
• its code
• its data section
• other OS resources

Bahir Dar University


01/23/2025 5
OS(CoSc2034)
Processes and Threads
Similarities Differences
• Both share CPU and only one • Unlike processes, threads
thread/process is active (running) are not independent of one
at a time. another.

•Like processes, threads within a • Unlike processes, all


process execute sequentially. threads can access every
address in the task.
•Like processes, thread can
create children. • Unlike processes, thread
are design to assist one
•Like process, if one thread is other.
blocked, another thread can run.

Bahir Dar University


01/23/2025 6
OS(CoSc2034)
Thread usage
o several reasons for having multiple threads:
o many applications need multiple activities are going on at once.
 decomposing such an application into multiple sequential threads that
run in quasi-parallel, the programming model becomes simpler.
o they are lighter weight than processes, they are easier (i.e.,
faster) to create and destroy than processes.
o Having multiple threads within an application provide higher
performance argument.
• If there is substantial computing and also substantial I/0, having
threads allows these activities to overlap, thus speeding up the
application.
o Threads are useful on systems with multiple CPUs

01/23/2025 Bahir Dar University OS(CoSc 7


2034)
Thread library
o Thread libraries provide programmers an API to create and
manage threads
There are three basic libraries used:
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 OS X implement pthreads specifications
WIN32 threads
• These are provided as a kernel-level library on Windows systems.
Java threads
» Since Java generally runs on a Java Virtual Machine, 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.

01/23/2025 Bahir Dar University OS(CoSc 8


2034)
Thread implementation
o There are two main ways to implement a threads package: in user space and
in the kernel.
Implementing Threads in User Space
 All code and data structure are reside in user space.
 Invoking a function in the library results in a local prodecuder call in user
space not system call.
 the kernel is not aware of the existence of threads.
Advantage:
 To do thread switching, it calls a run-time system procedure, which is least
an order of magnitude-may be more-faster than trapping to the kernel
 They allow each process to have its own customized scheduling algorithm.
Disadvantage:
 problem of how blocking system calls are implemented
 Problem of page faults
 no other thread in that process will ever run unless the first thread
voluntarily gives up the CPU.
01/23/2025 Bahir Dar University OS(CoSc 9
2034)
Thread implementation
Implementing Threads in kernel Space
o All code and data structure are reside in kernels pace.
o Invoking a function in the library results system call.
o the kernel is aware of the existence of threads.
Advantage:
 All calls that might block a thread are implemented as system calls
 if one thread in a process causes a page fault, the kernel can easily check to
see if the process has any other runnable threads, and if so, run one of them
while waiting for the required page to be brought in from the disk.
o kernel threads solve some While problems, they do not solve all problem
o what happens when a multithreaded process forks?
 In many cases, the best choice depends on what the process is planning
to do next.

01/23/2025 Bahir Dar University OS(CoSc 10


2034)
Implementing Threads in kernel Space(con’t..)
o When a signal comes in, which thread should handle it?
 Possibly threads could register their interest in certain signals but there
may be two or more threads register for the same signal.
Hybrid Implementations
o use kernel-level threads and then multiplexes user-level threads
onto some or all of the kernel threads.

01/23/2025 Bahir Dar University OS(CoSc 11


2034)
End

01/23/2025 Bahir Dar University OS(CoSc 12


2034)

You might also like