Published on

COMP2310 - Week 3 - Processes and Threads

Table of Contents

CPUs and Flows

One CPU per Control Flow

  • Single CPU per control flow

    • Individual stack, code
  • Process management (scheduling) not required as each process runs on its own CPU

  • Coordinated shared memory access is required

  • I.e. important real-time systems such as brakes

CPU per control flow

CPU for all Control Flows

  • Single CPU where each control flow has its own emulated CPU

  • Process management required

  • Coordinated memory access required + memory protection

One CPU for all control flows

Symmetric Multi-Processing - SMP

  • All CPUs share same physical address space + resource access

  • Any process/thread can be executed on any one avaliable CPU

  • Use cases

    • Moving processes across CPUs for heat dissipation and load sharing
    • Big-Little architecture, a powerful CPU with many other less powerfull CPUS
SMP

Processes or Threads?

  • Threads -> A group of processes which can share some resources (Process hierachy)

    • Threads tend to share memory
    • Processes tend not to (specific exceptions apply)
  • Threads have less priority/less expensive than 'first-class-citizen-processes'

  • OS dependent

  • Scheduling

    • User-level -> No kernel support
    • Kernel-level -> Handled as processes with some restrictions

Processes

An instance of a program being executed in a multitasking OS

A process comprises of an address space + control flow(s)

  • Kernel must have full knowledge about all processes -> Allows kernel to protect and schedule access to appropriate resources
    • State
    • Requirements
      • Access
    • Held resources
Processes

Threads

Smallest sequence of programmed instructions that can be managed independently by a scheduler

  • Multiple control flows per process are known as a thread

  • Uses

    • If inside OS
      • Kernel scheduling
      • Thread performs I/O
    • If outside OS
      • User-level scheduling
      • Parent process performs I/O, each thread cannot directly perform I/O as kernel doesn't know about it
Threads

Process Control Blocks (PCBs)

  • Some information is essential
    • Process ID -> Unique identifier
    • Process State
      • Created
      • Ready
      • Executing
      • Blocked
      • Suspended
    • Scheduling attributes
      • Priorities
      • Deadlines
      • Consumed CPU-time
    • CPU State
      • Saved/restored information on context switches (i.e. interrupts)
        • Program counter, stack pointer, ..
    • Memory attributes / privileges
      • Memory base
      • Limits
      • Shared/protected areas
    • Allocated resources / privileges
      • Open or requested files/devices
PCB

Process States

  1. Created

Task ready to run, but is waiting for admission by a dispatcher

  1. Ready

Ready to run and waiting on a avaliable CPU

  1. Running

Holding a CPU and executing

  1. Blocked

Task not ready to run, i.e. waiting for a resource such as I/O

  1. Suspended

Rare. A task is swapped out of main memory (non-time-critical-processes) which are waiting for main memory space or other resources

Dispatching and suspending can now be considered independent modules

Process states
Process state flow

Unix Processes

Unix processes
pid = fork();
if (fork() == 0) {
|| Child process
exec("path_to_exe",args);
exit(0); // Terminate
} else { // If positive value
// Parent process
...
pid = wait();
// Waiting for termination of one child process
}
  1. Duplicates current process, returns 0 to newly created process ('child' process)
  2. Returns process id of child process to creating process ('parent' process)
  3. Returns -1 as indication of failure