top of page

[Operating Systems] CPU Virtualization - Process

  • Writer: 다영 윤
    다영 윤
  • Mar 3, 2022
  • 3 min read

Updated: Mar 16, 2022

[Reference] 'Operating Systems: Three Easy Pieces' Chapter 4. Abstraction: The Process


This is the start of a series of posts I will make while reading 'Operating Systems: Three Easy Pieces'.


What does the OS do?

An operating system is like a middle man between the system hardware (processor, main memory, hard disk) and user applications. Let's say you are running a web browser, a word editor, and a game on your computer, and each of these applications need to access computer resources such as the processor(e.g. CPU), memory(e.g. DRAM), and disk(e.g. SSD). The OS provides a virtualization of each hardware to user applications, so they can use them without having directly accessing them. Virtualization of each hardware resource is achieved by different mechanisms and policies, and this will be the main focus of this book (how operating systems are implemented).

  • Virtualization of CPU - > Scheduling

  • Virtualization of Main Memory -> Address Translation

  • Virtualization of Disk -> File System



Process

Virtualization of a CPU is needed to run a process...

  1. without letting it having direct access to system resources, but still being able to use them in a safe manner

  2. creating an illusion that there are mutiple CPUs so mutiple processes can run virtually simutaneously

The software solution for number one is to use system calls and two modes of operation, kernel mode and user mode. For the second requisite, we use time sharing, which is implemented by hardware timer interrupts , context switching (a special peice of code run in the timer interrupt handler) and scheduling (deciding which process will be run next).


Processes

We keep saying virtualizaing the CPU has something to do with processes, but what exactly are they? A process is an abstraction of a running program by the operating system. Processes are defined by what it can read and update, which is called the machine state.


Machine State of a Process
  • CPU state: register context (esp: stack pointer, eip: program counter/intstruction pointer)

  • Memory state: address space (instructions / static data / program stack / heap)


Process Creation

How exactly is a process created, physically?

The instructions and static data needed to run a program lives in the disk(ROM, SSD, hard drive). For the program to be run, we need to load this static data and code into the main memory and initialize address sapces and etc for the process. The initialization steps include...

  • initialize run-time stack that stores local variables, return addresses, agruments, etc.

  • initialize heap (OS will manage the heap by allocating/freeing memory when needed)

  • set up I/O (ex. in Unix systems, 3 default file descriptors are opened - standard input/output & error)

Now we jump to the main() routine and transfer control of the CPU to the newly created process!



Process States

The OS has to manage mutiple running processes at once, and to do so it keeps track of each processes state, namely running/ready/blocked.

  • Running: the process is being run by the CPU

  • Ready: the process is being considered as a potential process to be run next

  • Blocked: the process is waiting for a different operation to be completed, otherwise it can't be run or in a ready state. (ex. waiting for a disk read, waiting for a network packet to arrive)



Data Structures: Process List & Process Control Block (PCB)

We have seen that a process consists of its CPU state (registers) and memory state (address space). The data structure for this information is called a PCB(process control block). It contains fields such as...

  • Process ID

  • Process State (running, ready, blocked)

  • Memory State: where the address space is and its size

  • CPU State: register context

  • Open Files


The process structure for xv6 can be found in the proc.h file of its source code.

Now that we have a concrete data structure that can capture the process state, we can stop a process and resume it later by simply restoring its register context and address space that has been stored in the process's proc_struct. In the next post, we cover system calls / limited direct execution protocol and time sharing / context switching.



Comments


  • Facebook
  • Twitter
  • LinkedIn

©2022 by Dayoung Yun. Proudly created with Wix.com

bottom of page