Build an Operating System from Scratch

Learn via video courses
Topics Covered

Overview

Operating systems provide us with different features that enable us to develop services and are used by billions of people in different streams. An operating system has millions of lines of code. For example, the Windows operating system has 50 million lines of code. Even though there is a large amount of work besides an operating system, we can create an operating system easily with development kits.

Introduction

An operating system has many functions such as management of memory, processor, device, etc... The question of how to make an operating system can be explained by modeling these functions in terms of programming. The windows operating system has all these functions programmed using C, C++, C#, and assembly programming languages. These functions are essential for an OS to function efficiently and the personalization of the OS can be done above these basic features. These personalizations are the key to the creation of your operating system.

What is an Operating System?

  • An operating system is a program that acts as a bridge between the hardware components of the system and the software programs in the computer.
  • There are different types of operating systems for different purposes.
  • Few functions of an operating system include managing memory by allocating memory to other programs and keeping track of the amount of memory allocated, deciding the order of execution of process, managing the input and output streams, managing files, and managing the authorization and authentication of files and other programs in the system.

What are the Parts of an Operating System?

Operating systems are the combination of a kernel and system programs.

The Kernel

The kernel represents the operating system and is the core component of an operating system. It is a group of programs that will be loaded to the memory at first when the system is turned on and used to manage all the other programs in the system.

the kernel

System Programs

System programs are programs that are used to connect the kernel with other parts of the computer. System programs also provide the perfect environment for developing and executing a program that utilizes the internal features of the system.

the role system programs

Step 1: The Concept

The operating system is made of millions of lines of code that can give instructions to multiple microprocessors and microcontrollers on the system. A microprocessor has millions of logical gates and the data sent to a microprocessor is processed according to a specific logic in the processor a microcontroller is an integrated circuit that is embedded in a system and used to perform a specific function. We can use the COSMOS development kit to program on a specific microprocessor in our computer and eventually develop our operating system.

Step 2: Materials and Prerequisite Knowledge

To develop a terminal operating system, a piece of fundamental knowledge is required of the following software and programming languages,

  • Microsoft Visual studio is an integrated development environment in which external libraries and development environments can be configured.
  • COSMOS User Development Kit is an operating system development kit that uses the C# programming language and can be used only in Windows operating systems.
  • A basic understanding of the C# programming language and .NET framework is essential.
  • A virtualization software such as VMware Player or virtual box that helps us to run an operating system on top of our base operating system.

Step 3: Introduction to COSMOS

C# Open Source Managed Operating System (Cosmos) is a development kit for operating systems that use Visual Studio as a development environment. Even though we use C#, any language that is based on .NET can be employed, including VB.NET, Fortran, Delphi Prism, IronPython, F#, and many more.

In Cosmos, the Common Intermediate Language (CIL) is translated into native instructions using an Ahead time(AOT) compiler called IL2CPU. IL2CPU is used by Cosmos to compile user-made programs and associated libraries into bootable native executables. Developers can increase the speed of development and testing operating systems without constantly restarting their computers by using the cosmos virtualization feature. We usually use VMWare Player as a virtualization option as it integrates well with the framework. It is generally used by default.

The common intermediate language includes a set of instructions that are generated from the source code. An AOT compiler compiles the code just before the running of the code.

Step 4: Writing Your First Operating System

Following the following steps may be a solution to the question of how to make an operating system,

  • Open Visual Studio and click on the File on the toolbar and click new project.
  • On the dialog box opened after clicking the new project, click cosmosboot and name the project.
  • A new file named program. cs can be found in solution explorer. Just click and load the new file.
  • Click the start button on the top-left of the visual studio. If you can't find the start button, you can click the start debugging option in the debug menu.
  • A window named cosmos builder will appear, click the build button in that window.
  • We can see a terminal window which is a virtual emulator that compiles and runs the code of your operating system.

Writing Your First Operating System Writing Your First Operating System 2 Writing Your First Operating System 3

Step 5: Personalizing the OS

  • We can make changes to the operating system by making changes in the program. cs file.
  • Find the public static void Init() method in the program. cs file and inside the method, we can find the Console.WriteLine(text) command, and the text will be displayed in the black terminal that was displayed in the previous step.
  • Make changes to the text and you can see it in the terminal.

Code

Output

Step 6: Making a Command Line OS

Now, let's answer the question of how to make an operating system by a command line os with simple features such as giving input to get information about the operating system.

The following code can be added under the Console.WriteLine() line that we have added in the last step,

Code:

Explanation

  • The above code allows the operating system to get input from the user through the terminal.
  • For different inputs, the operating system can respond with different responses using if..else loops.
  • If a command is not found, the operating system returns as Not found using the else loop.
  • The DateTimeImpl class in the cosmos framework can be used to deal with date and time. We can get the current date by using the UtcNow() function in the DateTimeImpl class.
  • The UtcNow() function returns the present date and time System.DateTime object which can be converted into a string by using the ToLongDateString(DateTime) method in the DateTimeImpl class.
  • Thus by giving input as time to the operating system, we can get the present date and time.

Output

Step 7: Adding More Features to the OS

We can add more features to complete the question of how to make an operating system by using the in-built functions of the cosmos framework. Let us create a directory in our operating system and the following code illustrates this,

Code

Explanation

  • The above code illustrates how to create a directory and delete a file in the new operating system.
  • The user can give the command to the operating system as either create directory_name or delete file_name.
  • As the first step, the command will be split using the " "(space) as a delimiter to get the name of the file or directory.
  • The CosmosFileSystem class has a method that can be used to create and handle files or directories.
  • The CreateDirectory() function takes two arguments, the file path and the permission of the file.
  • The DeleteFile() function takes the path of the file to be deleted as an argument and deletes the file.

Output

Step 8: Adding Shutdown and Restart Features

An operating system must be able to turn on and turn off according to the need of the user. Therefore, we must implement turn-on and off features in our operating system. The following code illustrates the adding the shutdown and turnoff features to the operating system,

Code

Explanation

  • The shutdown function is present in the Deboot class. On calling the ShutDown() function, the operating system terminates all its processes and turns off.
  • The reboot function is present in the Deboot class of the cosmos framework. On calling the Reboot() function, the system terminates all its processes and turns off. After a few seconds, the operating system loads again and turns on.

Step 9: Final Touches

The current coding is made in a way that it will only execute once and to prevent that we can put all our code in a while loop and change the if conditions to a switch case.

Code

Explanation:

  • The while(true) will run indefinitely until the system is shut down or restarted.
  • The switch() cases are used to take action based on the command given by the user.
  • If the user issues a wrong command, the default switch case will take effect and print Not found.

Output

Step 10: Running & Testing the OS

We can test the operating system that we have created by using virtualization software such as a virtual box or VMware player. The following steps can be followed to run the created operating system in a virtual machine,

  • Go to the virtualization software and click the New button and create a new virtual machine.
  • Then configure the new virtual machine with a minimal amount of space and threads.
  • You can find the newly created operating system file in the C:\Users\{Your Username}\AppData\Roaming\Cosmos User Kit folder. The file will be an iso file.
  • Select that file and boot it into the virtual machine to experience the created operating system in your system.

testing the operating  system testing the operating  system 2

Conclusion

  • An operating system acts as a bridge between the software and hardware components of the system.
  • The operating system is made of a kernel and system programs. These system programs use system calls to interact with other programs and hardware of the system.
  • The question of how to make an operating system can be answered with the help of the COSMOS user kit and the C# programming language.
  • The COSMOS user kit provides us with an easy interface to create an operating system.
  • The public static void Init() function in the program. cs file can be modified to personalize the operating system.
  • The kit has many in-built functions that can be used to access the information in the operating system.
  • After developing an operating system, the iso file of the operating system is generated and this can be booted with virtualization software to test, thus answering the question of how to make an operating system.