Post

[OS] Process Abstraction

What is a Process?

A process is a running program in a computer.

Process - Address Space

Each process has its own address space. The address space consists of 4 main parts.

  1. Stack: Stores local variables, arguments to be passed to a function, return address.
  2. Heap: Stores dynamically allocated memory.
  3. Data: Stores static and global variables.
  4. Code: Stoes the code of the program.

Process - Process States

There are 5 essential process states in a process execution life cycle.

  1. Initial: process is created and memory is allocated.
  2. Ready: process is in the ready queue waiting to be executed.
  3. Running: process is being executed by cpu.
  4. Blocked: process is waiting for an I/O operation to finish.
  5. Terminated (Zombie): process has finsihed execution, but waiting for parent process to read the returned code.

Process Control Block (PCB)

In order for the OS to control each process, it needs a process control block to maintain infomration about a process.

Below are some important parts that the PCB includes:

  1. Process Id (PID): Unique Id is assigned to each process.
  2. Program Counter: Pointer pointing to the next instruction (address) that needs to be executed.
  3. Process State: The current state of the process.
  4. Register Context: Stores the content of the latest executed code before being moved out from running state.
  5. Scheduling Information: Process priority, pointers to scheduling queues.

Process Table

The OS needs a process table to manage all the PCB. The process table is a hash table where the key is the PID and the value is a pointer pointing to the PCB for that key. When a process finish execution, the PCB for that process will be removed from the process table.

Process List Structures

The OS also has a ready queue and block queue that stores the processes that are waiting to be executed.

Process Creation

In general, a parent process spawns a new process. This new process is called a child process and it can also create a new process, thus forming a tree.

In modern operating systems, when a parent process is terminated, the child process can proceed independently of the terminated parent process.

Here are the specific steps:

  1. Assign a unique PID and allocate memory.
  2. Put the process in the ready queue.
  3. Initialize the process control block.
  4. Put the process in the ready queue.

Process Termination

A process can be terminated involuntarily:

  • Parent process terminates child process.
  • A number of error and faults leading to termination of process.

After termination, the child process returns the termination status to its parent process. The terminated process’ resources are de-allocated afterwards.

Zombie Process

A process that has terminated but the OS keeps the PCB for that process, so that the parent process can read information later. We call this a zombie process.

Suspending a Process

Unlike blocking where the process is blocked by internal activity of a process, suspending comes from external activity.

Suspending occurs to a process when:

  1. User request.
  2. Request by parent process.
  3. OS suspends a blocked process to free up memory for another ready process.

Signals

Signals also known as software interrupt is used to notify a process that some event has occured.

  1. Synchronous Signal: Triggered by current running process.
    • Illegal memory access, division by zero
  2. Asynchronous Signal: Triggered by external events.
    • Timer interrupt, parent process killing the child process

A process can decide whether to catch, ignore, and mask a signal.

  1. Catch: Use OS’s default action to handle the signal.
  2. Ignore: Inform OS that it does not want to handle the signal.
  3. Masking: Instruct the OS not to deliver signals of that type until the process clears the signal mask.

To respond to a particular signal, a process’ PCB contains a pointer to a vector of signal handlers. Each entry corresponds to the handler function for that signal.

This post is licensed under CC BY 4.0 by the author.