In: Computer Science
What is a program? What is a process? What is a thread? Explain the differences/similarities in details between them.
1. What is a Program: A program is a group of instructions which is created to perform a specific task on the computer. The program itself does not perform any action and it stored on any file or memory device.
Example: below is a program which will add a and b variables and print the sum which is stored in c variable:
Main(){
Int a =10, b= 20;
Int c;
Cout<<“Sum of a and b is “< } 2. What is a Process: A process is an activity
going on in the computer. The program to be executed needs a
process or group of processes for its execution. When a process is
started required data is moved from secondary storage to
processor’s registers. A process needs Processor, Input/Output
devices, memory to accomplish the goal. A process can be: Execution of the program means, that program is using the
processor, accessing the other resources required, changing its
variable values etc. Process understands what is being required in
the program to be executed and perform the same actions. A program
can have multiple processes in it. Example: The process to execute the example
program will have below characteristics: 3. What is a Thread: Threads are the light
weight processes OR sub-part of processes, which can be
independently managed by the scheduler. The threads are part of
processes. Each thread has its parent process and it
belongs to exactly one process during its life time. Example: To execute the above program in the
processor, it will be divided into small threads to perform the
independent task. E.g. Thread #1 will move the variables to the
main memory, Thread #2 will make a space in memory to store
variable c whereas another variable will calculate sum of a and b.
The use of threads can be user defined as well as processor
defined. Below diagram shows the purpose and definition of Program,
Purpose and Threads: Similarities between Program,
Process and Thread From above definitions, it can be clearly defined that program,
process and thread has same purpose, which is to perform the action
required on the user’s input. The program tells what to
do in a passive way; the process is created by the process to DO
the actions in program. The process is divided into small threads
to manage the processes in a better way. The following figure depicts that how three aspects are used
together to make the computer functioning correctly and provide the
results for the user input. Difference between Program,
Process and Thread Although the program, process and threads work with
each other to achieve the result for the end user yet all terms has
certain differences among functionality, lifetime and behaviour.
Please find below the differences between the same:
Feature
Program
Process
Thread
Mode
It a
ordered instuctions in passive mode.
Program instance in active mode is called process
Threads have the active mode
Dependency
It
does not depends on process or thread.
Process depends on the instuctions in program
Threads depends on the parent process
Lifespan
It
has longer lifespan as compared to process and thread. The program
needs to be deleted by the user manually.
Process has longer lifespan than thread but smaller lifespan than
program
Threads have smallest lifespan
Memory
It
needs only a file space in memory. The file stores the instructions
in it
Needs
independent CPU memory and dedicated registers.
Doesn't has own memory, share the process's memory
Purpose
Tell
the computer what to do.
Execute the action defined in 'What to do'
Help
process to perform action in faster and better way
Input
Output devices
The
program does not need input output devices for execution.
Process needs dedicated input and output devices for executing the
process.
Threads use parent process's input output devices
System
Calls
System calls are not needed.
System calls are needed in various stages of the process.
System calls are not needed.
Memory
sharing
It
does not shared the memory
Processes do not share the memory.
Threads shares the memory with each other.
Existence
A
program can exist individually
Process can exist individually for a program
Threads can not exist individually, these are created and destroyed
within a process
Instances
Program can have different instances of processes. Whenever s
program is executed a separated process is created.
Process has its life cycle, where it starts, executes and ends as
per instructions.
Threads are the small instances of process which starts and ends
within the life of parent process.