Social Icons

Tuesday, July 15, 2014


WHAT IS PCB(process control block)

We have seen what is meant by process address space. When ever a program is loaded into the memory the memory address space is divided into four regions as we specified in my previous article. This region contains the code of the program the data. Heap is required while execution of the program and the stack is very essential for the execution of the program because the program gets executed inside a stack. The functions we write is executed inside the stack. The data of the functions is executed inside the stack. During the execution of the process if the process requires more memory then heap segment is used to allocate the memory for the process. It allocated additional blocks of memory to the process so that the process executes. Whats important to understand is when the program is loaded into the memory only the data segment and the code segment is allocated. The stack and the heap will be allocated only when the program starts execution.

                                          Now try to understand this concept.. Lets assume the program has been loaded into the memory and now it needs to execute. For execution of the process it requires the CPU time or not??? Yes... without the CPU time the process will not be able to execute. The CPU is a resource and all the resources in our computer system will be handled by the kernel. Hence, we can deduce that for a program to get CPU time it must be registered with the Kernel. Now, when the program which is loaded into the memory it has to  register itself with the kernel to get the CPU time. When the program registers with the kernel, the kernel will create an object specific to this process. This object is what we call as a PCB or the Process Control Block.

PCB is a structure created by the kernel to identify the programs uniquely. This object will contain one variable called ID. Using this variable it will differentiate the processes which are running in the memory. Depending on how many processes are running in the memory, those many PCB's are created in the kernel. CPU will allocate the time to only those processes which are registered with the kernel i.e has a PCB. If there is no Id the kernel could not identify the process and it will not run.


If any process somehow manages to run without a PCB then those processes are called as viruses. Usually these viruses will get attached to the existing processes and acquire the CPU time. In this way, the viruses will manage to corrupt many files in the filesystem and make the system slower as it gets attached to multiple processes. Linux is one of the most secure operating systems and this is the reason. In Linux each and every process requires a PCB and viruses cannot get attached to just any  process as the attached program will also require a PCB. In Windows Operating System its not required and hence its easily attracted by viruses. This concept will be more clear when we learn about the threads in Linux. Also keep it in your mind that the above concept is the reason why high definition games are not developed in Linux operating system. We will learn it slowly through my articles.

So, coming back to the PCB it contains a variable called the ID in which it contains a value called the pid. This is the process id of a program. All programs that are getting executed in the memory will be assigned a PCB in which a variable ID is present with a value called pid which is a number with which the process will differentiate every process. The structure of the PCB will look something similar to this.

The image in the left gives a basic idea of the pcb structure diagrammatically. This is just for a basic understanding. The PCB contains all the information about the process. The process number is nothing but the process ID. 

It also contains the current program counter, the number of CPU registers, the memory allocations of a process, the event info, the list of open file descriptors, the program priority information, scheduling policy, resources used by the program... etc.

The kernel maintains the pcb object until the program will terminate. When the program is loaded first an address space is created, and then a pcb object is created for the specific process. Until a pcb is not created its not called a process.

A program becomes runnable only when it becomes a process. When its running on the CPU its called a context. Hence a process could always be not running.
Some might be running and some might be in the wait state. Depending on the CPU availability the process will be in running and wait states.

To access the kernel data structures we have a special filesystem called the proc filesystem. This filesystem will display the kernel datastructures in an ordered format. The below image is how the kernel datastructures are displayed in proc filesystem.
 We will talk about this in detail in our later articles.

No comments:

Post a Comment