Process Virtualization: Creating the Illusion

Aditya Raghuvanshi
6 min readJun 29, 2024

--

Source : https://stl.tech/blog/introduction-to-virtualization/

Process virtualization is the operating system’s ability to create an illusion that each process has its own CPU, even when there’s only one physical CPU available. This illusion allows multiple processes to run concurrently, making efficient use of system resources.

But how does the OS achieve this magic trick? The answer lies in two key components:

  1. Low-level mechanisms: Hardware support for switching between processes (context switching).
  2. Intelligent software: Algorithms that decide when and how to switch between processes (CPU scheduling).

Each process feels that it has its own CPU

  • Even in Single core machines — There can be multiple process that run at the same time
Computer Hardware : CS3.301 Operating Systems and Networks, IIIT Hyderabad

How is CPU handling this?

The Anatomy of a Process

To understand how the OS manages processes, we need to know what constitutes a process. A process is more than just the program code; it’s a complex entity with several components:

CS3.301 Operating Systems and Networks, IIIT Hyderabad

Memory (Address Space): The memory that the process can address, including:

  • Code segment (instructions)
  • Data segment (static data)
  • Stack (for local variables and function calls)
  • Heap (for dynamically allocated memory)

Registers:

  • Program Counter (PC): Points to the next instruction to be executed
  • Stack Pointer (SP): Points to the top of the process stack
  • Other general-purpose registers

I/O Information:

  • File descriptors for open files and devices

Process ID: A unique identifier for the process

The Life Cycle of a Process

Processes aren’t static entities, they go through various states during their lifetime which are mentioned below :

Process State Transitions : CS3.301 Operating Systems and Networks, IIIT Hyderabad
  1. New: The process is being created.
  2. Ready: The process is waiting to be assigned to a processor.
  3. Running: Instructions are being executed.
  4. Blocked: The process is waiting for some event to occur (e.g., an I/O operation to complete).
  5. Terminated: The process has finished execution.

What features should the OS Provide?

Imagine us making an OS and we have a bucket that we can fill woth whatever powers we want, then what all powers we need for our OS considering that we should be able to run multiple processes!

Create a process

Double click and something just runs

Destroy a process

Force quit, task manager -> end process

Wait

Wait before running

Suspend

Keep the process in pause and resume (eg: Downloading from websites!)

Status

Can we get some status of the process (task manager, system monitor, top)

How to make it happen? — Heard of APIs?

What is an API?

An API, or Application Programming Interface, is a set of protocols, routines, and tools for building software applications. It specifies how software components should interact, providing a way for different programs or systems to communicate with each other.

Think of an API as a contract between two applications, which defines the kinds of requests and data exchanges that can be made between them.

The Operating System API: System Calls

Now that we understand the general concept of APIs, let’s explore how operating systems provide their own API for applications to interact with the system resources: System Calls.

What are System Calls?

System calls are the API that the operating system provides to user-level programs. They form the interface between the operating system and user programs, allowing applications to request services from the operating system kernel.

Why are System Calls Necessary?

System calls serve several crucial purposes:

  1. Resource Management: They allow the OS to manage and allocate system resources efficiently.
  2. Security: By channeling all requests through system calls, the OS can enforce security policies and prevent unauthorized access to hardware.
  3. Abstraction: System calls abstract away the complexities of hardware interactions, providing a uniform interface across different hardware configurations.
  4. Portability: By using standard system calls, programs can be more easily ported between different systems that support the same API.

Types of System Calls

System calls can be broadly categorized into several groups:

  1. Process Control: For creating, terminating, and managing processes.
  2. File Management: For creating, deleting, reading, and writing files.
  3. Device Management: For requesting and releasing devices, reading from and writing to devices.
  4. Information Maintenance: For getting or setting system information.
  5. Communication: For creating and managing inter-process communication channels.

for the sake of not making this article tooo long, lets briefly discuss the process control API calls in this article

The Process API: Controlling Process Lifecycle

Operating systems provide a set of system calls, collectively known as the Process API, to manage processes. These system calls allow programs to create, control, and terminate processes. Let’s explore some key system calls in Unix-like operating systems:

  1. fork(): Creates a new child process that is an exact copy of the parent process.
  2. exec(): Replaces the current process image with a new process image.
  3. wait(): Causes a parent process to pause until one of its child processes terminates.
  4. exit(): Terminates the calling process.

The Fork System Call

The fork() system call is a fundamental operation in Unix-like systems. When a process calls fork():

  1. A new process (child) is created as a copy of the calling process (parent).
  2. Both processes continue execution from the point immediately after the fork() call.
  3. The child process gets a new process ID.
  4. The return value of fork() is different for the parent and child:
  • In the parent process, it returns the child’s PID.
  • In the child process, it returns 0.

This unique behavior allows for powerful and flexible process creation in Unix-like systems.

The Exec System Call

While fork() creates a new process that is a copy of the parent, exec() allows a process to start a completely new program. When a process calls exec():

  1. The current process image is replaced by a new process image.
  2. The new program starts executing from its main function.
  3. The process ID remains the same.

exec() is often used in combination with fork() to create a new process and then run a different program in that process.

The Wait System Call

The wait() system call is crucial for process synchronization and resource management. When a parent process calls wait():

  1. It blocks until one of its child processes terminates.
  2. It collects the exit status of the terminated child.
  3. It allows the operating system to release the resources held by the terminated child process.

Without wait(), terminated child processes would become "zombie" processes, still consuming system resources.

Conclusion

Process virtualization and the Process API are fundamental concepts in operating systems that enable the efficient execution of multiple programs on a single computer. By creating the illusion of infinite CPUs and providing powerful tools for process management, modern operating systems can deliver the responsive, multi-tasking experience we’ve come to expect from our computers.

As we continue our journey through the world of operating systems and networks, we’ll explore more advanced topics, including memory management, file systems, and inter-process communication. Stay tuned for our next article, where we’ll delve into the intricacies of memory virtualization!

These articles are inspired by the Operating Systems and Networks (OSN) course taught at IIIT Hyderabad.

List of articles for OS&N :

  1. Introduction to Operating systems and Networks
  2. Process Virtualization: Creating the Illusion
  3. The Art of Juggling Processes: Unraveling Process Virtualization Mechanisms
  4. Process Scheduling Policies : Part 1
  5. Process Scheduling Policies : Part 2
  6. Networking Fundamentals: Connecting Processes Across Machines
  7. Understanding Networking from Layers to Sockets

I feel extremely happy sharing all this knowledge and do let me know if this article has helped you.

Thank you for reading, I hope this article helped you

Aditya Raghuvanshi ( IIIT Hyderabad, INDIA )

Connect me on the following :

Github | linkedin | Medium | Gmail : tanalpha.aditya@gmail.com

--

--

Aditya Raghuvanshi

AI researcher | NLP and Speech processing | IIIT Hyderabad