Professional Documents
Culture Documents
COA Chapter 2
COA Chapter 2
1. Tutorial Points
2. Geek for Geeks
3. Wikipedia
4. Lecture notes of experts as posted on Internet
Process
A process is defined as an entity which represents the basic unit of work to be implemented in the
system.
To put it in simple terms, we write our computer programs in a text file and when we execute this
program, it becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into four
sections ─ stack, heap, text and data. The following image shows a simplified layout of a process
inside main memory −
S.N. Component & Description
Stack
1
The process Stack contains the temporary data such as method/function parameters, return
address and local variables.
Heap
2
This is dynamically allocated memory to a process during its run time.
Text
3
This includes the current activity represented by the value of Program Counter and the
contents of the processor's registers.
Data
4
This section contains the global and static variables.
Program
A program is a piece of code which may be a single line or millions of lines. A computer program
is usually written by a computer programmer in a programming language. For example, here is a
simple program written in C programming language −
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
A computer program is a collection of instructions that performs a specific task when executed by
a computer. When we compare a program with a process, we can conclude that a process is a
dynamic instance of a computer program.
When a process executes, it passes through different states. These stages may differ in different
operating systems, and the names of these states are also not standardized.
In general, a process can have one of the following five states at a time.
2 The process is waiting to be assigned to a processor. Ready processes are waiting to have
the processor allocated to them by the operating system so that they can run. Process may
come into this state after Start state or while running it by but interrupted by the scheduler
to assign CPU to some other process.
Running
3
Once the process has been assigned to a processor by the OS scheduler, the process state is
set to running and the processor executes its instructions.
Waiting
4
Process moves into the waiting state if it needs to wait for a resource, such as waiting for
user input, or waiting for a file to become available.
Terminated or Exit
5
Once the process finishes its execution, or it is terminated by the operating system, it is
moved to the terminated state where it waits to be removed from main memory.
A Process Control Block is a data structure maintained by the Operating System for every process.
The PCB is identified by an integer process ID (PID). A PCB keeps all the information needed to
keep track of a process as listed below in the table −
Process State
1
The current state of the process i.e., whether it is ready, running, waiting, or whatever.
Process privileges
2
This is required to allow/disallow access to system resources.
Process ID
3
Unique identification for each of the process in the operating system.
Pointer
4
A pointer to parent process.
Program Counter
5
Program Counter is a pointer to the address of the next instruction to be executed for this
process.
CPU registers
6
Various CPU registers where process need to be stored for execution for running state.
CPU Scheduling Information
7
Process priority and other scheduling information which is required to schedule the process.
Memory management information
8
This includes the information of page table, memory limits, Segment table depending on
memory used by the operating system.
Accounting information
9
This includes the amount of CPU used for process execution, time limits, execution ID etc.
IO status information
10
This includes a list of I/O devices allocated to the process.
The architecture of a PCB is completely dependent on Operating System and may contain different
information in different operating systems. Here is a simplified diagram of a PCB −
The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.
There are three major triggers for context switching. These are given as follows:
Context Switching leads to an overhead cost because of TLB flushes, sharing the cache between
multiple tasks, running the task scheduler etc. Context switching between two threads of the same
process is faster than between two different processes as threads have the same virtual memory
maps. Because of this TLB flushing is not required.
A context switching is a process that involves switching of the CPU from one process or task to
another. In this phenomenon, the execution of the process that is present in the running state is
suspended by the kernel and another process that is present in the ready state is executed by the
CPU.
It is one of the essential features of the multitasking operating system. The processes are switched
so fastly that it gives an illusion to the user that all the processes are being executed at the same
time.
But the context switching process involved a number of steps that need to be followed. You can't
directly switch a process from the running state to the ready state. You have to save the context of
that process. If you are not saving the context of any process P then after some time, when the
process P comes in the CPU for execution again, then the process will start executing from starting.
But in reality, it should continue from that point where it left the CPU in its previous execution.
So, the context of the process should be saved before putting any other process in the running state.
A context is the contents of a CPU's registers and program counter at any point in time. Context
switching can happen due to the following reasons:
• When a process of high priority comes in the ready state. In this case, the execution of the
running process should be stopped and the higher priority process should be given the CPU
for execution.
• When an interruption occurs then the process in the running state should be stopped and
the CPU should handle the interrupt before doing something else.
• When a transition between the user mode and kernel mode is required then you have to
perform the context switching.
The process of context switching involves a number of steps. The following diagram depicts the
process of context switching between the two processes P1 and P2.
In the above figure, you can see that initially, the process P1 is in the running state and the process
P2 is in the ready state. Now, when some interruption occurs then you have to switch the process
P1 from running to the ready state after saving the context and the process P2 from ready to running
state. The following steps will be performed:
1. Firstly, the context of the process P1 i.e. the process present in the running state will be
saved in the Process Control Block of process P1 i.e. PCB1.
2. Now, you have to move the PCB1 to the relevant queue i.e. ready queue, I/O queue, waiting
queue, etc.
3. From the ready state, select the new process that is to be executed i.e. the process P2.
4. Now, update the Process Control Block of process P2 i.e. PCB2 by setting the process state
to running. If the process P2 was earlier executed by the CPU, then you can get the position
of last executed instruction so that you can resume the execution of P2.
5. Similarly, if you want to execute the process P1 again, then you have to follow the same
steps as mentioned above(from step 1 to 4).
For context switching to happen, two processes are at least required in general, and in the case of
the round-robin algorithm, you can perform context switching with the help of one process only.
The time involved in the context switching of one process by other is called the Context
Switching Time.
The disadvantage of context switching is that it requires some time for context switching i.e. the
context switching time. Time is required to save the context of one process that is in the running
state and then getting the context of another process that is about to come in the running state.
During that time, there is no useful work done by the CPU from the user perspective. So, context
switching is pure overhead in this condition.