Processes and Threads

  1. Definition of a Process: A Program in execution
    1. Includes Program Counter, Registers, open file descriptors, memory, stack, etc.
    2. There can be more than one process all running the same program. No problem.
    3. Normally, the state for each process is stored in a process control block.
  2. Scheduling
    1. Switching from one process to another is called a context switch.
    2. Normally takes from 100 ... 1000 instructions. Normally happens 100 ... 1000 times /second
    1. Includes
      1. Switch to kernel state and memory mapping
      2. save applicatiosn regsters
      3. put app on appropriate que (ready, device queue, timer queue, etc)
      4. recompute apps priority
      5. figure out what to run next
      6. switch to that apps conext
      7. load that apps registers
      8. jump back into that app.
    2. It's just overhead
      1. Normally takes hundred or thousands of instructions
      2. Overhead is SwitchInstr * SwitchRate / MIPS
      3. 60 * 1000 / 1000000 = 6%
      4. 60 * 2000 / 100,000,000 = 0.12%
      5. Overhead dependant on hardware support
        1. Decsystem-20 has muliple registers and can change the active register set with one instruction
        2. Sparc has overlapping registers windows and might need to dump/restore 200 registers.

Process Creation and Destruction

  • Process Creation
    1. fork()/exec() model
      1. Primative fork() clones current process in all respects
        1. One process is PARENT, other is CHILD (can tell difference only on fork return code).
          1. Code/bss/file-descriptors all shared
          2. Stack/Data marked Copy-On-Write
        2. Exec() replaces old program with new program
          1. KEEPS SAME PROCESS
          2. Keeps same file descriptors, resource limits, etc.
          3. New code, data, stack
        3. Common paradigm is ..
          1. fork()
            1. if (child)
              1. change file descriptors
              2. exec other program
            2. else // parent
              1. wait for child to finish
      2. System() model.
        1. Name program file you with run
        2. Sometimes can name file descriptors or limits, sometimes not.
      3. Plan 9 model
        1. Plan 9 is from ATT (the Unix people)
        2. Blend of the previous two.
          1. Does fork() like thing, but each element can be copied or not.
          2. Could copy data (for example), but don't have too.
          3. Linux has this (called clone()).
  • Process Termination
    1. Normal termination by exit() call.
      1. Unix/DOS allow one-integer EXIT CODE that normally indicates success/failure
        1. MacOS has no exit code.
    2. Can terminate by
      1. Resource limit violation
      2. Parent exits and system does not allow a child without a parent (VMS).
      3. interrupt
    3. Termination should free code/data/stack/file-descriptors/other-kernel-data

    Threads

    1. Definition: a CPU context sharing within in address space and protection domain.
      1. Threads include program counter, register set, stack space
        1. Threads share code and data sections, open file descriptors, signals, address mappings.
      2. Program considerations:
        1. Auto variables are NOT shared
        2. malloc'ed new'ed variables ARE shared.
        3. File descriptors ARE shared.
        4. Library might screw you (consider errno)