Processes, Threads, Resources

Process Concept

Process: Instance of a program being executed.

Parts of a Process:

Program v.s. Process: Programs are passive entities, stored on the disk. Processes are active entities.

Process Control Block

Process Control Block (PCB): The concrete representation of a process.

OS creates a new PCB for every new process.

Organizing PCBs

Two ways to efficiently organize PCBs:

  1. Array of Structures: PCBs marked as free or allocated, and stored which eliminates need for dynamic memory management.
  2. Array of Pointers: Pointers point to dynamically-allocated PCBs.

Linked-List Free Implementation

Why: Linked-list introduces a lot of performance issues and dynamic memory management issues.

Linux solves this by adding two more fields for “left and right sibling”, resulting in:

  1. first_child
  2. left_sibling
  3. right_sibling
  4. parent

This improves time efficiency, though space efficiency remains the same.

TODO Diagram Linked List

Process States and Transitions

Typical Process States:

Example: A very simple diagram of process states
ReadyRunningBlockedOS SchedulingOS SchedulingRequest ResourceRelease Resource

Newly created processes begin in the Ready state.

Example: States when executing a program
Void main()
{
  printf("Hello world");
}

State Sequence:

  1. New: Process is created.
  2. Ready
  3. Running
  4. Blocked: Wait for I/O (printf)
  5. Ready:
  6. Running
  7. Terminated

Context Switch

Context Switch: When CPU transfers control from one process to another.

CPU State: All intermediate values held in any CPU registers and hardware flags at the time of interruption.

Example:

  1. Save state of old process
  2. Load state of new process

Note: The PCB contains an up-to-date copy of CPU state only when a process’s state is ready or blocked.

TODO Diagram detailing context switch

Process Scheduling

Process Scheduler: Selects among available processes for next execution on CPU.

Why?: Process schedulers exist to aid the goals of:

  1. Multiprogramming: Have some process running at all times to maximize CPU utilization.
  2. Timesharing: Switch the CPU among processes frequently so that users can interact with each program while it’s running.

Types of Queues:

Processes migrate among the various queues.

TODO Queuing diagram

Schedulers

Short-Term Scheduler (CPU Scheduler)

Selects which process should be executed next and allocates CPU.

Long-Term Scheduler (Job Scheduler)

Selects which processes should be brought into the ready queue.

Processes can be described as either:

These must be mixed to avoid starving the other.

TODO PlantUML of XXI doodle in my papers

Medium-Term Scheduler

Suspends process from main memory to achieve higher degrees of multiprogramming.

TODO Not everything is wiped from memory, so what exactly is wiped?

TODO Medium term scheduler diagram

Process Operations

There are other operations as well.

Process Creation

Parent process creates a child process, which, can create other processes, forming a tree of processes.

Sharing Options:

  1. Parent and children share all resources.
  2. Children share subset of parent’s resources.
  3. Parent and child share no resources.

fork() system call: Copies variables and registers from parent to child.

Example: fork and exec
pid = fork();

if (pid < 0) {
  // Error
  return 1;
} else if (pid == 0) {
  // Child process
  execlp("/bin/ls","ls",NULL);
} else {
  // Parent process
  wait(NULL);
  printf("Child complete");
}
return 0;

Process Termination

pid = wait(&status)

Some OSes don’t allow children to exist if their parent is terminated.

wait(): Returns status information and PID of process.

Two Scenarios:

  1. Zombie: No parent waiting (did not invoke wait)
  2. Orphan: Parent terminated without invoking wait

Interprocess Communication

Background

Concurrently executing process within a system may be:

  1. Independent: Cannot affect or be affected by another process.
  2. Cooperating: Can affect or be affected by the execution of another process.

Purpose:

IPC

Two models of IPC:

  1. Shared Memory:
  2. Message Passing:

Shared Memory v. Message Passing:

Virtual CPUs

Structuring an application as processes allows for independence from:

  1. Number of CPUs, and
  2. Type of CPU