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

Parallel and distributed computing lec 5

The document provides an overview of threads in operating systems, detailing their components, types (user-level and kernel-level), benefits, and issues such as thread cancellation and signal handling. It emphasizes the advantages of multithreading, including improved performance and resource sharing, particularly in distributed systems. Additionally, it discusses the differences between threads and processes, as well as the implementation of multithreaded clients and servers.

Uploaded by

reactuser76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Parallel and distributed computing lec 5

The document provides an overview of threads in operating systems, detailing their components, types (user-level and kernel-level), benefits, and issues such as thread cancellation and signal handling. It emphasizes the advantages of multithreading, including improved performance and resource sharing, particularly in distributed systems. Additionally, it discusses the differences between threads and processes, as well as the implementation of multithreaded clients and servers.

Uploaded by

reactuser76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Parallel and distributed

computing
COMP3139
Agenda
• Thread in OS

• Its components

• Its types

• Difference between thread and process

• Multithread

• Benefits of thread

• Threads in distributed Systems


Threads
Threads in OS
• A thread is a single sequence stream within a process.
Threads are also called lightweight processes as they
possess some of the properties of processes.

• Each thread belongs to exactly one process.

• The process can consist of many threads. But threads


can be effective only if the CPU is more than 1 otherwise
two threads have to context switch for that single CPU.
Why Do We Need Thread?

• Threads run in parallel improving the application performance.

• Threads can share common data, so they do not need to use inter-
process Communication. Like the processes, threads also have
states like ready, executing, blocked, etc.

• Priority can be assigned to the threads just like the process, and
the highest priority thread is scheduled first.
Why Do We Need Thread?

• Each thread has its own Thread Control Block(TLB).

• Like the process, a context switch occurs for the thread, and
register contents are saved.

• Threads share the same address space and resources;


synchronization is also required for the various activities of the
thread.
Components of
Threads

These are the basic


components of the Operating
System.

• Stack Space

• Register Set

• Program Counter
Thread Implementation

• Threads are often provided in the form of a thread package.

• Such a package contains operations to create and destroy threads as well as


operations on synchronization variables such as mutexes and condition variables.

• There are basically two approaches to implement a thread package.

• The first approach is to construct a thread library that is executed entirely in user
mode.

• The second approach is to have the kernel be aware of threads and schedule
them.
Types of Threads

Types of Thread in Operating


System

Threads are of two types. These


are described below.

• User Level Thread

• Kernel Level Thread


User Level Thread

• User Level Thread is a type of thread that is


not created using system calls. The kernel has
no work in the management of user-level
threads.
• User-level threads can be easily implemented
by the user.
• when user-level threads are single-handed
processes, kernel-level thread manages them.
• User-level threads are implemented using user-
level libraries and the OS does not recognize
these threads.
User Level Thread
• User-level thread is faster to create and manage
compared to kernel-level thread.
• Context switching in user-level threads is faster.
• If one user-level thread performs a blocking
operation, then the entire process gets blocked.
E.g.: POSIX threads, Java threads, etc.
Advantages of User-Level Threads
• Implementation of the User-Level Thread is
easier than Kernel Level Thread.
• Context Switch Time is less in User Level
Thread.
Advantages of User-level Thread

• User-Level Thread is more efficient than Kernel-Level Thread.

• Because of the presence of only Program Counter, Register Set, and Stack
Space, it has a simple representation .

• It does not require modifications of the operating system.

• User-level threads representation is very simple. The register, PC, stack,


and mini thread control blocks are stored in the address space of the user-
level process.

• It is simple to create, switch, and synchronize threads without the


intervention of the process.
Disadvantages of User level Thread

• There is a lack of coordination between


Thread and Kernel.

• In case of a page fault, the whole process


can be blocked.
Kernel Level Thread

• The kernel thread recognizes the operating


system.

• There is a thread control block and process


control block in the system for each thread and
process in the kernel-level thread.

• The kernel-level thread is implemented by the


operating system.
Kernel Level Thread

• The kernel knows about all the threads and manages them.

• The kernel-level thread offers a system call to create and manage the
threads from user-space.

• The implementation of kernel threads is more difficult than the user


thread. Context switch time is longer in the kernel thread.

• The implementation of kernel threads is more difficult than the user


thread. Context switch time is longer in the kernel thread.
Advantages of Kernel-level
threads

• The kernel-level thread is fully aware of all


threads.

• The scheduler may decide to spend more CPU


time in the process of threads being large
numerical.

• The kernel-level thread is good for those


applications that block the frequency.
Disadvantages of Kernel-level
threads

• The kernel thread manages and schedules all


threads.

• The implementation of kernel threads is difficult


than the user thread.

• The kernel-level thread is slower than user-level


threads.
Coordination between both

• The combination of (user-level) threads and


L/VPs works as follows:

• The thread package has a single routine to


schedule the next thread. When creating an LWP
the LWP is given its own stack,

• It is instructed to execute the scheduling


routine in search of a thread to execute.
Coordination between both

• If there are several LWPs, then each of them executes the scheduler.

• The thread table, which is used to keep track of the current set of
threads, is thus shared by the LWPs.

• synchronization between LWPs does not require any kernel support.

• When an LWP finds a runnable thread, it switches context to that thread.

• creating, destroying, and synchronizing threads is relatively cheap and


involves no kernel intervention at all.
Difference Between Process and Thread

• The primary difference is that threads within the same


process run in a shared memory space, while processes
run in separate memory spaces.

• Threads are not independent of one another like


processes are,

• Threads share with other threads their code section,


data section, and OS resources (like open files and
signals)

• like a process, a thread has its own Program Counter


register set, and stack space.
What is Multi-Threading?

• A thread is also known as a lightweight process. The idea is to achieve


parallelism by dividing a process into multiple threads.

• For example, in a browser multiple tabs can be different threads. MS


Word uses multiple threads: one thread to format the text, another thread
to process inputs, etc.

• Multithreading is a technique used in operating systems to improve the


performance and responsiveness of computer systems.
Multithreading

• Multithreading allows multiple threads (i.e., lightweight processes) to


share the same resources of a single process, such as the CPU memory
and I/O devices.

• In Multithreading, the idea is to divide a single process into multiple


threads instead of creating a whole new process.

• Program responsiveness enables a program to run even if part of the


program is blocked or executing a lengthy operation.

• It is more economical to use threads as they share the resources of a


single process. On the other hand, creating processes is expensive.
Benefits of Threads

• Enhanced throughput of the system: When the process is split into many
threads, and each thread is treated as a job.

• Effective Utilization of Multiprocessor system: When you have more than


one thread in one process, you can schedule more than one thread in
more than one processor.

• Faster context switch: The context switching period between threads is


less than the process context switching. The process context switch
means more overhead for the CPU.
Benefits of Thread

• Responsiveness: When the process is split into several threads, and


when a thread completes its execution, that process can be responded to
as soon as possible.

• Communication: Multiple-thread communication is simple because the


threads share the same address space, In process, we adopt just a few
exclusive communication strategies.

• Resource sharing: Resources can be shared between all threads within a


process, such as code, data, and files.
Issues with Thread

1. The semantics of fork() and exec() system calls:

• During a fork() call the issue that arises is whether the whole process
should be duplicated or just the thread which made the fork() call should
be duplicated.

• The exec() call replaces the whole process that called it including all the
threads in the process with a new program.

2. Thread cancellation: The termination of a thread before its completion is


called thread cancellation and the terminated thread is termed as target
thread.
Issues with Thread

Thread cancellation is of two types:

i. Asynchronous Cancellation: Inasynchronous cancellation, one


thread immediately terminates the target thread.

ii. Deferred Cancellation: In deferred cancellation, the target thread


periodically checks if it should be terminated.
Issues with Thread

3. Signal handling: In UNIX systems, a signal is used to notify a


process that a particular event has happened. Based on the source
of the signal, signal handling can be categorized as:

i. Asynchronous Signal: The signal which is generated outside the


process which receives it.

ii. Synchronous Signal: The signal which is generated and delivered


in the same process.
Threads in Distributed Systems

• Threads can provide a convenient means of allowing blocking system


calls without blocking the entire process in which the thread is
running.

• It makes threads particularly attractive to use in distributed systems.

• It makes it much easier to express communication in the form of


maintaining multiple logical connections at the same time.
Multithreaded Clients

• In a single-process, single-
threaded environment all requests
to, e.g., the I/O interface blocks any
further progress in the process. Any
combination of a multi-process.

• A multi-process environment
requires an extensive support from
the underlying operating system.
Multithreaded Clients

• The static parts like the code segment and the static memory can be shared
between the two processes and does not require a copy of the memory.

• The process of forking a child process is not unique for Unix, but the exact
behavior is often quite platform dependent.

• Every process has a large set of environment variables that must be


initialized at the creation of the process.

• synchronization of the state of the parent and child process must be


established before the child is ready to execute the request.
Multithreaded Servers

• A server having more than one thread


is known as Multithreaded Server.

• When a client sends the request, a


thread is generated through which a
user can communicate with the
server.
• We need to generate multiple
threads to accept multiple requests
from multiple clients at the same
time.
THANK YOU

You might also like