0% found this document useful (0 votes)
42 views7 pages

Threads and Processes

Threads and Processes

Uploaded by

Alejandro Zapata
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)
42 views7 pages

Threads and Processes

Threads and Processes

Uploaded by

Alejandro Zapata
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/ 7

Threads and Processes

In an operating system, a process is a job or a program that can be executed by the computer.
Think of MS Word application, which is a process that runs on computer. But an application
can do more than one thing at a time, which means that a given process in an operating
system can have one or more threads. Threads represent the actual processing of the code.

A process has its own system registers and memory stack which helps them in executing
threads. Threads are sometimes called lightweight processes. The graphic below shows a
process with a single thread within it:

Single Thread in a Process

Threads are very helpful in today's multi-tasking world. Considering MS Word application, it
is convenient to highlight the misspelled words while you type; or complete auto-saving of
your information as you work on it. The threads within the application process help you
achieve this goal.

Differences
Processes and thread work together, but they have lots of differences between them.
Typically, processes are fairly heavy (like MS Word), while the threads are lighter (like
background save option). The table below highlights some of the differences between the
two.

Processes Threads
When switching a process, operating system's No OS resources are required for thread-
resources are required switching
If a process is blocked, other processes waiting If a thread is blocked, another thread in the
in the queue are also blocked same process can still execute
Each process uses same code and has its own All threads can share files and share child
memory processes
An application having multiple processes will Processes using multiple threads use less
use more system resources system resources
Each process works on its own Threads can access data of other threads

Multi-Threading
It might be easy to confuse multitasking with multi-threading. Multitasking is a general term
for doing many tasks at the same time. On the other hand, multi-threading is the ability of a
process to execute multiple threads at the same time. Again, the MS Word example is
appropriate in multi-threading scenarios. The process can check spelling, auto-save, and read
files from the hard-drive, all while you are working on a document.

Consider the following diagram. The threads share the same code, files, and data. This means
that two ore more threads can run at the same time (auto-save, grammar check, spell-check,
word-count, etc.).

Multiple Threads in a Process


Multi-threading provides greater benefit than single-threading:

What are Threads?


Thread is an execution unit which consists of its own program counter, a stack, and a set of
registers. Threads are also known as Lightweight processes. Threads are popular way to
improve application through parallelism. The CPU switches rapidly back and forth among the
threads giving illusion that the threads are running in parallel.

As each thread has its own independent resource for process execution, multpile processes
can be executed parallely by increasing number of threads.
Types of Thread
There are two types of threads:

1. User Threads
2. Kernel Threads

User threads, are above the kernel and without kernel support. These are the threads that
application programmers use in their programs.

Kernel threads are supported within the kernel of the OS itself. All modern OSs support
kernel level threads, allowing the kernel to perform multiple simultaneous tasks and/or to
service multiple kernel system calls simultaneously.

Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies:

 Many to One Model


 One to One Model
 Many to Many Model

Many to One Model

 In the many to one model, many user-level threads are all mapped onto a single kernel
thread.
 Thread management is handled by the thread library in user space, which is efficient in
nature.
One to One Model

 The one to one model creates a separate kernel thread to handle each and every user
thread.
 Most implementations of this model place a limit on how many threads can be created.
 Linux and Windows from 95 to XP implement the one-to-one model for threads.

Many to Many Model

 The many to many model multiplexes any number of user threads onto an equal or smaller
number of kernel threads, combining the best features of the one-to-one and many-to-one
models.
 Users can create any number of the threads.
 Blocking the kernel system calls does not block the entire process.
 Processes can be split across multiple processors.
What are Thread Libraries?
Thread libraries provide programmers with API for creation and management of threads.

Thread libraries may be implemented either in user space or in kernel space. The user space
involves API functions implemented solely within the user space, with no kernel support. The
kernel space involves system calls, and requires a kernel with thread library support.

Three types of Thread

1. POSIX Pitheads, may be provided as either a user or kernel library, as an extension to the
POSIX standard.
2. Win32 threads, are provided as a kernel-level library on Windows systems.
3. 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 Pitheads
or Win32 threads depending on the system.

Benefits of Multithreading
1. Responsiveness
2. Resource sharing, hence allowing better utilization of resources.
3. Economy. Creating and managing threads becomes easier.
4. Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be
distributed over a series of processors to scale.
5. Context Switching is smooth. Context switching refers to the procedure followed by CPU to
change from one task to another.
Multithreading Issues
Below we have mentioned a few issues related to multithreading. Well, it's an old saying, All
good things, come at a price.

Thread Cancellation

Thread cancellation means terminating a thread before it has finished working. There can be
two approaches for this, one is Asynchronous cancellation, which terminates the target
thread immediately. The other is Deferred cancellation allows the target thread to
periodically check if it should be cancelled.

Signal Handling

Signals are used in UNIX systems to notify a process that a particular event has occurred.
Now in when a Multithreaded process receives a signal, to which thread it must be delivered?
It can be delivered to all, or a single thread.

fork() System Call

fork() is a system call executed in the kernel through which a process creates a copy of itself.
Now the problem in Multithreaded process is, if one thread forks, will the entire process be
copied or not?

Security Issues

Yes, there can be security issues because of extensive sharing of resources between multiple
threads.

There are many other issues that you might face in a multithreaded process, but there are
appropriate solutions available for them. Pointing out some issues here was just to study both
sides of the coin.

You might also like