COMP 3511: Lecture 6

Date: 2024-09-19 14:59:46

Reviewed:

Topic / Chapter:

summary

❓Questions

Notes

Process Concept (cont.)
  • Process state

    • traditional Unix: each process = 1 thread
      • process state current activity of that process
    • as process executes: it changes states
      • new: process being created / allocating resource
      • ready: process waiting to be assigned to CPU
        • πŸ‘¨β€πŸ« remember ready queue and CPU core!
      • running: instructions being fetched & executed
      • waiting: process waiting for event to occur
      • terminated: process finished execution / deallocating resource
      • ❓ how do process manager work? how much CPU resource does it take?
        • πŸ‘¨β€πŸ« will be discussed later (whole chapter)
    • 01_process_states
    • different things might happen in running state
      • exit: finished all tasks
      • interrupt: after each line of instruction: checks interrupt signal
        • if detected: add interrupt routine to process queue
      • I/O || event: I/O service, etc: are much slower than CPU
        • so the process gives up CPU itself, and waits for signal (from IO / etc.)
  • Process control block (PCB)

    • each process: corresponds to 1 PCB
      • i.e. info. associated w/ each process
      • aka: task control block
    • includes following info:
      • process state: running / waiting, etc.
      • program counter: location of instruction to be fetched next
      • CPU registers: accumulators, index resisters, stack pointers, general-purpose registers, condition-code information
      • CPU scheduling info.: priorities, pointers to scheduling queue, scheduling parameters
      • memory-management info.: memory allocated to the process
        • complicated data structure: paging & segmentation tables
        • πŸ‘¨β€πŸ« for now, consider it as a pointer to program data structure
        • πŸ‘¨β€πŸ« program: simple binary; process: dynamic!
      • accounting info.: CPU used, clock time elapsed, time limits
        • simple statistics
      • IO status info.: IO devices allocated to process
        • e.g. list of open files
    • 02_pcb_diagram
  • Threads

    • we assumed: a process w/ single thread of execution
      • thread: represented by: PC, registers, execution flags, stack
    • modern OS: allow a process w/ multiple threads
      • thus can perform parallel execution
      • e.g. browsers: contacting web server vs. displaying image, etc.
      • w/ multi-core systems: multiple threads of process: can run in parallel
    • process: can consist of 1 / multiple threads
      • running within the same address space (e.g. sharing code & data)
      • each thread: w/ its own, unique state
    • multi-threads: process providing concurrency & active components
      • 06_single_vs_multi_thread
  • Process representation in Linux

    • Linux: storing list of processes in circular doubly LL
      • aka task list
      • each element in task list: of type struct task_struct
        • i.e. PCB
    pid t_pid;
    long state;
    unsigned int time_slice;
    struct task_struct *parent /* TBD */
    struct list_head children /* TBD */
    struct files_struct *files /* TBD */
    struct task_struct *parent /* TBD */
    
Process Scheduling
  • Process schedule

    • primary object of multiprogramming: keep CPU busy
      • as it's a scars resource
    • process scheduler: OS mechanism selecting a process
      • from available processes inside main memory
      • scheduling criteria: CPU utilization, job response time, fairness, real-time guarantee, latency optimization...
    • degree of multiprogramming: no. of processes currently residing in memory
      • determines resource consumption
    • processes: roughly either:
      • I/O bound process: spends more time w/ IO than computations
        • many short CPU bursts
      • CPU bound process: generates IO requests infrequently
        • more time doing computations; few, very long CPU bursts
  • Scheduling queue

    • OS: maintains different scheduling queues of processes
      • ready queue: set of processes in main memory;
        • ready & waiting to execute
      • wait queue: set of processes waiting for an event
    • processes: migrate among various queues during lifetime
  • Representation of process scheduling

    • 03_process_representation
  • Schedulers

    • long-term scheduler (job scheduler)
      • for scientific & heavy computation
      • selects which processes to be brought to memory
      • for mainframe minicomputers (not PC)
        • made of job queue: hosts job submitted to mainframe computers
    • short-term scheduler (CPU scheduler)
      • selects a process from a ready queue to be executed next & allocate CPU for it
    • short-term scheduler: invoked frequently (ΞΌs, must be fast)
    • long-term scheduler: invoked infrequently (s,min, may be slow)
    • long term scheduler: dictates degree of multiprogramming
    • mainframe computer system:
  • Medium term scheduling

    • if you underestimated the program resource:
      • swap out (not terminating) some programs
        • entire process information (not just PCB)
      • and use the freed up resource to accelerate & give more space
    • for PC: you can simply kill the resources
      • for mainstream: it's all services that's paid
      • mostly not implemented for PC
  • Inter-process CPU switch

    • context switch: when CPU switches from one process to another
      • sample OS task
        • save state into PCB-0
        • reload state from PCB-1
        • save state into PCB-1
        • reload from PCB-0
        • ...
    • context: must be saved (in PCB / in each thread of process)
      • typically: including registers, PC, stack pointer
    • context-switch time: an overhead
      • system: doesn't do useful work during it
      • takes longer if OS & PCBs are complex
    • switching speed: depends on
      • memory speed
      • no. of registers copied
      • existence of a special instructions to load & store all registers
    • speed: highly dependent on underlying hardware
      • e.g. some processors: provide multiple sets of registers
      • context switch: simply changing the pointer to a different register set
Dual-mode Operation
  • Dual mode operation

    • allows OS to protect itself & other system components
    • πŸ‘¨β€πŸ« MS-DOS didn't have it; Windows have
    • user mode: has less access / permission to hardware than the kernel mode
    • can be easily extended to multiple modes!
    • ❓ sudo = executing as the lowermost user?
    • dual-mode operation provides: basic means of OS protection & users
      • from errant / bad users
    • all access: must be strictly controlled by well-defined OS API
    • switching between modes: constant
      • e.g. (user) API call to open -> (kernel) open -> (user) displays
  • Three types of mode transfer

    • 3 common ways to access kernel mode from user mode
    • system call:
    • interrupt:
    • trap / exception:
Operations on Processes
  • Operations on processes

    • processes in most systems: execute concurrently;
    • created & deleted dynamically; thus OS must support
      • process creation: running a new program
      • process termination: program execute completes / terminate
    • parent process: can create children processes
      • children: can create other child processes
      • tree of processes!
    • πŸ‘¨β€πŸ« shell: also a process!
      • waiting for your input
      • all commands running on it: children process of shell
    • desktop: can be a parent process
      • process started on it (e.g. by double click): its children process
    • the grandpa: root process
  • Tree of processes on Linux system

    • 04_process_tree
  • Process creation

    • most OS: identify process according to unique process identifier
      • or: pid, usually an integer
    • child process: needs certain resources
      • CPU time, memory, files, IO devices to accomplish task
      • parent process: also pass along initialization dat (input)
        • to child process
    • parent & children: share "almost" all resources
    • three choices possible on how to share the resource
      1. parent & children: share almost all resources: fork()
      2. children: share only subset of parent's resources: clone()
      3. parent & children: share no resources
    • two options for execution after process creation
      1. parent: continues to execute concurrently with its children
      2. parent: wait until some / all its children to terminate
    • again, two choices of address space for a new child process
      • child process: duplicates of parent (same program & data) - Unix/Linux
      • child process: has a new program loaded into it (different program & data) - Windows
    • UNIX examples
      • fork(): creates a new process; duplicates entire address space of the parent
        • both processes: continue execution at the next instruction after fork()
      • exec(): maybe used after fork(): to replace child process's address space w/ a new program
        • loading a new program
      • parent: can call wait() to wait for a child to finish
    • 05_exec_example