Skip to content

Introduction to Operating Systems

An operating system (OS) is a program that acts as an intermediary between a computer user and the computer hardware (User -- OS -- Hardware).

When your program is running, the OS is not, vice versa

OS examples

Some common OS on computers include Windows 11/10/etc., Mac OS X, Linux Distros (Ubuntu, Fedora, Suse, SlackWare) and Solaris.

On smartphones we have Andriod, iOS, Windows Mobile. Other hardware include game consoles like PS4, XBox, Wii U and home appliances like BlueraY/DVD player, Mio TV console etc.

History of OS

OS evolved with copmuter hardware and user application & pattern. The "first" computers include the Electronic Numerical Integrator And Computer (ENIAC) and Harvard Mark I.

Back then, OS did not really exist and programs directly interact with hardware and was reprogrammed by changing physical configuration (wiring) of hardware. This gave an advantage of minimal overhead, but was not portable nor an efficient use of a computer.

Mainframes

Mainframes were commonly used by large corporations in tehe 60s and 70s. An example is the IBM 360 which costed 5 billion USD in 1964 to develop and 130k USD in 1965 to buy!

The OS for mainframes included the Batch OS which executes user program (aka job) one at a time. The user job still interact with hardware directly with some additional information for the OS. However, simple batch processing is inefficient (CPU is idle when performing I/O). A possible improvement was multiprogramming (run multiple programs at once), and a development during the 70s was Time-Sharing OS.

Time-Sharing OS

Time-Sharing OS allowed multiple user to interact with machine using terminals (teletypes), user job scheduling (illusion of concurrency) and memory management. this is actually similar to Unix servers today!

Some famous examples include the CTSS seveloped at MIT (1960s) and Multics (1970s, considered the parent of Unix). These pushed the state of art in virtual memory and secruity.

timeShareOS

The OS provides sharing of CPU time, memory and storage. Virtualization of hardware meant that each program executes as if it has all the resources to itself.

Minicomputer & Unix

The minicomputer follows the mainframe -- it is a 'mini' version and was smaller and cheaper. An example is the Digital Equipment Corp (DEC) PdP-11

Unix is a very famous OS (developed by AT&T employees like Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna)

Personal Computer

With the machine dedicated to the user, this gave rise to Personal OS and has several models - Windows model (single user) and Unix model (general time sharing). Since Unix was based on a sharing system, it is inherently more secure.

Motivations of OS

Abstraction

There is a large variation in hardware configurations, like different capacity and capabilities (in hard disks). However, despite the different configurations for the devices, hardware in the same category has well defined and common functionality.

The OS gives an interface that on 1 side gives functionalities that are common e.g. go to x part of the drive and write n amount of data, go to y part of the drive and write n amounts of data. On the other side, the OS can issue commands specific to the particular disk drive.

Thus, OS serves as an abstraction to hide the different low level details and present the common high level functionality to the user. The user can then perform essential tasks through the OS without concern of low level details. This provides efficiency and portability.

Resource Allocator

Program execution requires many resources like CPU, memory, I/O devices etc. Multiple programs should be allowed to execute simultaneously, so OS is a resource allocator by managing all resources and arbitrate potentially conflicting requests for efficient and fair resource use.

Control Program

Programs can misuse the computer either accidentally (due to coding bugs) or maliciously (virus, malware) and multiple users can share the computer, making it tricky to ensure separate user space. The OS helps to control execution of programs by preventing errors and improper use of the computer and provide security and protection.

To summarise, the OS has the following uses:

  • Manage resources and coordination
    • process synchronization, resource sharing
  • Simplify programming
    • abstraction of hardware, convenient services
  • Enforce usage policies
  • Security and protection
  • User Program Portability:
    • Across different hardware
  • Efficiency
    • Sophisticated implementations
    • Optimized for particular usage and hardware

OS Structure

Having identified the main capabilities (specifications) of an OS, we now have to consider the best ways to provide these capabilities, i.e. the implementation of the OS. The structure of an OS consists of an organisation of the various components and the important factors include flexibility (can update parts of OS equally), robustness (running upgrades don't cause it to crash) and maintainability (can update frequently).

osCom

Real-world CPUs work in 2 different modes. Kernel and User mode. Under Kernel mode, any program executed has 100% free access to the entire computer. Under user mode, it is restricted. It cannot access hardware on its own and the programs can only access a certain part of memory (accessing other parts results in segmentation fault!). Thus it will require the OS.

OS as a Program

The OS is also known as the kernel. It is a program with special features that deals with hardware issues, provides system call interface and has special code for interrupt handlers and device drivers (provides uniform interface on 1 end and very specific hardware calls on the other).

Kernel code has to be different from normal programs -- no system calls can be made (kernels provide the sys calls, it'll be calling itself! this will cause recursive loop and break the stack). You also cannot use normal libraries (specialised libraries needed) or do normal I/O (direct hardware calls needed).

Since an operating system is just a program, it consists of machine instructions and data which has to be picked up by the CPU for execution. If the CPU has only 1 pipeline, it can only pick up 1 instruction at a time, so when your OS is running, your program cannot run vice versa. We will see how to manage this later.

genOs

As seen in the diagram, the program makes a call(software interrupt, but can think of it as a function call) to the OS. This invokes the OS and causes it to execute. Alternatively, the user program can invoke a library which in turn invokes the OS which casues it to start up (e.g. when you call printf).

Implementing OS

Historically is written in assembly/machine code, and now in high-level languages like C/C++. But this can be heavily hardware architecture dependent. The common code organization means you have machine independent HLL code which uses queues (implementation independent of hardware). Machine dependent HLL is to set up hardware for OS to work. Machine dependent assembly code for context switching (to access CPU registers which HLL cannot)

This is complex, hard to debug and codebase is huge.

OS Structures

There are several wasy to structure an OS such as monolithic and microkernel, layered, client-server, exokernel etc.

Monolithic OS

The kernel can be thought of as 1 big special program (but is not actually compiled as a big program and yes good SE practices to sstructure the program properly is still possible). This is the traditional approach taken by most Unix variants and Windows NT/XP

Linux

Linux is a monolithic OS. But you can add and remove components of the OS. e.g. using insmod to insert a component into the OS or another command to remove a component from the OS.

monoK

In a monolithic kernel, the process, memory and file management and device drivers are operating in kernel mode (so they have full access to all the memory and resources). This can cause problems when buggy code of device drivers etc. crash, bringing down your entire system! This can cause data loss.

Despite this, having everything in kernel mode means you only switch to kernel mode a few times (everything is there, don't need for back and forth) making it inherently more efficient. The only switch is when user mode makes a call to the OS and the OS exits back.

Microkernel OS

The kernel is a very small and clean architecture, provides only basic and essential facilities (if written internally, can be much more secure and reliable) and offers ways to access basic services via inter-process communication (IPC)

microK

The OS is very small and light, consisting mostly of process management components, interrupt handlers (dealing with hardware), some other components and IPC components. Only these are operating in kernel mode and other porcesses operate in user mode as much as possible (certain jobs of memory management has to be done in kernel mode). Since they are run in user mode, any buggy code will have less impact overall.

The downside of this is there is much more switching between user and kernel mode, making it much less efficient. MacOS uses this!

Layered Systems

Layered systems is a generalization of the monolithic system. It organises the system into the following heirarchy of layers:

  • Upper layer: makes use of lower layers
  • Lowest layer: hardware
  • Highest layer: user interface

Client-Server Model

This is a variation of microkernel. It has 2 class of processes:

  • Client process: request service from serer process
  • Server process: built on top of microkernel

The client and server process can be on separate machines!

Virtual Machines

OS assumes total control of the hardware. But what if we want to run several OS on the same hardware at the same time? OS is also hard to debug/monitor. So how do we observe the working of the OS and test potentially destructive implementation? Well, we have virtual machines(VMs)!

Loosely speaking, VMs are a software emulation of hardware. They provide virtualization of the underlying hardware. This gives an illusion that each OS has control of the entire machine. A normal (primitive) operating system can then run on top of the virtual machine.

You may face the problem of sharing the memory among different OSes. How do you share the 1 piece of hardware? We have a layer called Hypervisor to handle these complexities..

Hypervisor Implementation

hv1

Type 1 hypervisor OS provides individual virtual machines to guest OSes. In other words, the CPU itself provides support (special instructions) for virtualization. It switches into a mode that can virtualize hardware to support multiple OSes. Thus the hypervisor can interact directly with the CPU hardware and give support to multiple OS.

hv2

Type 2 hypervisor OS runs in host OS while the guest OS runs inside the VM. Type 2 hypervisors can provide a machine emulation or perform emulation by re-mapping calls from the guest OS to the host OS. This allows for guest OS to be run on the host OS.

1 key problem of running on the host OS is that this is very slow.

Intel chips

Intel chips allow for nested virtualization. This means you can run a VM inside another VM.