PCB

  • PCB(Process Control Block): Kernel internal data structure used by the operating system to manage processes
  • A program is an executable file stored on disk, and a process is a program that is loaded into memory and running.
    • For a process to run, it requires resources such as CPU, memory, files, and input/output devices.
    • The operating system allocates CPU time to multiple processes so that they appear to be running simultaneously.
    • At this time, the status of each process, how far it has run, and what resources it is using must be stored.
  • PCB is a data structure that stores execution information of these processes.

Process

A process is a running program.

For example, when a user runs a program, the operating system loads the executable file into memory and creates a process.

A process is not simply executed with code, but also has the following elements:

  • Code area
  • Data area
  • Heap area
  • Stack area
  • CPU Register value
  • Program Counter
  • Open file information
  • Memory management information
  • Scheduling Information

In other words, a process can be viewed as an execution unit that combines the code of an executable file and information on the running state.

The role of PCB

PCB is an information storage space used by the operating system to identify and control processes.

An operating system must manage multiple processes simultaneously.

However, CPU can only process one execution flow at a time.

Therefore, the operating system alternately allocates CPU to multiple processes, making it as if multiple programs are running simultaneously.

At this time, if a process is interrupted while using CPU, it must be able to continue running again later.

For this purpose, the operating system stores the execution status of the current process in PCB.

And later, when the process is assigned CPU again, the information stored in PCB is restored and the previous execution flow continues.

In other words, PCB is a key data structure for saving and restoring the execution state of the process.

Information stored in PCB

The following information is generally stored in PCB.

  • Process ID
  • Process State
  • Program Counter
  • CPU Register Information
  • CPU Scheduling information
  • Memory Management Information
  • Accounting information
  • I/O Status information

The internal implementation of PCB may differ depending on the operating system, but basically, the information necessary to stop and restart the process is stored.

Process ID

Process ID is a unique value to identify a process.

Because the operating system manages multiple processes simultaneously, it must be able to distinguish between each process.

The value used at this time is PID.

For example, using the ps command in Linux, you can check PID of each process.

ps

When a process is created, the operating system assigns PID to the process.

Then, PID is used during process management, termination, tracking, and permission checking.

For example, when terminating a specific process, specify the process based on PID.

kill 1234

The above command has a similar meaning to sending a termination signal to a process where PID is 1234.

Process State

Process State is information that indicates what state the process is currently in.

A process moves through several states while it is running.

Representative process states are as follows:

  • New
  • Ready
  • Running
  • Waiting
  • Terminated

New

The New state is a state in which a process is being created.

When a user runs a program or a process creates a new process, the operating system creates a new PCB and prepares the resources needed to run the process.

After this process is completed, the process can move to the Ready state.

Ready

The Ready state is ready to be assigned CPU.

The process has completed the basic preparations required for execution, but has not yet been assigned CPU.

Processes in the Ready state usually enter the Ready Queue, and the scheduler selects one of them and allocates CPU.

Running

The Running state means that the process is actually using CPU.

When a process is assigned CPU, it enters Running state and executes its own command.

However, it cannot continue to occupy CPU.

It can move from the Running state to another state when the time slice ends, a higher priority process appears, or I/O operations are required.

Waiting

The waiting state is a state in which a process is waiting for an event.

For example, a process is in the waiting state when waiting for operations such as file reading, network response, user input, or disk I/O.

A process in the waiting state cannot be executed immediately even if CPU is allocated, so it waits until the event is completed.

When the event is completed, it moves back to the Ready state.

Terminated

The Terminated state means that process execution has finished.

When a process terminates normally or with an error, the operating system cleans up the resources of the process.

At this time, PCB is also removed when it is no longer needed.

Program Counter

Program Counter is a register that stores the address of the next instruction to be executed.

If a process is interrupted while using CPU, it needs to know where to continue when it runs again later.

For this purpose, the current Program Counter value is stored in PCB.

For example, if the process was running in the following flow:

mov eax, 1
add eax, 2
mov ebx, eax

If the process is stopped after executing add eax, 2, it must be executed starting from mov ebx, eax when it is executed again later.

The Program Counter value is needed to know this location.

In other words, Program Counter is information for restoring the execution location of the process.

CPU Register Information

While the process is running, various values are stored in CPU Register.

For example, based on x86, the following registers can be used.

eax
ebx
ecx
edx
esp
ebp
eip
eflags

Based on x86-64, the following registers are used.

rax
rbx
rcx
rdx
rsp
rbp
rip
rflags

If a process is stopped while using CPU, the register value must also be saved.

If register values are not saved, previous calculation results or stack positions cannot be restored when the process is re-executed later.

For example, if a process is stopped while saving calculation results in rax, the rax value must be restored when it is run again.

Therefore, the operating system saves the current CPU Register value in PCB during the Context Switch process and restores the Register value in PCB of the next process to be executed.

CPU Scheduling Information

CPU Scheduling information is information used by the operating system to determine which process to allocate CPU.

The following scheduling-related information can be stored in PCB.

  • process priority
  • scheduling queue pointer
  • CPU usage time
  • Time Slice Information
  • Scheduling policy related values

The operating system’s scheduler selects the process to be executed next based on this information.

For example, in priority-based scheduling, processes with higher priority may be executed first.

In the Round Robin method, each process uses CPU for a set amount of time, and when the time expires, it goes back into the Ready Queue.

For this scheduling operation, the scheduling status of the process is stored in PCB.

Memory Management Information

Memory Management information is information for managing the memory address space of a process.

Each process has an independent virtual address space.

The operating system must manage where each process’s virtual address is mapped in actual physical memory.

For this purpose, the following information can be stored in PCB.

  • Page Table Information
  • Segment Table Information
  • Base Register
  • Limit Register
  • Address space related information

Modern operating systems usually use virtual memory.

Although the process appears to use its own independent memory space, the operating system actually converts virtual addresses to physical addresses through page tables.

When a process is switched, the memory address space must also be switched.

Therefore, PCB contains information that allows you to find the memory management information of the process.

Accounting Information

Accounting information is information related to the resource usage of the process.

The operating system can record how much the process used CPU, which user ran it, how much time it was used, etc.

Accounting information that can be saved in PCB is as follows.

  • CPU usage time
  • actual execution time
  • Process creation time
  • user ID
  • group id
  • resource usage

This information can be used in system monitoring, permission management, resource restrictions, billing systems, etc.

For example, in a server environment, you may need to check whether a particular user is using excessive amounts of CPU time.

At this time, resource usage information for each process is needed.

I/O Status Information

I/O Status information is information related to the input/output resources being used by the process.

A process can use resources such as files, sockets, and devices while running.

The operating system must manage which files each process has open and which device it is using.

I/O-related information that can be stored in PCB is as follows.

  • List of open files
  • file descriptor table
  • Information on the input/output device in use
  • Pending I/O request information

For example, when a process opens a file, the operating system registers the file information in the file descriptor table of that process.

int fd = open("test.txt", O_RDONLY);

Afterwards, the process accesses the file using the fd value.

read(fd, buf, 100);

The operating system must know which file this fd actually points to.

This information is managed as the I/O status information of the process.

Context

Context refers to the execution status of the process.

If a process is running at a certain point, the CPU Register value, Program Counter, Stack Pointer, memory information, etc. at that point can be considered the context of the process.

If the operating system wants to stop a process and run it again later, it must save and restore this Context.

At this time, the place where the context is saved is PCB.

In other words, PCB can be viewed as a space that stores the context of the process.

Context Switch

Context Switch is the process of switching the process in which CPU is running to another process.

For example, if Process A uses CPU and then switches to Process B, the operating system performs the following tasks.

  1. Saves the current execution state of Process A.
  2. Process A’s Register, Program Counter, Stack Pointer, etc. are stored in PCB.
  3. The scheduler selects Process B to be executed next.
  4. Restore the execution state saved in PCB of Process B.
  5. CPU The execution flow moves to Process B.

In other words, Context Switch is the process of saving the state of the current process in PCB and restoring the state of the next process in PCB.

Context Switch Example

For example, if you have two processes:

Process A
Process B

At first, let’s say Process A is using CPU.

CPU -> Process A

Then, when a timer interrupt occurs, the operating system intervenes.

The operating system stores the current state of Process A in PCB of Process A.

Process A PCB
	- Program Counter
	- Register
	- Stack Pointer
	- Process State

Afterwards, when the scheduler selects Process B, the operating system restores the value saved in PCB of Process B to CPU.

Process B PCB
	- Program Counter
	- Register
	- Stack Pointer
	- Process State

Then, CPU continues the execution flow of Process B.

CPU -> Process B

In this way, the operating system makes it appear that multiple processes are running simultaneously.

PCB and Kernel

PCB is generally stored in the kernel area.

If a process can directly modify its own PCB, the entire system may become unstable.

For example, if a process can arbitrarily increase its own priority or change the memory information of another process, the security and stability of the operating system will be compromised.

Therefore, PCB exists in the kernel memory area managed by the operating system and cannot be directly accessed by general user processes.

The process makes a request to the operating system through a system call, and if necessary, the operating system modifies PCB or the internal kernel data structure.

For example, when terminating a process, the user program does not directly delete PCB, but sends a termination request to the operating system and the operating system cleans up resources.

PCB and Process Table

The operating system must manage PCB of multiple processes.

For this purpose, the data structure that collects PCB can be called Process Table.

The Process Table stores information about processes that currently exist in the system.

When a process is created, a new PCB is created and registered in the Process Table.

When the process ends, the corresponding PCB is removed from the Process Table.

The operating system searches for processes with a specific PID, searches for schedulable processes, or manages process status through the Process Table.

Zombie Process

Zombie Process is a process that has finished execution, but part of PCB remains because the parent process did not retrieve the termination status.

Since the execution of the process itself has finished, CPU is not used.

However, information such as process termination status and PID remains and may occupy entries in the Process Table.

For example, in Linux, if a child process is terminated but the parent process does not call wait(), the child process may remain in a zombie state.

wait(NULL);

wait() is used when the parent process retrieves the exit status of the child process.

When the parent retrieves the exit status, the operating system can clean up any remaining PCB information.

PCB and TCB

A similar concept to PCB is TCB(Thread Control Block).

PCB is a data structure for managing processes, and TCB is a data structure for managing threads.

A process can have one or more threads.

Since each thread has an independent execution flow, execution status such as Program Counter, Register, and Stack must be stored separately for each thread.

The data structure used at this time is TCB.

  • PCB: Manage process-level resources and status
  • TCB: Manages execution flow and state on a per-thread basis

A process has resources such as address space, file descriptors, and memory information, and threads share the execution flow within the same process.

Therefore, in a multi-threaded environment, one PCB and multiple TCB can be used together.

Summary

PCB is an internal kernel data structure used by the operating system to manage processes.

For a process to run, it requires several resources such as CPU, memory, files, and input/output devices, and the operating system must manage this information.

The following information is generally stored in PCB.

  • Process ID
  • Process State
  • Program Counter
  • CPU Register Information
  • CPU Scheduling information
  • Memory Management Information
  • Accounting information
  • I/O Status information