Introduction to Operating System

The operating system is resource management software.

OS manages the hardware and acts as the sole coordinator between software and hardware components. OS is responsible for computer security and ensuring efficient use of processors, memory, and persistent storage.

There are three (popular) types of OS available currently in the market:

  1. Linux
  2. macOS
  3. Windows

Linux

Linux was designed and developed using the basic principles of the Unix operating system. Unix was a proprietary operating system developed by Ken Thompson, Dennis Ritchie, and others. Later an open-source version of Unix was developed by the FreeBSD organization.

Ubuntu, CentOS, Fedora, REHL are some of the most popular OS built using the Linux kernel.

Linux kernel is the primary component of the Linux OS that provides the core interface between a computer’s hardware and its software/processes.

MacOS

macOS was originally developed using Mach kernel which was based on the FreeBSD Unix version. Later the improved macOS kernel was named as XNU kernel. A free version of an OS named Darwin developed using the XNU kernel was released by Apple.

On top of Darwin Apple has added many proprietary layers including the Aqua Interface, Finder, etc, which is now known as macOS used across Apple computers.

Windows

The early version of windows was developed on MS-DOS. Later Windows versions were migrated to POSIX compatible OS/2.

Later versions of Windows including Windows-7/8/10/11 use a hybrid kernel, which is a combination of microkernel and monolithic kernel.

What does a OS do?

OS provides interfaces for the software to make efficient and secure use of the hardware resources.

OS makes it seamless for the user to do work on the computer by hiding the subtle complexities involved.

OS makes it possible to do multitask even with a single processor.

These are the four primary responsibilities or functionalities of an OS:

  1. Virtualization,
  2. Concurrency
  3. Persistence
  4. Security

Following diagram shows a very high level overview of an Operating System. OS acting as a resource manager of the available hardware components letting multiple users (running multiple programs) use the system.

Virtualization

Virtualization enables OS to share the CPU/memory among multiple programs to do multitasking or execute multiple programs in parallel.

OS uses an abstraction called Process to execute multiple programs in parallel. One program can have one or more processes. Though process OS creates an illusion of multiple virtual CPUs running different programs.

Though processes are executed serially, the illusion of multiple programs running at the same time is made possible through executing chunks of different processes one by one, which enables multiple programs to use the CPU without waiting for a particular program to finish.

OS makes use of Job Scheduler to schedule processes across one or more processors. Job schedular make sure that no programs are starved and processors are utilized efficiently.

Job schedular maintains one/more queues to keep track of processes requesting access to CPU to execute code and select one (more based on the number of processors) to execute in the CPU. Job schedular makes sure that all processes get fair access to the CPU(s). Job schedular strives to balance the response time and the runtime of different processes waiting in the execution queue.

Different processes are decoupled from each other, each of the processes has its virtual memory called the address space which mimics the physical memory. Though address space memory is virtualized and enables different processes to use the physical memory efficiently.

OS makes use of hardware level interrupts to facilitate CPU time sharing.

Following diagram shows a very high level overview of virtualization of CPU. OS enables CPU time sharing among multiple processes, such that all processes can get access to the CPU(s) without waiting for other processes to complete.

What happens when you run a program?

When a user runs a program through the command line interface or double click on an application icon, the process running the CLI or the window manager, creates a new process using a fork and load the program code and data into memory and call exec to execute the program on the CPU.

Concurrency

OS provides another abstraction called Threads to implement concurrency or parallelism. Threads can be considered as a separate process with one exception that it shares the virtual memory or address space the process that creates the threads. But the thread has its stack for variables and program counter. For a single-CPU concurrency is achieved by time-shares from OS through virtualization.

Concurrency is useful when the computer has more than one processor (multiple cores). A process can spawn multiple threads that can make sure of multiple processors simultaneously to execute independent parts of the programs. Since the threads share the process address space, special care needs to be taken when accessing or updating process data.

OS provides locks, condition variables, semaphores (which can be used as locks and condition variables) to prevent accessing critical data simultaneously by different threads. The issue of multiple threads updating the same variable simultaneously is called race condition (data race), which can result in unpredicted results from a program.

OS exposes semaphores to avoid race conditions through the mutual exclusion of the critical section of the code (the section code responsible for race condition).

Following diagram shows a very high level overview of a single threaded process.

Following diagram shows a very high level overview of a Multi threaded process. It is the user responsibility is to avoid data race.

Persistence

OS provides another abstraction called the file-system to manage persistence storage.

When a user runs a program on the computer, the OS loads the code and data into memory creating a process. Then the OS schedule the execution of the process on the CPU, where instructions (code) are executed one by one. Variables or data created during the execution of the process do not persist in the memory once the program is unloaded unless the required data are stored in some persistence storage devices. The term memory means RAM/ROM.

OS-provided file-system is used to securely manage persistent storage devices such as disk or flash drives.

The purpose of the file system is to make sure the followings:

  1. No unauthorized read/write/execute

  2. When a user request to write or read data to/from persistence storage, the control of the software is transferred to the OS kernel (kernel-mode through context switching), kernel then decide whether the user has required permission to perform that operation, if yes then the operation returns, otherwise the process is killed.

  3. Efficient read and write operations

  4. File-system makes sure that reading and writing are faster through the uses of different algorithms and data structures. Multiple writes are batched together and executed for efficiency.

  5. Efficient utilisations of the disk space

  6. File-system makes sure that when a file is deleted, the disk space is reclaimed and marked as free. Also, it makes sure that related data are written sequentially for faster access.

  7. Handling data loss

  8. File-system implements journaling to prevent data loss in case of the unexpected killing of the programs. Journaling enabled the file system to retrieve the data and attempt rewriting.

Security

OS is the guard of the computer, security is one of the prime responsibilities of the OS. Since all software runs on the OS, the security of an application ultimately depends on OS security. Now since all computers are connected to the internet, the security aspect is becoming more important.

When a malicious program or virus takes control of the OS, it can make any changes it wishes, starting from accessing other process memory, stealing other process data, disabling other processes execution, accessing protected part of the disk, using the computer as a bot and many more.

OS implements authentication and authorization protocols to enable secure access of resources.