Skip to content

What is an os

Kyle Terrien edited this page Mar 18, 2017 · 4 revisions

An operating system (OS) is software that makes your machine usable.

  • It talks to the hardware
  • It provides a method to load and run other userspace software.
  • It provides services to userspace software, the most important of which is a file system.

This page is intended to give a quick introduction to operating system concepts so that you can find more information online. It is written with Linux/Unix in mind. However, concepts apply to other (inferior) operating systems.

Kernel

The kernel is the gut of the operating system. It is the first program that loads during boot.

The kernel is the software that talks to the hardware. Specifically, device drivers talk to hardware.

The kernel provides services to applications in the form of syscalls. For instance, if an application wants to write to a file, then it makes a syscall to the kernel to ask it for a file handle.

The kernel also handles context switching (aka multiprocessing). There are several ways to do this, but it generally comes down to interleaving between many processes at once.

  • Non-preemptive - Each process tells the kernel when it may context switch between processes. MacOS (before OS X) and older versions of Windows used non-preemptive scheduling. There is one big problem with this method: a process can hold execution however long it wants.
  • Preemptive - The kernel sets a hardware timer interrupt to some time, loads a process, and then runs it. If the process does not finish before the timer goes off, then it is suspended, the timer is reset, and another process is loaded. This is the method that Linux/Unix uses.

Userspace

Software that is not part of the kernel is said to be userspace. Userspace programs do useful stuff.

  • On a Linux system, the most critical userspace utilities are GNU coreutils or busybox.
  • On BSD systems, the userspace utilities and kernel are developed as part of the same project.

The kernel provides a mechanism to run userspace programs at will. Nowadays, multiple programs can be run at once. The kernel manages the lifecycle of each userspace process.

Init system

In Linux, the init process is the first userspace process to run. It is called after the kernel enumerates all hardware and loads all critical drivers.

Traditionally, init calls a shell script that launches daemons and ultimately starts getty (to display a text login prompt) or a display manager (e.g. gdm to display a graphical login screen). After spawning the master shell script, init occasionally calls wait() on orphaned processes to clean up their meta-information.

Init runs for the entire lifetime of the system. If it dies, bad things will happen. (Some systems kernel panic if init dies.)

In modern Linux distributions, init has been replaced by systemd, which manages daemons in a dependency-based manner.

Process states: orphaned and zombie processes

A program is a blob of code that is executed. When a program is executed, a process is created with a unique id (a PID). A process executes and eventually ends. During its execution, a process could be in one of many states:

  • Running - The process is actually executing instructions right now in a CPU core.
  • Suspended - The process is not running. It could have been preempted so that another process could get CPU time.
  • Waiting on IO (or any other syscall) - The process is waiting for an input/output operation to complete. It could be waiting for input from the user in the terminal, or it could be waiting for the hard disk to fetch a block of data.
  • Orphaned - The parent of the process has terminated. The process is still running.
  • Zombie - The process has terminated. However, its parent has not acknowledged its exit code.

The last two states are particularly confusing to newcomers.

Processes are tracked in a tree structure. When a process calls another program, a child process is created and tracked. Normally, the child process exits, and the parent process acknowledges its exit code by calling wait() on the child process. After the parent acknowledges the exit code, the child process is removed from the process tree, and its PID is freed.

However, other situations can happen. The parent could terminate before the child. If the parent terminates while the child is still running, then the child is reparented to the init process (PID 1). The child process is said to be orphaned. (This technique is called daemonization and is used extensively during system startup to start background services.)

Alternatively, the child process can terminate, and the parent can continue an indefinite time before calling wait() on the child. In this case, the child process is said to be zombified. The only information left in this state is the return status, which must be returned when the parent calls wait(). Zombie processes cannot be killed because there is nothing to kill; all they are is meta-information.

But there is another edge case. A child process could terminate, and then the parent process could terminate without calling wait(). (Generally, this only happens in poorly coded programs.) In this case, the program is zombified and then orphaned. The zombie orphan gets reparented to PID 1 (init). Fortunately, PID 1 calls wait() on its children every now and then. If it didn't, there could eventually be a zombie uprising in the system.

Memory segmentation

TODO

Desktop

The desktop is the software that displays a graphical environment. On Linux systems, it is generally a large collection of utilities (termed desktop environment).

A Linux/Unix desktop has several layers:

  • Xorg - X11 display server. It handles graphical drivers and provides a keyboard/mouse based environment on which to draw windows. X11 is also network transparent, meaning X commands can be sent across the network to a remote display.
    • The notion of "server" is counterintuitive. The server is the process handling the display. Applications are "clients" that connect to the X server in order to use its services (drawing to the display).
    • X11 and X usually refer to the protocol used for communication with the X server. Xorg is an implementation of the X11 protocol.
  • Window manager - the program that is responsible for drawing window captions and providing mechanisms to move/resize windows. Some are standalone; some run in particular desktop environments. Examples: metacity, marco, kwin, xfwm4, fvwm, openbox, wmaker
  • Desktop environment - A collection of utilities that typically include a window manager, configuration utilities, and other useful software. Examples: GNOME, MATE, KDE, XFCE