Process in Operating System
A process is a program in execution. For example, when we write a program in C or C++ and
compile it, the compiler creates binary code. The original code and binary code are both
programs. When we actually run the binary code, it becomes a process.
• A process is an 'active' entity instead of a program, which is considered a 'passive' entity.
• A single program can create many processes when run multiple times; for example,
when we open a .exe or binary file multiple times, multiple instances begin (multiple
processes are created).
How Does a Process Look Like in Memory?
A process in memory is divided into several distinct sections, each serving a different purpose.
Here's how a process typically looks in memory.
Process structure
• Text Section: A text or code segment contains executable instructions. It is typically a
read only section
• Stack: The stack contains temporary data, such as function parameters, returns
addresses, and local variables.
• Data Section: Contains the global variable.
• Heap Section: Dynamically memory allocated to process during its run time.
Attributes of a Process
A process has several important attributes that help the operating system manage and control
it. These attributes are stored in a structure called the Process Control Block
(PCB) (sometimes called a task control block). The PCB keeps all the key information about
the process, including:
1. Process ID (PID): A unique number assigned to each process so the operating system
can identify it.
2. Process State: This shows the current status of the process, like whether it is running,
waiting, or ready to execute.
3. Priority and other CPU Scheduling Information: Data that helps the operating
system decide which process should run next, like priority levels and pointers to
scheduling queues.
4. I/O Information: Information about input/output devices the process is using.
5. File Descriptors: Information about open files and network connections.
6. Accounting Information: Tracks how long the process has run, the amount of CPU
time used, and other resource usage data.
7. Memory Management Information: Details about the memory space allocated to the
process, including where it is loaded in memory and the structure of its memory layout
(stack, heap, etc.).
These attributes in the PCB help the operating system control, schedule, and manage each
process effectively.
States of a Process in Operating Systems
•
A process in an operating system passes through multiple states as it begins execution, waits
for resources, gets scheduled, runs, and eventually finishes. When a program is launched and
becomes a process, it transitions through several well-defined stages based on its activity and
resource availability. These stages collectively describe the complete lifecycle of a process.
Common types of process state models include:
• Two-State Model
• Five-State Model
• Seven-State Model
Two-State Model
The Two-State Model is the simplest representation of how a process behaves during its
lifetime. It divides the process lifecycle into only two possible conditions. A process is either
actively using the CPU to execute instructions or waiting until it gets a chance to run. This
model helps illustrate the basic interaction between a process and the CPU.
The two states are
• Running: The process is currently using the CPU to execute its tasks.
• Not Running: The process is not using the CPU. It may be waiting for input,
waiting for resources, or simply paused.
When a new process is created, it starts in the not running state. Initially, this process is kept in
a program called the dispatcher. here’s what happens step by step:
1. Not Running State: A newly created process starts here since it is not yet using
the CPU.
2. Dispatcher Role: The dispatcher checks if the CPU is free for a new process.
3. Moving to Running State: If the CPU is available, the dispatcher loads the
process onto it, shifting it to the running state.
4. CPU Scheduler Role: The scheduler selects which process should run next based
on the OS’s scheduling policy.
The Five-State Model
The five-state process lifecycle expands the basic two-state idea by separating processes that
are simply waiting for CPU time from those that cannot run because they are waiting for an
external event. This makes the model more accurate for real operating systems, where
processes may pause for input, data, or device responses before continuing.
The five states are:
• New: The process has just been created. It hasn’t started running yet, but its PCB
(Process Control Block) is prepared.
• Ready: The process is loaded into memory and prepared to run as soon as the
CPU becomes free.
• Running: The CPU is currently executing this process. With a single CPU
system, only one process can be in this state at a time.
• Blocked/Waiting: The process cannot continue execution because it is waiting for
an event like I/O completion or data input.
• Exit/Terminate: The process has completed its execution or has been stopped,
and the operating system removes it from memory.
The Seven-State Model
A process moves through different states as it is created, prepared for execution, runs on the
CPU, waits for resources, or completes. These states help the operating system manage how
processes use memory, CPU, and I/O devices.
The main process states are:
• New: The process is in the creation phase. The program exists in secondary
memory, and the OS prepares its PCB before loading it into main memory.
• Ready: The process has been loaded into main memory and is prepared to run. It
is waiting in the ready queue until the CPU becomes available.
• Running: The CPU is currently executing the instructions of the process. Only
one process can be in this state at a time on a single-processor system.
• Blocked/Waiting: The process cannot continue because it is waiting for an event
such as I/O completion, user input, or access to a locked resource. Once the event
is complete, it moves back to the ready state.
• Terminated: The process has completed its execution or has been stopped. The
OS deletes its PCB and frees all resources allocated to it.
• Suspend Ready: A ready process that has been swapped out of main memory and
placed in secondary storage due to memory shortage. When brought back to main
memory, it returns to the ready state.
• CPU-Bound / I/O-Bound: A CPU-bound process spends most of its time
performing computations, while an I/O-bound process spends more time waiting
for input/output operations.
Movement of a Process From One State to Another
A process transitions between different states depending on its progress and the availability of
system resources. These movements help the operating system manage tasks efficiently.
• New → Ready:
A process is created, resources are allocated, and it is loaded into main memory.
• Ready → Running:
The scheduler selects a ready process and assigns the CPU to it.
• Running → Blocked (Waiting):
The process must wait for an event or resource (e.g., I/O, user input, system call).
• Blocked → Ready:
The event completes or the needed resource becomes available, so the process is
ready to run again.
• Running → Ready:
The OS preempts the running process—often because a higher-priority process
becomes ready.
• Running → Terminated:
The process completes its execution or is forcefully stopped.
• Blocked → Terminated:
The process waiting for an event is aborted or killed by the OS or another process.
• (General Rule):
A process may move between ready, running, and blocked many times,
but new and terminated happen only once in its lifetime.
Types of Schedulers
• Long-Term Scheduler:
The long-term scheduler decides which new processes should enter the ready state
and controls the degree of multiprogramming.
• Short-Term Scheduler:
The Short-term scheduler selects the next process from the ready queue to run on
the CPU and works very frequently.
• Medium-Term Scheduler:
The medium-term scheduler handles swapping by suspending processes and
moving them between main memory and secondary storage.
Multiprogramming
We have many processes ready to run. There are two types of multiprogramming:
• Preemption: Process is forcefully removed from CPU. Pre-emption is also called
time sharing or multitasking.
• Non-Preemption: Processes are not removed until they complete the execution.
Once control is given to the CPU for a process execution, till the CPU releases the
control by itself, control cannot be taken back forcibly from the CPU.
The number of processes that can reside in the ready state at maximum decides the degree of
multiprogramming, e.g., if the degree of programming = 100, this means 100 processes can
reside in the ready state at maximum.
Operation on The Process
• Creation: A process is created and placed in the ready queue (main memory),
where it waits to be executed.
• Scheduling: The operating system selects one process from the ready queue to
run next. This selection step is called scheduling.
• Execution: The CPU starts running the chosen process. If the process needs to
wait for an event or resource, it becomes blocked, and the CPU switches to another
process.
• Termination: Once the process completes its task, the OS terminates it and clears
its context.
• Blocking: When a process waits for an event or resource (like I/O), it enters the
blocked state and cannot continue until that event is completed.
• Context Switching: When the OS switches from one process to another, it saves
the current process’s context and loads the next one. This switching is called context
switching.
• Inter-Process Communication (IPC): Processes often need to exchange data or
coordinate. The OS supports IPC using mechanisms like shared memory, message
passing, and synchronization tools.
These states—new, ready, running, waiting, and terminated—represent different stages in a
process's life cycle. By transitioning through these states, the operating system ensures that
processes are executed smoothly, resources are allocated effectively, and the overall
performance of the computer is optimized.
Process Control Block in OS
A Process Control Block (PCB) is used by the operating system to manage information about
a process. The process control keeps track of crucial data needed to manage processes
efficiently. A process control block (PCB) contains information about the process, i.e. registers,
quantum, priority, etc. The process table is an array of PCBs, which logically contains a PCB
for all of the current processes in the system. In this article, we will discuss every point about
the Process Control Block.
What is a Process Control Block (PCB)?
A Process Control Block (PCB) is a data structure that is used by an Operating System to
manage and regulate how processes are carried out. In operating systems, managing the process
and scheduling them properly play the most significant role in the efficient usage of memory
and other system resources. In the process control block, all the details regarding the process
corresponding to it like its current status, its program counter, its memory use, its open files,
and details about CPU scheduling are stored.
With the creation of a process, a PCB is created which controls how that process is being carried
out. The PCB is created with the aim of helping the OS to manage the enormous amounts of
tasks that are being carried out in the system. PCB is helpful in doing that as it helps the OS to
actively monitor the process and redirect system resources to each process accordingly. The
OS creates a PCB for every process which is created, and it contains all the important
information about the process. All this information is afterward used by the OS to manage
processes and run them efficiently.
Primary Terminologies Related to Process Control Block
• Process State: The state of the process is stored in the PCB which helps to manage the
processes and schedule them. There are different states for a process which are
"running," "waiting," "ready," or "terminated."
• Process ID: The OS assigns a unique identifier to every process as soon as it is created
which is known as Process ID, this helps to distinguish between processes.
• Program Counter: While running processes when the context switch occurs the last
instruction to be executed is stored in the program counter which helps in resuming the
execution of the process from where it left off.
• CPU Registers: The CPU registers of the process helps to restore the state of the
process so the PCB stores a copy of them.
• Memory Information: The information like the base address or total memory
allocated to a process is stored in PCB which helps in efficient memory allocation to
the processes.
• Process Scheduling Information: The priority of the processes or the algorithm of
scheduling is stored in the PCB to help in making scheduling decisions of the OS.
• Accounting Information: The information such as CPU time, memory usage, etc
helps the OS to monitor the performance of the process.
Operations Carried out through PCB
• Process Scheduling: The different information like Process priority, process state, and
resources used can be used by the OS to schedule the process on the execution stack.
The scheduler checks the priority and other information to set when the process will be
executed.
• Multitasking: Resource allocation, process scheduling, and process synchronization
altogether helps the OS to multitask and run different processes simultaneously.
• Context Switching: When context switching happens in the OS the process state is
saved in the CPU register and a copy of it is stored in the PCB. When the CPU switches
to another process and then switches back to that process the CPU fetches that value
from the PCB and restores the previous state of the process.
• Resources Sharing: The PCB stores information like the resources that a process is
using, such as files open and memory allocated. This information helps the OS to let a
new process use the resources which are being used by any other process to execute
sharing of the resources.
Points to Consider for Process Control Block (PCB)
• Interrupt Handling
• Context Switching
• Real-Time Systems
• Virtual Memory Management
• Inter-Process Communication
• Fault Tolerance
Location of The Process Control Block
The Process Control Block (PCB) is stored in a special part of memory that normal users can’t
access. This is because it holds important information about the process. Some operating
systems place the PCB at the start of the kernel stack for the process, as this is a safe and secure
spot.
Advantages of Using Process Control Block
• As, PCB stores all the information about the process so it lets the operating system
execute different tasks like process scheduling, context switching, etc.
• Using PCB helps in scheduling the processes and it ensures that the CPU resources are
allocated efficiently.
• When the different resource utilization information about a process are used from the
PCB they help in efficient resource utilization and resource sharing.
• The CPU registers and stack pointers information helps the OS to save the process state
which helps in Context switching.
• The process table and PCB can be used to synchronize processes in an operating
system. The PCB contains information about each process’s synchronization state, such
as its waiting status and the resources it is waiting for.
• The process table and PCB can be used to schedule processes for execution. By keeping
track of each process’s state and resource usage, the operating system can determine
which processes should be executed next.
Disadvantages of using Process Control Block
• To store the PCB for each and every process there is a significant usage of the memory
in there can be a large number of processes available simultaneously in the OS. So using
PCB adds extra memory usage.
• Using PCB reduces the scalability of the process in the OS as the whole process of
using the PCB adds some complexity to the user so it makes it tougher to scale the
system further.
• The process table and PCB can introduce overhead and reduce system performance.
The operating system must maintain the process table and PCB for each process, which
can consume system resources.
• The process table and PCB can increase system complexity and make it more
challenging to develop and maintain operating systems. The need to manage and
synchronize multiple processes can make it more difficult to design and implement
system features and ensure system stability
Conclusion
The Process Control Block (PCB) is essential for managing processes in an operating system.
It stores crucial data about each process, like its unique ID, current state, and resource usage.
The PCB enables smooth process switching, effective multitasking, and efficient resource
allocation. By keeping detailed records of each process, the PCB helps maintain system
stability and performance. Understanding the role and structure of PCBs is key to appreciating
how operating systems handle multiple processes simultaneously.
Context Switching in Operating System
Context switching is the process where the CPU stops running one process, saves its current
state, and loads the saved state of another process so that multiple processes can share the CPU
effectively.
Need in Multitasking
It is essential in multitasking systems where many processes need CPU time.
• In multitasking, the CPU keeps switching between processes.
• This makes it seem like processes are running at the same time, even though the CPU
works on one process at a time.
• Without context switching, one process could monopolize the CPU, and others would
have to wait indefinitely
Role in Scheduling
• The scheduler decides which process should run next based on a scheduling algorithm
(like Round Robin, Priority Scheduling, etc.).
• Context switching executes that decision by stopping the current process and starting
the chosen one.
• The dispatcher is the component that actually performs the switch.
Context Switching Triggers
Context Switching Happen:
• When a high-priority process comes to a ready state (i.e. with higher priority than the
running process).
• An Interrupt occurs.
• User and kernel-mode switch (It is not necessary though)
• Preemptive CPU scheduling is used.
When switching between user mode and kernel/user mode is necessary, operating systems use
the kernel/user switch.
Working Process of Context Switching
Let us take examples of two processes po and p1 that execute in interleaved manner
Explanation of the Above Diagram
The diagram illustrates how the CPU alternates execution between two processes — p0 and p1
— through context switching.
1. Execution Phase (Process p0)
• Process p0 is running while p1 remains idle.
• At some point, an interrupt or system call occurs (e.g., a timer interrupt or I/O
completion).
2. Saving Current State (p0 → PCB0)
• The operating system pauses p0.
• The current state of p0 (register values, program counter, etc.) is saved into its Process
Control Block (PCB0).
• This ensures that p0 can later resume exactly where it left off.
3. Loading New Process State (PCB1 → p1)
• The OS retrieves the saved state of p1 from PCB1.
• This step restores p1’s CPU context so it can continue execution from its last saved
point.
4. Execution Phase (Process p1)
• Now, p1 is running while p0 is idle.
• Another interrupt or system call occurs, triggering another switch.
5. Saving State of p1 (p1 → PCB1): The OS saves the current state of p1 into PCB1.
6. Reloading State of p0 (PCB0 → p0)
• The saved state of p0 from PCB0 is loaded back into the CPU registers.
• p0 resumes execution right where it left off.
Overhead
• Time spent switching is called context switch overhead.
• During this time, the CPU is not performing actual process work it’s just preparing to
run the next process.
• If context switching happens too often, CPU efficiency drops.
Factors Affecting Switch Time
Context-switch times are highly dependent on hardware support. For example, some processors
(such as the Sun UltraSPARC) provide multiple sets of registers. In this case, a context switch
simply requires changing the pointer to the current register set. However, if there are more
active processes than available register sets, the system resorts to copying register data to and
from memory, as before. Additionally, the more complex the operating system, the greater the
amount of work that must be done during a context switch.
• Hardware support for fast register saving/loading.
• Number of registers (more registers = more time to save/load).
• Memory speed for PCB storage.
• Efficiency of OS kernel code.