Processes

The Process Concept

What is a Process?

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.

The Need for Processes (Virtual CPUs)

Structuring an application as processes allows for independence from:

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

Process Management

Process Control Block

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

PCB Contents

Example: PCB Data Structure
PCBCPU_stateSet of integersprocess_stateInteger or charmemoryPointersscheduling_informationSet of integersaccounting_informationSet of integersopen_filesStart of linked listother_resourcesStart of linked listparentPointer or indexchildrenStart of linked list

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.

Linux PCB Implementation (Linked-List Free)

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

  1. parent: Points to process’s parent (same as before)
  2. first_child: Points to process’s first child
  3. younger_sibling (left sibling): Points to sibling of process, created immediately following this process.
  4. older_sibling (right sibling): Points to sibling of process, created immediately prior to this process.

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

Process States and Transitions

Process States

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.

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

Example: A simple context switch
process P₀process P₀operating systemoperating systemprocess P₁process P₀operating systemprocess P₁process P₀process P₀operating systemoperating systemprocess P₁process P₁process P₀process P₀operating systemoperating systemprocess P₁executinginterrupt or system callsave state into PCB₀(do stuff...)reload state from PCB₁executingidleinterrupt or system callsave state into PCB₁(do stuff...)reload state from PCB₀executingidle

Example Steps:

  1. Save state of old process (P1)
  2. Load state of new process (P2)

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.

Scheduling Queues

Types of Queues

Processes migrate among the various queues.

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.

More on Process Mix:

Processes can be described as either:

These must be mixed to avoid starving the other.

Medium-Term Scheduler

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

More on Swapping Out — When a process is swapped out, its entire memory image (Text, Data, Stack, and Heap sections) is moved from main memory (RAM) to a backing store (like a disk).

When swapping back in, the memory image is loaded back into RAM (not necessarily in the same location), and the process eventually becomes Ready again.

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.

Resource 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() and exec() System Calls

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

exec(): Can be used after fork to replace process’s memory space with a new program.

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

Design Choice — Can children live without their parents?

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

wait() System Call

pid = wait(&status)

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

Zombie and Orphan Processes

Two Scenarios:

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

Interprocess Communication

Background and Purpose

Concurrently executing processes within a system may be:

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

Why do IPC?

IPC Models

Two models of IPC:

  1. Shared Memory
  2. Message Passing

Shared Memory v. Message Passing:

Example: Producer-Consumer Problem

Producer makes info used by the consumer

Shared Memory is a solution to the producer-consumer problem.

Naive Producer:

item next_produced;

while(true) {
    while (((in + 1) % BUFFER_SIZE == out)
        ; /* do nothing */
    
    buffer[in] = next_produced;
    in = (in + 1) % BUFFER_SIZE;
}

Naive Consumer:

item next_consumed;

while (true) {
    while (in == out)
        ; /* do nothing */
    
    next_consumed = buffer[out];
    out = (out + 1) % BUFFER_SIZE

    /* consume the item in next_consumed */
}

This code uses the following:

This code doesn’t synchronize the producer and consumer, so if the consumer tries to consume an item that hasn’t been produced, it’ll fail. We’ll discuss process synchronization in a future chapter, as this has just been a short sample.

Message Passing

Message System: Processes communicate without sharing variables.

IPC provides two operations:

Synchronization (Blocking v. Non-Blocking)

Synchronization: Message passing may blocking or non-blocking.

1. Blocking (Sync)

2. Non-Blocking (Async)