Processes

A program in execution Program + process state (current instruction, state of memory)

Terminology

  • Program: A sequence of instructions
  • Job: A task to be completed
    • Often a program (or many) awaiting execution or a process during execution
    • A sequence of processes

The Process Model

  • Unique program counter
    • It needs at least one thread of execution (memory space, stack, program pointer)
  • Independence of execution
    • A process does not depend on any other processes executing
    • What executes is independent of any other values, unless there is explicit communication

Pseudo-parallelism

  • Imitates parallel execution on a single processor
  • Each process uses the CPU by taking turns
  • Still used today with multi-core processors (because number of processes >> number of cores)
  • Uses the Scheduler

What is in a Process

  • Process Management
    • Process table (state & attributes)
    • Register values
  • Memory Management
    • Text (instructions) pointer
    • Data pointer
    • Heap pointer
    • Stack pointer
  • File Management
    • Files
    • Pipes
    • Devices

Memory

Each process has its own address space

  • text
    • code that the process is executing
  • data
    • initialized static and global variables
    • uninitialized static and global variables (bss) — separate because an entire page of zeros can be optimized
    • heap (grows and shrinks)
  • stack
    • activation frames of the function calls made
    • argument and environment variables

Inter-Process Communication

  • Why
    • Coordination of actions
    • Transmission of data
    • Notification of event
    • Updating status
  • How
    • Shared Memory
      • Directly reading and writing to memory
      • Requires shared space
      • Caching & consistency issues
      • Requires explicit synchronization
    • Message Passing
      • Library or OS support needed
      • System handles synchronization
      • Avoids memory-specific problems
      • Require a communication protocol
        • Connecting to & addressing recipients
        • Formatting and encoding of information
        • Data buffering & flow control
        • Message delivery & ordering
        • Synchronization
      • Common approaches
        • Pipes
          • Between two processes on the same system
          • Handled by the kernel
          • Usually a file-based transaction (file is controlled by the os)
          • The OS just puts two processes in communication with each other and then lets them do their own thing
        • Network Sockets
          • Similar to pipes
          • But information can be exchanged between the same host or different hosts
          • Uses a buffer managed by the OS which is slower but more versatile
  • Synchronization

State

Controlling State

  • Creation
    • System initialization (init)
    • Process request (fork)
      • Creates a child process of the process that called fork
  • Termination
    • Normal Exit (voluntary)
      • I did everything, I’m done, all is well
    • Abnormal exit (voluntary)
      • Aborting on purpose
      • Exception handled by process
    • Fatal error (involuntary)
      • Uncaught exception caught by OS
    • Signaling/kill via another process (involuntary)
      • Software signal coming from another process kills the process
      • Handled at the start of the target process turn to run (so it has an opportunity to clean up before shutting down)

States

  • Primary
    • Running
      • Executing instructions
    • Ready
      • Runnable
      • Not currently executing instructions
    • Blocked
      • Waiting on an external event
      • Has yielded the CPU
  • Swapped
    • Swapped Out & Ready/Blocked
      • Not in RAM
      • Process copied to disk to save memory
  • Lifecycle
    • Initializing
      • Preparing process to run
    • Exiting
      • Shutting down
      • Cleaning up following execution

Hierarchy

  • init
    • shell
      • processes that shell spawns
        • processes that those processes spawn, etc…

Termination

  • Methods
    • returning from main
      • 0 success
      • 0 other cases

    • calling the _exit system call
    • calling the exit() library function
      1. calls termination handlers
      2. flushes the stdio buffers
      3. calls _exit

Various Syscalls

exec

  • Replaces the process with a new executable (reseting the virtual address space)
  • If successful, would never return
  • Leaves FDT untouched
  • Often used after a fork so it the child getting replaced
  • Variations
    • l args are a null terminated list
    • v args are a null terminated array
    • p rely on PATH to find exec (no p means find the full path)
    • e last arg provides the env vars (no e means keeps the existing env)

fork

  • Spawn a new child process
  • Copies virtual address space and file descriptor table (points to same entries in the file table, just with refcount incremented)
  • Copies environment variables
  • When a child process ends without the parent waiting for it, it becomes a zombie
    • After zombies are orphaned, init waits for it and then kills it
    • Zombies cannot be killed by the parent

wait and waitpid

  • Wait for a child process to finish
  • wait(NULL) and wait(-1, NULL) are the same

kill

  • End a process
  • When a process is killed, its children become orphaned and adopted by the init process

Function Pointers

  • Can tell certain functions to run when events happen
  • Can register multiple functions for a single event
    • Runs in opposite order registered
  • Examples
    • atexit
    • onexit
      • uses fn(int exitStatus, void* args)
      • args are a void pointer that is provided when registering the function
  • atexit and onexit run at the same time, onexit is just a nonstandard gnu extension

Environment Variables

  • Form of interprocess communication
  • Inherited from the parent
  • Each variable is in the form NAME=VALUE
  • Access via
    • extern char** environ global variable
    • adding char *env[] as a third parameter of main
  • Examples
    • PATH where to search for programs to run
    • SHELL path to shell to use
  • Stdlib functions to interact
    • clearenv()
      • Note: sets environ to NULL
    • putenv(char* string)
    • setenv(char* name, char* value, int overwrite)
    • unsetenv(char* name)