012ProcessandThreadinJava
012ProcessandThreadinJava
Process
It is a program that is running on your computer. The process is a heavy-weight,
which takes memory separately to other processes. The process may be a small
background task like spell-checker; it may be internet explorer or Microsoft word.
All processes have one or more threads. The process performs a series of
operations on a set of data.
The processes are created, scheduled, and terminated by the operating system
for the use of the CPU. The operations of the process are controlled with the help
of PCB (Process Control Block). PCB can be considered as the brain of the
process having all the crucial information of a process like its process id, priority,
state, contents CPU register, and PWS.
PCB is considered as the kernel-based data structure, which provides the three
kinds of functions which are scheduling, dispatching, and context save.
Scheduling: It chooses(decides) which process is (run)executed first in the CPU.
Dispatching: An environment is set up for the execution of the process.
Context save: It saves the information regarding a process when it gets
resumed or blocked.
The process is a program in execution. The operating system has to allocate the
resources; every process uses these resources and releases the resources on
the process termination. These increase the responsibility of the operating
system to provide all these resources to a process. In turn, the operating system
has a burden to manage these processes and to schedule the process. If there
are many processes created in the system, a point will come where the operating
system will not be able to allocate these resources to any process, and in turn,
the system will get hung. To solve this concept of threads was implemented.
The process life cycle has certain states included, such as ready, running,
blocked, and terminated. For consisting of the track of the processing activity at
an instant, its states are used.
Processes provide the medium to achieve the concurrent execution of a
program. The child process is created by the chief process of a concurrent
program. The main process and child process need to interact with each other
for completing the common task.
Feature of Process
1. The process has process id, CPU registers, memory locations, stack.
2. The process has its own virtual address space.
3. It provides protected access to processors, other processes, files, and I/O.
4. The process is a heavyweight.
5. The creation of a process includes system calls for each process
separately.
6. A single process does not share data and information and takes its
isolated memory space.
7. The IPC (Inter-process communication) mechanism for communication is
used by the processes, which significantly increases the number of system
calls.
8. More system calls are consumed by Process management.
9. Every process contains the stack and heap memory, instruction, data, and
memory map.
Thread
A thread is a lightweight process that shares the memory of a single process. It
supports Multithreading. Each thread relates to exactly one process, and no
thread can exist outside a process. Threads represent a software approach to
improve the performance of the operating system. They also specify a suitable
mechanism for parallel execution of applications on shared-memory
multiprocessors. Threads are not independent of one another; that is why they
share their code section, data section, and OS resources (like open files and
signals) with each other. But a thread has its program counter (PC), register set,
and stack space just like a process. The kernel provides a stack and a thread
control block (TCB) to every thread. The Threads mainly have the states called
the running, ready, and blocked states; it only includes computational state, not
resource allocation, and communication state, which reduces the switching
overhead. It enhances the concurrency (parallelism) because the many threads
are the parts of the single process. So, each thread is assigned to do the task to
speed up to complete the task.
When multiple threads are active, JVM provides the default priorities to each
thread. The priority of the thread specifies the time available for execution
relative to other active threads in the same process.
Feature of Thread:
As this is the multithreaded process, the threads execute in the same resources
that are being allotted to a process; hence multithreaded process or a thread is
said to be the lightweight process as it does not require in additional resources to
execute.
For example:
Responsiveness
By using Multithreading, it allows a program to run even if part of it is blocked. A
program continuously runs also if the process is performing a lengthy operation.
As many threads execute simultaneously for any single process, it increases the
responsiveness of a process as many threads doing the same job will improve
the response of the process. In an interactive application, we allow the program
to continue running even if a part is blocked or is performing some lengthy
operation; still, the application will respond because of Multithreaded.
Resource sharing
A single application can have many threads within the same address space for
sharing the resources that are allocated to the process.
As threads are the parts of the same process, they share the memory and
resource of the process to which they belong. The thread belongs to a specific
process that is executing some instruction, so these allow the benefit of code
sharing for an application so different threads may execute various activities for
an application in the same address space.
Economy
To create processes relates to more expensive and time-consuming as they
require more memory and resources as compared to threads. Management and
creation are much higher than thread creation and management.
Every thread executes in the process to which it belongs. The threads do not
have to allocate the new memory space or the resources, so they reduce the
resource utilization of the operating system. There is not much more need for
context switching between the processes as every thread is the part of the same
process and executing a particular part of the process, so the context switching
of the process is reduced, and that in turn reduces the resource requirement of
any operating system. Thus, it removes the overhead for creating and
maintaining a process rather than a thread.
Utilization of multiprocessor architectures
In this architecture, each thread can run on a different processor in parallel using
multithreading. This increases the concurrency of the system. In contrast, a
single processor system, where only one process or thread can run on a
processor at a time.
We can utilize the full power of multiprocessor architectures where every thread
can be assigned to a processor for some job. The multiple processors in the
multiprocessor systems will execute one single job that may be part of a big
processor, so this increases the speed of execution or speed of executing the
processes highly by running parallel instructions on a different processor.
A Single-threaded process will run only on the one processor even there are
many processors available for execution. In contrast, the Multithreaded process
will utilize all the processors that are available in the system.
What is the difference between Process and the Thread?
Process Thread
All process has the same code but has its All threads are sharing the same set of
own private memory and file resources in resources and open files of the
multiple processing environments. processes.
There is no other process can execute If one thread is blocked and waiting,
the other thread in the same task can
until the first process is unblocked.
run.
Without using threads, use more Multiple threaded processes use fewer
resources by the process. resources.
Types of Multitasking
1. Process-based Multitasking
2. Thread-based Multitasking.
Process-based Multitasking: Executing several multiple tasks simultaneously,
where each task is a separate independent program(process), is the process-
based Multitasking.
Example –All these three activities will be performed simultaneously, and each
task is independent of each other.
While typing a java program in the editor, we can listen to audio songs from the
same system at the same time we can download your file from the net. All these
tasks will be executed simultaneously and independently of each other hence it is
process-based Multitasking. It is better for the operating system level.
Thread based Multitasking:
It is the process of executing several tasks simultaneously where each task is a
separate independent part of the same program. Such type of Multitasking is
called Thread based Multitasking, and each independent part is called Thread. It
is suitable for programming level
Example: In a program, there are 10,000 lines of words. The first 5000 lines of
code are independent to other 5000 lines of code that means first related to the
first customer and second relates to another customer, so no need to wait to
execute second customer lines of code after first lines of code. Both parts can be
run simultaneously of the same program, so hardly less time to take to complete
the execution. The first independent part and second independent part executing
separate tasks simultaneously where each part is a separate independent part of
the same program, and each independent task is Thread This type of
Multitasking is Multithreading.
What is the difference between the Process and Thread based
Multitasking?
Process-based Multitasking Thread-based Multitasking
Each task is a separate independent Each task is the separate individual part of
process. the same program.