Operating Systems: Process Control Blocks (PCB)
Tags: computerscience, tutorial, os
Published: 2021-09-25 05:46:48

> 💡 I assume, you have read the previous two articles of this series before you start because I have used some of the jargon I explained in them frequently.

For sake of simplicity, assume we have only 1 CPU in a multiprogramming( preemptive or non-preemptive doesn't matter ) environment.

Whenever I use the term memory, I mean main memory or RAM until unless specified.
 
## Need for PCB
In the previous article, we saw a program is a _passive_ entity while a process is an _active_ entity. Simply putting, the process is your program in execution.

It's the job of OS to dynamically assign CPU to the process which requires it because processes don't need CPU all the time, a good example might be when they go for I/O.

The question is how do you(as an OS) _identify_ which process is which? I mean to say, there are dozens of processes that can exist at the same time in your system. For example, there can be an MS-Word process, pdf reader process, the browser process, and of course, the background processes all existing at the same time. Moreover, sometimes a process needs to be pulled out of the main memory for some other important process say, the task manager process in windows.

This is the reason, we need an identification card for every process that is created. And that identification card is what we call a **Process Control Block(PCB)**.

>💡 Commonly, PCB is also referred to as Context (of a process)

## What is a PCB?
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uxzibp6n8157a8c2ba6.png)
 
Whenever a process is created, OS also creates a data structure(a record) that contains important information about a process. This is called PCB. 

Every process that comes into existence has a corresponding context or PCB associated with it. These PCBs are stored in the memory reserved for the operating system.

You can think of it as a dictionary having various keys(attribute or information name) and values(the information itself!).

## Important Attributes or fields in a PCB

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnggv8wrc6qdb6ss0fgg.png)
  
- Process ID: A unique _identification number_ for every process.

- Program Counter: Address of the _next instruction_ that is to be executed.

- Process State: It contains the _current state_ of the process. A process goes under various states like ready, running, wait, etc. throughout its execution. (Topic for next post)

- Priority: A number specifying the _importance_ of this process. (I will come to this when we discuss scheduling algorithms) 

- General Purpose Registers: It contains information about _CPU registers_ used by the process. 
 - When a process is executing, data about it is stored in these so-called registers. When a process is preempted by OS to schedule some other process, this new process will override the old data present in these registers. 
 - Thus when our old process is scheduled again, we need its register information.

- List of open files: It is self-explanatory; contains information about files that are opened by this process.

- List of open devices: Again self-explanatory; contains information about opened devices say, printer.

And the list goes on... 
I think you should appreciate the fact that OS exists otherwise just imagine doing process management manually!!

## How are PCBs stored
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q41m03igr6qlx4d9hfha.png)
  
These PCBs are organized in the form of _LinkedList_ in the memory.

## Summary
- PCBs are important data structures created by OS for controlling the execution of a process.
- Every process has a corresponding PCB associated with it.
- Also known as Context.
- Created in memory reserved for the operating system.