Skip to main content

Operating System: Introduction

What is an Operating System?

An operating system is a layer of system software between applications and hardware that abstracts (i.e. simplify what hardware actually looks like) and arbitrates (ie. manage, overseas and control the hardware use).

Functions of Operating System

  • Hides hardware complexity: OS hides hardware complexity for applications as well as for application developers. Application developers, for example, don't need to worry about disk sectors or block when saving a file to the disk. In fact mechanism for reading/writing a file on a hard drive is different from reading/writing the file on a USB drive or SSD which is all handled by OS. Similarly, OS abstracts the network resource and provide a higher level abstraction called Socket and provide some services to send and receive packets from that socket.
  • Resource Management: OS also manages the resources used by the application. It decides how many and which one of the above resources will be used by the application, allocates memory to them, schedules these task to the CPU that can execute them, controls access to the applications to various devices in the system, etc. Overall OS is responsible for all types of resource management and allocation to these applications.
  • Provides isolations and protection: When multiple apps are co-running on the same hardware, the OS must ensure that each of them must make adequate progress and they don't hurt each other. For example, OS allocates different part of the physical memory to different applications and makes sure that they don't access each other's memory.
Some examples of Operating Systems are Windows, Linux, MacOS, iOS, Android, Symbian, etc.

Elements of OS

  • Abstractions: Abstraction is the simplification of what hardware actually looks like, some OS abstractions are a process, thread, file, socket, memory page, etc. I'll be discussing them in future posts.
  • Mechanism: To operate on the above abstraction the OS must know the mechanism i.e the implementation or the steps involved which determine how to perform some activities. Some common mechanisms are: create, schedule, open, write, allocate, etc.
  • Policy: Policy is the ways to choose which activity to perform, maximum number of sockets that an application can have access to, how much memory to allocate, etc. Some common policies are Least Recently Used (LRU), Earliest deadline first (EDF), etc.

OS Design Principles

  • Separation of Mechanism and Policy: Mechanisms determine how to do something; policies determine what will be done. For example, the timer construct (see Section 1.5.2) is a mechanism for ensuring CPU protection, but deciding how long the timer is to be set for a particular user is a policy decision.
  • Optimize for the common case: Where will the OS be used? What will the user want to execute on that machine? What are the workload requirements?

Kernel and OS

The OS is the package that communicates directly with hardware and our application, whereas kernel is the lowest level of the OS and is the core of the OS with complete control over everything in the system. it is one of the first programs loaded on start-up (after the bootloader). It handles the rest of start-up as well as input/output requests from software, translating them into data-processing instructions for the CPU.

User/Kernel Protection boundary

User Mode: In User mode, the executing code has no ability to directly access hardware or reference memory. Code running in user mode have to request services from kernel via System Calls to access hardware or memory. Most of our application executes in this mode.

Kernel Mode: In kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system.

To achieve its goal of controlling and managing hardware resources on behalf of applications, the operating system must have special privileges to have direct access to the hardware. OS must operate in kernel mode. Whereas all the applications operate in user mode. If an application wants OS to perform a certain task or service like allocating memory, accessing some files from the disk, access to a network resource, etc it has to explicitly invoke System Calls, which is a mode of communication between applications and OS. OS also support signal which is a mechanism for the OS to pass notifications into the application.

A bit, called the mode bit, is added to the hardware of the computer to indicate the current mode: kernel(0) or user(1). With the mode bit, we are able to distinguish between a task that is executed on behalf of the operating system and one that is executed on behalf of the user.

System Calls: A System call is a programmatic way in which a computer program requests a service from the kernel of the OS. This may include hardware related services like accessing files from hard disk, creation and execution of new processes, etc.

System calls flow chart



System call flow chart
System calls flow chart

Initially, (1) the application process executes in User Mode, while executing (2) if an application needs to access some file or needs some input from the user or maybe the application wants to make a network call, our application will make a system call, the control will be passed to kernel and the mode bit will be set to 0. The OS will perform whatever operation was specified in the system call after that OS changes the mode bit back to 1 and returns the result to process. The system call involves the change in execution context to kernel mode and then back to user mode, therefore the system call is not a cheap operation.
Before making a system call the application process must:

  • Write Argument: In order to make a system call, some data (data bundles to send, file address to read/write, etc) needs to be passed to the kernel which is passed as a system call arguments.
  • Save relevant data to a well-defined location: Well-defined location is necessary so that kernel can determine which arguments, how many arguments it should retrieve and their location in memory.
  • Make System call with specific system call number, which is necessary to access arguments and other relevant data. 
The arguments can be passed directly or they can be passed indirectly by specifying their address.

User/Kernel transition

Applications may need access to certain types of hardware or need to request some changes in the allocation of hardware resources, etc which can only be done by Operating System.
Hardware provides support for user/kernel transition, for example, the hardware will cause a trap if an application from unprivileged mode try to perform some instruction or some memory access or which it doesn't have permission, for example, application process cannot change content of some registers and give itself more CPU or allocate more memory. After the trap, the control is passed to the operating system and mode bit is set to 0, at this point OS checks what causes the tarp and can choose whether to grant or deny that specific request that causes the trap.
Performing all these tasks require a number of instructions which makes a system call an expensive operation.
Accessing memory is about 100 times slower than accessing the cache. Accessing cache is close to cycles of the processor which makes it very fast. The application performance is very dependant on the cache, so when control is passed to the OS, it brings its own content to the cache memory in order to perform system call, replacing application's content. So after the execution of system call, the application will no longer be able to access its data in the cache and it will have to go the memory to access its data which makes user/kernel transition even more expensive.

Basic Operating System services

The operating system provides a number of services, some are directly linked to the underlying hardware like scheduling or memory management while others are linked with higher-level abstractions like file system.

Some example services provided by the operating system for the convenience of the user are:


  • Program Execution: The operating system provides an environment where the user can conveniently run programs. Running a program involves the allocating and deallocating memory, CPU scheduling, etc, the user does not have to worry about these things. They are taken care of by the operating systems.
  • I/O Operations: One of the important jobs of an Operating System is to manage various I/O devices including mouse, keyboards, touchpad, disk drives, display adapters, USB devices, Bit-mapped screen, LED, Analog-to-digital converter, On/off switch, network connections, audio I/O, printers etc. The operating system hides the user the details of underlying hardware for efficiently and protected use of I/O operations.
  • File System: The user process may need to read/write file from the disk. The operating system provides this service. The user does not have to worry about secondary storage management. The user gives a command for reading or writing to a file and sees his/her task accomplished.
  • Communications: There are instances where processes need to communicate with each other to exchange information. It may be between processes running on the same computer or running on the different computers. By providing this service the operating system relieves the user of the worry of passing messages between processes.
  • Error Detection: An error is one part of the system may cause malfunctioning of the complete system. To avoid such a situation the operating system constantly monitors the system for detecting the errors. This relieves the user of the worry of errors propagating to various part of the system and causing malfunctioning. This service cannot be allowed to be handled by user programs because it involves monitoring and in cases altering an area of memory or deallocation of memory for a faulty process. Or maybe relinquishing the CPU of a process that goes into an infinite loop. These tasks are too critical to be handed over to the user programs.  A user program if given these privileges can interfere with the correct (normal) operation of the operating systems.
  • Protection: Considering a computer system having multiple users and concurrent execution of multiple processes, the various processes must be protected from each other's activities. Protection refers to a mechanism or a way to control the access of programs, processes, or users to the resources defined by a computer system.


OS makes all these services available via system calls, following are some popular System calls for windows and Linux.
List of System calls
Windows and Linux System Calls

Different types of operating system architecture

  • Monolithic Architecture: Monolithic OS is one of the oldest OS architecture, in this OS every possible service an application can require or any type of hardware will demand is already a part of the operating system. All the basic services of OS like process management, file management, memory management, exception handling, process communication etc. are all present inside the kernel only. It may also include several file system one for sequential workloads and other for random I/O. This makes OS very large.
    • Advantages: Everything is included in the operating system, i.e. all the abstractions, services, etc and everything is packaged at the same time because of that there are some possibilities of some inlining and compile-time optimization
    • Disadvantages: This types of OS is very large so there are too many states, too much code which is hard to maintain, debug, upgrade and require more memory making less memory available for applications.
  • Modular Architecture: It most commonly used approach today. It has a number of basic services and APIs already part of it but also everything (file system, scheduler, etc) can be customized as a module. The operating system specifies certain interfaces which the module must implement to be apart of the operating system so that depending on our need we can customize various services by installing modules dynamically.
    • Advantages: It's easier to maintain and upgrade, it has a smaller code base and it's less resource intensive which means it can leave more resources and memory for the application to use which can lead to better performance.
    • Disadvantages: Because of its modularity and customizability this can reduce some opportunities for optimizations, which can have some effects on performance although not very significant. Maintainance can be tough as the modules may come from different code bases and may contain some bugs.
  • Microkernel: Microkernel only includes most primitive services at the operating system level. The microkernel support some basic low-level address space management, thread management, and inter-process communication (IPC). Everything else like file system, device drivers, etc will run outside of the operating system at the user level. For this reason, microkernel requires lots of interprocess interactions, so microkernel itself support inter-process communication (IPC) as one of its core abstractions.
    • Advantages: As it doesn't include many abstractions it's size is very small, it can not only lead to lower overheads and better performance but it may be very easy to test and verify the code.
    • Disadvantages: As it has very frequent interactions which require lots of System calls which require a lot of protection mode changes i.e. the execution context changes many times from user mode to kernel mode which is an expensive operation. Software complexity is also very high as it's very hard to find common components of software.

Summary

In this post, we learned about different elements of the operating system i.e abstractions, mechanisms,  and policies. we discussed different services provided by the operating system, we also learned about communications between applications and OS via System Calls, and then very briefly we learned about different types of OS architecture which are Monolithic, Modular and Microkernel.

Next: Process and Process Management

Comments

Popular posts from this blog

Operating System: Process and Process Management

Process In simple words, a process is an instance of an executing application. An application is a file containing a list of instructions stored in the disk (often called an executable file), in flash memory, maybe in the cloud but it's not executing, it's a static entity. When an application is launched it is loaded into the memory and it becomes a process, so it is an active entity with a program counter specifying the next instruction to execute and a set of associated resources. If the same program is launched more than once than multiple processes will be created executing the same program but will be having a very different state. A process encapsulates all the data for running application, this includes the text , the code of the program, a data section, which contains global variables and data which are available when the process is first initialized. As text and the data are available when the process is first initialized they are called static states and a

Convolution Neural Network (CNN): Introduction

Convolution Neural Network:  When it comes to Machine Learning, Artificial Neural Networks perform really well. Artificial Neural Networks are used in various classification task like images, audios, words, etc. Different types of Neural Networks are used for different purposes, for example for predicting the sequence of words we use Recurrent Neural Networks, more precisely a LSTM, similarly for image classification we use Convolution Neural Network. In this blog, we are going to build basic building block for CNN. Before diving into the Convolution Neural Network, let us first revisit some concepts of Basic Neural Network. In a regular Neural Network there are three types of layers: Input Layers:  It’s the layer in which we give input to our model. The number of neurons in this layer is equal to total number of features in our dataset (number of pixels incase of an image). Hidden Layer:  The input from Input layer is then fed into the hidden layer. There can be many hidden lay

Operating System: Threads and Concurrency

Threads Definition: A Thread also called lightweight process, is basic CPU utilization; it compromises a thread ID, a program counter, a register set, and a stack. A thread is an entity within a process that can be scheduled for execution. If we want a process to be able to execute on multiple CPUs at a time, to take advantage of the multi-core systems, the process must have several execution-context called threads. A thread is an active entity which executes a part of a process. Multiple threads execute simultaneously with each other which results in the execution of a single whole process. As several threads of a process execute at the same time they are required to maintain coordination with each other. Coordination between the threads is required in order to share system resources like I/O devices, memory, and of course CPU. The threads of a process are the part of the same virtual address space (i.e. they share all virtual to physical mapping), they share all the data, cod