How to switch from one process to another: all about context switching

Last update: July 27, 2023
context change, process

As I already explained in the article about changing a thread, it is also important to return to this topic of the context changes But applied to processes this time. This way, you'll have the missing piece of information to understand how the CPU and operating system work hand in hand when you're running multitasking software.

What is a process?

Task Manager

In computing, a process A process is a running instance of a program or software. It can be considered an active entity that executes a series of sequential instructions, accesses system resources, and communicates with other processes. A process has its own allocated memory space, which includes the program code, data, execution stack, and variables.

Each process It is created and managed by the operating system, which allocates resources such as memory, CPU time, open files, and input/output devices. The operating system is also responsible for scheduling and coordinating the execution of processes, ensuring efficient use of available resources.

Each process can have its own state, which can be active (running), waiting (blocked by an event), suspended (temporarily stopped), or terminated (terminated). Processes can communicate with each other through mechanisms such as interprocess communication (IPC), which allows for data exchange and synchronization between processes.

What is the CPI?

IPC (Inter-Process Communication) It is a set of mechanisms and techniques used by processes in an operating system to communicate and share information with each other. IPC allows processes to communicate and cooperate, whether on the same computer or across different computers connected by a network.

The CPI is essential for the concurrent programming and the creation of applications that require interaction and synchronization between multiple processes. It provides a means for data exchange, activity synchronization, and task coordination between processes.

There are different CPI mechanisms available, including:

  • message queues: allow the sending and receiving of messages between processes using a shared queue.
  • Shared memory: It allows multiple processes to access a common memory region, allowing them to exchange data quickly and efficiently.
  • Sockets: They provide a network interface for communication between processes on different machines via TCP/IP or other network protocols.
  • Pipes: They allow one-way communication between processes, where the output of one process is redirected as input to the other process.
  • Synchronization: Synchronization mechanisms, such as semaphores, mutexes, and condition variables, are used to coordinate access to shared resources and avoid race conditions between processes.

The choice of IPC mechanism depends on the specific requirements of the application and the environment in which the processes run. Each mechanism has its own advantages and limitations in terms of performance, complexity, and security.

IPC plays a fundamental role in concurrent programming and the construction of distributed systems, as it enables communication and collaboration between processes, facilitating the development of complex applications and the efficient use of system resources.

What is PCB?

In the context of processes, PCB (Process Control Block) refers to a data structure used by the operating system to maintain important information about a running process. It is also known as a Process Control Block.

The PCB is created by the operating system when a new process is created and stored in main memory. It contains essential information about the process's status and administration, allowing the operating system to properly control and manage its execution.

Some of the data stored on the PCB may include:

  • Process identification: a unique identifier for the process, such as a process number or name.
  • Status of the process: Indicates whether the process is running, waiting, ready to run, suspended, or terminated.
  • Memory management information: size of memory allocated to the process, base address, memory limits, page tables, etc.
  • Resource management information: list of files opened by the process, access permissions, file descriptors, etc.
  • Planner information: process priority, CPU time used, scheduling and resource allocation, among others.
  • Control information: Process context data, such as CPU registers, stack pointers, program counter, status registers, etc.
  How to Enable Windows Remote Desktop Using Commands

The PCB is continuously updated by the operating system as the process changes state or a context switch occurs. When a process is suspended or switched to another process, the operating system saves the current state of the process to its PCB and loads the state of the next process to execute.

The PCB is essential for the operating system's efficient management of processes, enabling proper control and coordination of CPU resources and scheduling. Each time a context switch operation is performed, the operating system consults the corresponding PCB to obtain the necessary information about the running process.

Differences between process and thread

The threads and processes Threads are two important concepts in the field of programming and program execution in an operating system. Below are some key differences between threads and processes:

  • Execution contextA process has its own independent execution context, which includes its own memory space, CPU registers, process ID, open files, and other resources. Threads, on the other hand, share the same execution context within a process. They share the same memory space, open files, and other resources as the parent process.
  • Creation and destructionProcesses are created by an operating system call and have their own independent lifecycle. They can be created and destroyed independently, meaning that terminating one process does not affect other running processes. Threads, on the other hand, are created within an existing process. A process can have multiple threads, and thread creation and destruction are done within the context of the parent process.
  • Communication and synchronizationProcesses typically communicate and synchronize using interprocess communication (IPC) mechanisms, such as pipes, message queues, or shared memory. Threads, sharing the same memory space, can communicate and synchronize directly using shared variables or mutual exclusion locks.
  • Performance and efficiency: Because threads share resources and have lower creation and destruction costs than processes, switching between threads within a process is generally faster and more efficient than switching between processes. This makes threads suitable for concurrent and parallel tasks that can benefit from simultaneous processing and resource sharing.
  • Resource protectionThreads share the same memory space and resources within a process, which means they must be cautious when accessing and modifying the same shared resources. Proper synchronization is required to avoid race conditions and ensure the consistency of shared data. In contrast, processes have their own memory space and resources, making them more independent and less prone to resource conflicts.

Differences between process context switching and thread context switching

Thread context switchProcess context change
TCS occurs when the CPU saves the current state of the thread and switches to another thread in the same process.PCS occurs when the operating system scheduler saves the current state of the running program (including the PCB state) and switches to another program.
TCS helps the CPU handle multiple threads simultaneously.PCS involves loading the new program states for execution.
TCS does not involve changing memory address spaces. All memory addresses that the processor addresses remain saved.PCS involves swapping memory address spaces. All memory addresses counted by the processor are emptied.
The processor cache and translation lookup buffer retain their state.The processor cache and TLB are flushed.
Although TCS involves changing registers and stack pointers, it doesn't involve the cost of changing the address space. Therefore, it's more efficient.PCS involves the high cost of changing the address space. Therefore, it is less efficient.
TCS is a little faster and cheaper.PCS is relatively slower and more expensive.

In single-threaded processes, the thread itself represents the entire process. However, in multi-threaded processes, switching between different threads is necessary for program execution:

  • Thread changeThread switching involves switching from one thread to another within the same process. This type of context switching is highly efficient and economical, as it only involves changing specific identities and resources, such as the program counter, registers, and stack pointers. The cost of switching from one thread to another is almost equivalent to the cost of entering and exiting the kernel.
  • Process changeProcess switching involves switching from one process to another. In this case, all resources associated with the current process must be exchanged for the resources needed by the new process. This involves changing the memory address space, including memory addresses, page tables, and kernel resources, as well as processor caches.
  Tin alloys for electronic soldering: types and which one to use in each case

How to perform a context switch

Let's see how to change context in a computer system, and to do so, we first need to understand some concepts:

While context switching took hundreds of nanoseconds on older processors, it only takes a few ns on newer processors. The impact of a context switch depends on the processor and can be measured in clock cycles, ranging in the hundreds in some cases and even over a thousand in others. Keep in mind that context switching entails additional costs due to TLB (Translation Lookaside Buffer) flushing, cache sharing among multiple tasks, and task scheduler execution, among other things. Context switching between two threads in the same process is faster than between two different processes, since the threads share the same virtual memory maps. Because of this, TLB flushing is not necessary…

What is context switching?

El context switch, is the process of saving the state of a process for later restoration and resumption, and then loading a previously saved, different state. This allows multiple processes to share a single central processing unit (CPU) and is a fundamental feature of multiprogramming or multitasking operating systems. In a traditional CPU system, each running process uses CPU registers to store data and maintain its current state. However, in a multitasking system, the operating system switches between processes or threads to allow multiple processes to execute simultaneously. At each switch, the operating system must save the state of the currently running process and then load the state of the next process to execute on the CPU. This sequence of operations involving saving and loading process states is known as context switching.

  How to encrypt a USB on Windows and Mac

Context Switching Cost: Impact on Performance

Context switches often require a significant amount of resources hardware, and much of operating system design focuses on optimizing their use. Switching from one process to another involves performing various administrative tasks, such as saving and loading registers and memory maps, updating tables and lists, and more. The exact nature of a context switch depends on the system architecture, the operating system, and the amount of shared resources. Threads belonging to the same process share many resources, unlike unrelated processes that don't collaborate with each other.

For example, in the linux kernelA context switch involves loading the corresponding process control block (PCB) stored in the PCB table onto the kernel stack to obtain information about the state of the new process. CPU state data such as registers, stack pointer, and program counter are loaded, as well as memory management information such as segmentation tables and page tables (unless the old process shares memory with the new one). To prevent address translation errors in the event that the old and current processes use different memory spaces, the translation lookaside buffer (TLB) must be flushed. This negatively impacts performance, as after most context switches, all memory references in the TLB are lost, as it becomes empty.

Furthermore, a similar context switch occurs between user threads, especially in green threads, and is typically very lightweight, saving and restoring only minimal context. In extreme cases, such as switching between coroutines in Go, a context switch is equivalent to the performance of a coroutine and is only marginally more expensive than a subroutine call.

Process summary

The steps involved in the context change are the following:

  1. Save the context of the process currently running on the CPU. Update the process control block and other important fields.
  2. Move the control block of the previous process to the corresponding queue, such as the ready queue, I/O queue, etc.
  3. Select a new process to run.
  4. Update the control block of the selected process. This includes updating the process status to "running."
  5. Update memory management data structures as needed.
  6. Restore the context of the previously executing process when it is loaded back into the processor. This is done by loading the previous values ​​from the process control block and registers.

Eg the case of linux in particular, which is the system I know best, would be:

  1. The current process running on the CPU reaches a point where it must be interrupted, or an event occurs that requires a context switch, such as a system call or hardware interrupt.
  2. The operating system kernel saves the current process state, including the CPU registers, stack pointer, and program counter, in its process control block (PCB). This is done to preserve the process's state and allow it to resume later.
  3. The kernel selects the next process to run, either from the ready queue or according to a scheduling algorithm. The PCB of the newly selected process is updated, setting its status to running.
  4. The necessary tasks are performed to set up the environment of the new process, such as loading the CPU registers and stack pointer with the values ​​stored on its PCB.
  5. The kernel updates memory structures, such as segmentation tables and page tables, for the new process.
  6. Finally, the new process's context is restored, and it is assigned the CPU for execution. The process resumes execution from where it left off previously.

It's worth noting that this example is a simplification of the actual process that occurs in a Linux context switch. In reality, there are more tasks and considerations involved, such as managing I/O devices and other system resources. Furthermore, the Linux kernel implements various scheduling policies that determine how processes are selected for execution and how CPU time is distributed among them.

I hope this has been helpful, along with the thread changing article… If you are interested in these tutorials, you can see more here.