Skip to content

Instantly share code, notes, and snippets.

@mtilson
Last active August 1, 2020 13:15
Show Gist options
  • Save mtilson/43569b762dd2660bc4401ff63fd32d08 to your computer and use it in GitHub Desktop.
Save mtilson/43569b762dd2660bc4401ff63fd32d08 to your computer and use it in GitHub Desktop.
what are basic process management primitives in linux kernel [linux] [kernel]

Linux makes a distinction between process and program executing

System calls

  • exec()-like
    • related to program executing
    • load a new program
    • after the call the process resumes execution with a brand new address space containing the loaded program
  • fork()
    • related to process executing
    • create a new process
      • the process that invokes a fork() is the parent process
      • the new process is the child process related to the process that invokes a fork()
    • data structure describing each process includes
      • a pointer to its immediate parent process
      • pointers to all its immediate children processes
    • data and code
      • naive implementation
        • require both the parent’s data and the parent’s code to be duplicated and the copies assigned to the child
        • it is quite time consuming
      • current implementation
        • applied for kernels that can rely on hardware paging units
        • follow the Copy-On-Write approach
          • defers page duplication until the last moment, until the parent or the child is required to write into a page
  • wait4()
    • related to process executing
    • allows a process inquire about termination of its children and wait until one of its children terminate
      • the kernel checks whether any child of the process has already terminated (the system call handler extracts data about resource usage from the process descriptor fields)
        • if any child process has already terminated, the kernel returns its PID (process ID)
        • if no child process has already terminated, the kernel usually puts the process in a wait state until a child terminates
    • when the child is terminated, special zombie process state is introduced to represent this terminated processes
      • the process remains in that state until its parent process executes a wait4() system call on it
    • when a process having existing children terminates, the kernel changes the appropriate process descriptor pointers of all the existing children of the terminated process to make them become children of special system process init
      • this special system process monitors the execution of all its children and routinely issues wait4() system calls, whose side effect is to get rid of all orphaned zombies
  • _exit()
    • related to process executing
    • terminate a process
    • kernel handles this system call by
      • releasing the resources owned by the process
      • sending the parent process a SIGCHLD signal, which is ignored by default

Linux process groups and login sessions

Process group

  • represents a job abstraction
  • bash$ ls | sort | more
    • new group is created for the three processes corresponding to ls, sort, and more
    • each process descriptor of the above three processes (ls, sort, more) includes a field containing the process group ID
  • group of processes may have a group leader
    • PID of group leader process coincides with the process group ID
  • any newly created process is initially inserted into the process group of its parent

Login session

  • contains all processes that are descendants of the process (it is usually, the first command shell process created for the user) that has started a working session on a specific terminal
  • all processes in a process group must be in the same login session
  • a login session may have several process groups active simultaneously
    • one of these process groups is always in the foreground, which means that it has access to the terminal
    • other active process groups are in the background
      • if a background process tries to access the terminal, it receives a SIGTTIN or SIGTTOUT signal

See also

Basic Operating System Concepts

Computer system includes a basic set of programs

  • Operating system
    • is the most important program in the set of computer system programs (it is called the kernel)
    • is loaded into RAM when the system boots and contains many critical procedures that are needed for the system to operate
  • Other programs
    • are less crucial utilities
    • can provide a wide variety of interactive experiences for the user
    • do all the jobs the user bought the computer for

User and Kernel Modes

  • Unix-like operating system hides all low-level details concerning the physical organization of the computer from applications run by the user
  • When a program wants to use a hardware resource, it must issue a request to the operating system (via a system call)
  • The kernel evaluates the request and, if it chooses to grant the resource, interacts with the proper hardware components on behalf of the user program
  • To enforce this mechanism, modern operating systems rely on the availability of specific hardware features that forbid user programs to directly interact with low-level hardware components or to access arbitrary memory locations
  • The hardware introduces at least two different execution modes for the CPU
    • User Mode: nonprivileged mode for user programs
    • Kernel Mode: privileged mode for the kernel
  • Each CPU model provides special instructions to switch from User Mode to Kernel Mode and vice versa
    • Some CPUs can have more than two execution states
      • For instance, the 80x86 microprocessors have four different execution states
      • But all standard Unix kernels use only Kernel Mode and User Mode
  • A program usually executes in User Mode and switches to Kernel Mode only when requesting a service provided by the kernel
    • When the kernel has satisfied the program’s request, it puts the program back in User Mode
    • When a program is executed in User Mode, it cannot directly access the kernel data structures or the kernel programs
    • When an application executes in Kernel Mode, these restrictions no longer apply

Process abstraction

  • Process
    • An instance of a program in execution or execution context of a running program
      • In traditional OSes, a process executes a single sequence of instructions in an address space
      • Modern OSes allow processes with multiple execution flows: multiple sequences of instructions executed in the same address space
  • Address space
    • Set of memory addresses that the process is allowed to reference
    • Each process runs in its private address space
      • A process running in User Mode refers to
        • private stack area
        • data area
        • code area
      • When running in Kernel Mode the process addresses
        • kernel data areas
        • code data areas
        • another private stack

Process Implementation in relation to User and Kernel Modes

  • Processes are dynamic entities that usually have a limited life span within the system
    • The task of creating, eliminating, and synchronizing the existing processes is delegated to a group of routines in the kernel
    • The kernel itself is not a process but a process manager
  • The process/kernel model (Process/Kernel Model means a CPU can run in either User Mode or Kernel Mode) assumes that processes that require a kernel service use specific programming constructs called system calls
    • Each system call sets up the group of parameters that identifies the process request and then executes the hardware-dependent CPU instruction to switch from User Mode to Kernel Mode
    • Whenever a process makes a system call (request to the kernel), the hardware changes the privilege mode from User Mode to Kernel Mode and the process starts the execution of a kernel procedure with a strictly limited purpose
      • OS acts within the execution context of the process in order to satisfy its request
      • As the request is fully satisfied, the kernel procedure forces the hardware to return to User Mode and the process continues its execution from the instruction following the system call
  • To let the kernel manage processes, each process is represented by a process descriptor that includes information about the current state of the process
    • When the kernel stops the execution of a process, it saves the current contents of several CPU registers in the process descriptor, including:
      • The program counter (PC) register
      • The stack pointer (SP) register
      • The general purpose registers
      • The floating point registers
      • The processor control registers (Processor Status Word)c
        • Contains information about the CPU state
      • The memory management registers
        • Used to keep track of the RAM accessed by the process
    • When the kernel decides to resume executing a process, it uses the proper process descriptor fields to load the CPU registers
  • When a process is not executing on the CPU, it is waiting for some event
    • Unix kernels distinguish many wait states, which are usually implemented by queues of process descriptors
    • Each (possibly empty) queue corresponds to the set of processes waiting for a specific event

Other Abstractions and Primitives of Unix Kernels

Reentrancy and Synchronization

  • Kernel control path
    • The sequence of instructions executed by the kernel to handle
      • system call
      • exception
      • interrupt
  • Critical region
    • Any section of code that should be finished by each process that begins it before another process can enter it

Multithreaded application

  • Multithreaded applications
    • user programs that are designed in terms of many relatively independent execution flows that share a large portion of the application data structures
  • Lightweight processes (LWP) are what multithreaded user application could be composed of, and which can operate on
    • common address space
    • common physical memory pages
    • common opened files
    • etc
  • Linux regards lightweight processes as the basic execution context and handles them via the nonstandard clone() system call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment