0% found this document useful (0 votes)
25 views24 pages

Chapter Three

A process is an instance of a program that is being executed. A process can create child processes and each process has its own memory space. A thread is a lightweight process that is a subset of a process and multiple threads can exist within a process, sharing its resources. Threads allow for parallelism through concurrent execution and overlapping of I/O operations.

Uploaded by

miki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views24 pages

Chapter Three

A process is an instance of a program that is being executed. A process can create child processes and each process has its own memory space. A thread is a lightweight process that is a subset of a process and multiple threads can exist within a process, sharing its resources. Threads allow for parallelism through concurrent execution and overlapping of I/O operations.

Uploaded by

miki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

DEBRE BERHAN UNIVERSITY

Department of Computer science

Course Name: Introduction to Distributed System

Chapter 3: Processes What is


Process?
1
• A process is an instance of a program that is being executed.

• 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..

• A process can remain in any of the following states:

✓NEW: A new process is being created.


2
✓READY: A process is ready and waiting to be allocated to a processor.

✓RUNNING: The program is being executed.

✓WAITING: Waiting for some event to happen or occur.

✓TERMINATED: Execution finished.

How do Processes work?

• 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.

➢The program counter maintains the track of the program sequence.

➢The stack has information on the active subroutines of a computer program.

• 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?

• A thread is also known as lightweight process.

6
• The idea is to achieve parallelism by dividing a process into multiple threads.

o For example, in a browser, multiple tabs can be different 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.

• If a single thread executes in a process, it is known as a single-threaded And if multiple threads


execute simultaneously, then it is known as multithreading.

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.

• One system call is capable of creating more than one thread.

• Each thread has its own stack and register.

• 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.

• Processes are heavily weighted, whereas threads are light-weighted.

• A process can exist individually as it contains its own memory and other resources, whereas a
thread cannot have its individual existence.

• A proper synchronization between processes is not required. In contrast, threads need to be


synchronized in order to avoid unexpected scenarios.

• 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.

Threads in distributed system

• 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

▪ It can be used to hide delays/latencies in network communications by initiating communication


and immediately proceeding with something else.

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

• Perceived response is almost immediate

• Simple architecture

➢ Each connection thread is actually very simple

• Exploitation of replicated servers

14
➢ Each connection could be potentially served by the a different server,
according to availability

Multithreaded Server

❑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.

15
❑ We need to generate multiple threads to accept multiple requests from multiple clients at

the same time

Advantages of Multithreaded Server:


• Quick and Efficient: Multithreaded server could respond efficiently and quickly to the increasing
client queries quickly.

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.

An Architecture for a Multi-threaded Server


A multithreaded server organized in a dispatcher/worker model. e.g. web server

17
Code Migration
Moving Code

• Sometimes passing data is not enough

18
→ Sometimes we would like to change the place where the code is executed

—for load balancing, security, scalability, . . .

• 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.

Reason for code migration


Why?
✓ Improve computing performance by moving processes from heavily loaded machine to lightly
loaded machines(Load balancing ).

✓ Improve communication times by shipping code to systems where large data set reside.

Eg. A client ships code to a database server vice versa.

✓Flexibility to dynamically configure distributed systems

✓Improving fault tolerance

20
Migration models

• In code migration framework, a process consists of 3 segments.

➢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

• Migration(mobility) can be weak or strong:

1) Weak mobility:

• A characteristics feature of weak mobility is that a transferred program is


always started from its initial state.

• Only code segment can be transferred


Conti..

22
2) Strong mobility

• A characteristics feature of strong mobility is that a running process


can be stopped, subsequently moved to another m/c and then
resume execution where it left off.

• Code and execution segments can both be transferred.


Conti..

• Also migration can be

23
- Sender – initiated or -
Receiver –initiated.

➢Sender initiated :uploading code to a server


E.g.. Client sending a query to database server

➢Receiver –initiated – downloading code from a server by a client.


E.g.. Client downloading java applet from a server

Thank you!

24

You might also like