Chapter Three
Chapter Three
• When we run a program, it does not execute directly. It takes some time to follow all the steps
required to execute the program, and following these execution steps is known as a process.
• A process can create other processes to perform multiple tasks at a time; the created processes
are known as clone or child process, and the main process is known as the parent process.
• Each process contains its own memory space and does not share it with the other processes. It is
known as the active entity.
Conti..
• When we start executing the program, the processor begins to process it. It takes the following
steps:
➢Firstly, the program is loaded into the computer's memory in binary code after translation.
➢A program requires memory and other OS resources to run it. The resources such that
registers, program counter, and a stack, and these resources are provided by the OS.
3
➢A register can have an instruction, a storage address, or other data that is required by the
process.
• A program may have different instances of it, and each instance of the running program is knowns
as the individual process.
Introduction to Threads
What is Thread?
• A thread is the subset of a process and is also known as the lightweight process.
• A process can have more than one thread, and these threads are managed independently by the
scheduler.
4
• All the threads within one process are interrelated to each other.
• Threads have some common information, such as data segment, code segment, files, etc., that is
shared to their peer threads. But contains its own registers, stack, and counter.
Conti.
.
5
Why Multithreading?
6
• The idea is to achieve parallelism by dividing a process into multiple threads.
7
How does thread work?
• As we have discussed that a thread is a sub process or an execution unit within a process. A
process can contain a single thread to multiple threads. A thread works as follows:
• When a process starts, OS assigns the memory and resources to it. Each thread within a process
shares the memory and resources of that process only.
• Threads are mainly used to improve the processing of an application. In reality, only a single
thread is executed at a time, but due to fast context switching between threads gives an illusion
that threads are running parallelly.
8
❑The user-level threads are implemented by
Types of Threads
userlevel libraries, not by the system calls.
There are two types of threads, which are:
2. Kernel-Level Thread
1. User Level Thread
❑ The kernel-level threads are handled by the
❑As the name suggests, the user-level threads
Operating system and managed by its
are only managed by users, and the kernel
kernel.
does not have its information.
❑ These threads are slower than user-level
❑These are faster, easy to create and manage. threads because context information is
managed by the kernel.
❑The kernel takes all these threads as a single
process and handles them as one process only. ❑ To create and implement a kernel-level
thread, we need to make a system call.
9
Features of Thread
• Threads share data, memory, resources, files, etc., with their peer threads within a
process.
• Threads can directly communicate with each other as they share the same address
space.
10
Key Differences Between Process and Thread
• A process is independent and does not contained within another process, whereas all threads are
logically contained within a process.
• A process can exist individually as it contains its own memory and other resources, whereas a
thread cannot have its individual existence.
• Processes can communicate with each other using inter-process communication only; in contrast,
threads can directly communicate with each other as they share the same address space.
11
Process Thread
A process is an instance of a program that is being executed or Thread is a segment of a process or a lightweight process that is
processed. managed by the scheduler independently.
Processes are independent of each other and hence don't share a Threads are interdependent and share memory.
memory or other resources.
The operating system takes all the user-level threads as a single
Each process is treated as a new process by the operating system.
process.
If one process gets blocked by the operating system, then the If any user-level thread gets blocked, all of its peer threads also
other process can continue the execution. get blocked because OS takes all of them as a single process.
Context switching between two processes takes much time as Context switching between the threads is fast because they are
they are heavy compared to thread. very lightweight.
The data segment and code segment of each process are Threads share data segment and code segment with their peer
independent of the other. threads; hence are the same for other threads also.
The operating system takes more time to terminate a process. Threads can be terminated in very little time.
12
New process creation is more time taking as each new process A thread needs less time for creation.
takes all the resources.
• Used to express communication in the form of multiple logical connections at the same time.
• The main contribution of threads in distributed system is that they allow clients and servers to be
constructed such that communication and local processing can overlap, resulting in a high level of
performance.
Multithread clients
13
Eg. Web browser(such as chrome are multithreaded)
▪ A web browser can startup several threads like once main html file has been fetched, separate
threads can be activated to take care of fetching the other parts one each for images on page,
animation/applets etc.
Benefits
• Simple architecture
14
➢ Each connection could be potentially served by the a different server,
according to availability
Multithreaded Server
❑ When a client sends the request, a thread is generated through which a user can
communicate with the server.
15
❑ We need to generate multiple threads to accept multiple requests from multiple clients at
16
• Waiting time for users decreases: In a single-threaded server, other users had to wait until the
running process gets completed but in multithreaded servers, all users can get a response at a
single time so no user has to wait for other processes to finish.
• Threads are independent of each other: There is no relation between any two threads. When a
client is connected a new thread is generated every time.
• The issue in one thread does not affect other threads: If any error occurs in any of the threads
then no other thread is disturbed, all other processes keep running normally. In a single-threaded
server, every other client had to wait if any problem occurs in the thread.
17
Code Migration
Moving Code
18
→ Sometimes we would like to change the place where the code is executed
• Code migration in distributed systems has traditionally taken the form of process migration, in
which an entire process is moved from one machine to another.
• The basic concept is that if processes are moved from heavily loaded to lightly loaded machines,
overall system performance can be improved.
Code Migration
• code migration refers to moving programs between machines with the goal of having those
programs executed at the target.
19
• In some cases, such as in process migration, a program’s execution status, pending signals, and
other components of the environment must all be moved.
✓ Improve communication times by shipping code to systems where large data set reside.
20
Migration models
➢The code segment is the part that contains the set of instructions that make up the program that is
being executed
➢The resource segment contains references to external resources needed by the process, such as
files, printers, devices, other processes and so on.
➢The execution segment is used to store the current execution state of a process, consisting of
private data, the stack and the program counter.
Conti..
21
• Depending on what is moved along with the code, we can classify different
types of code mobility
1) Weak mobility:
22
2) Strong mobility
23
- Sender – initiated or -
Receiver –initiated.
Thank you!
24