」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 作業系統開發(真相)

作業系統開發(真相)

發佈於2024-11-08
瀏覽:706

OS Development (The truth)

Table of Contents

  • Introduction
  • 1. The Bootloader: Kicking Things Off
  • 2. Entering the Kernel: Where the Magic Happens
  • 3. Choosing Your Language
  • 4. Safety: Don’t Crash the Plane
  • 5. Optimizing for Speed
  • 6. Setting Up Basic Drivers
    • 6.1 Video Driver
    • 6.2 Keyboard Driver
    • 6.3 I/O Driver
  • 7. Writing a Shell: The User Interface
  • 8. Building a Custom Filesystem
  • 9. Adding a Mouse Driver: Click and Move
  • 10. Building a Simple GUI
  • 11. Handling Windows and Events
  • 12. Creating a Notepad App: From Click to Type
  • 13. Final Touches: Making it Feel Like an OS

Introduction

Building an operating system from scratch is one of the most challenging and rewarding experiences you can have as a developer. Unlike high-level application development, where a library exists for almost everything, OS development forces you to work close to the metal, touching hardware directly, managing memory manually, and controlling every aspect of how your machine runs.

From my experience, building an OS means getting deep into assembly language, wrestling with the hardware, and struggling through crashes, reboots (ESPECIALLY reboots), and long debugging sessions. If you think debugging a bootloader is tough, try doing it without the luxury of modern tools. OS development makes you question your life choices more times than you can count.

That said, let’s break it all down, from the bootloader to a fully functional desktop environment where you can move a mouse around and open up a text editor to type.


1. The Bootloader: Kicking Things Off

What is a Bootloader?

The bootloader is the first step in any OS development journey. When your computer turns on, the BIOS takes over, checks your hardware, and then loads your bootloader from disk into memory. This little program’s job is to get the CPU ready and load your operating system’s kernel into memory. You have to write the bootloader in assembly because you’re dealing directly with hardware at this stage.

When the bootloader starts, the CPU is in 16-bit real mode, which means it can only address 1MB of memory. The first thing you need to do is load the kernel from disk and move it to memory. After that, the bootloader switches the CPU to 32-bit protected mode, which is where the fun starts. Switching modes requires setting up the Global Descriptor Table (GDT) to manage memory segments and enabling the Protection Enable (PE) bit in the CPU’s control register. If you get this wrong, the system either freezes or crashes into a boot loop, which happened to me more times than I’d like to admit.

Real Mode vs. Protected Mode

In real mode, everything is super limited – 16-bit registers, 1MB memory access, no memory protection. This is why switching to protected mode is so important. Once in protected mode, your CPU has access to 32-bit registers, larger memory addressing, and advanced features like multitasking and paging (virtual memory). The bootloader is all about making this transition happen seamlessly.


2. Entering the Kernel: Where the Magic Happens

Once the CPU switches to protected mode, the bootloader hands control to the kernel. The kernel is the core of the operating system and is responsible for managing everything: hardware, memory, processes, and system resources.

When the kernel starts, it has to set up several critical systems:

  1. Paging: This is a memory management scheme that allows the OS to give each process its own virtual memory space. Without it, all processes would share the same memory, which is a recipe for disaster.
  2. Interrupt Handling: The kernel needs to handle interrupts, which are signals from hardware (like keyboards or disk drives) that something needs immediate attention. To do this, you need to define an Interrupt Descriptor Table (IDT), which maps interrupts to specific handler functions in the kernel.
  3. Task Scheduling: In any OS that runs multiple processes, the kernel needs a way to manage CPU time. A scheduler decides which process gets CPU time and when, making sure the system is responsive and efficient.

Building the kernel is a long and complex task, but it’s also one of the most rewarding. This is where you get to see the inner workings of an operating system and control every little detail of how your machine behaves.


3. Choosing Your Language

When building an OS, you have to choose the right programming language for each task. The bootloader is typically written in assembly, as you need to directly control the hardware. However, once you’re in protected mode and working on the kernel, most developers switch to C because it gives you low-level control without the headache of writing everything in assembly.

Some developers use C for kernel development, as it offers object-oriented features that can make managing complex systems easier. However, C comes with additional overhead, and memory management in C can be trickier in an OS environment. C gives you the raw power and simplicity needed for system programming.


4. Safety: Don’t Crash the Plane

In OS development, safety is critical. Unlike high-level programming, where a crash might just mean an error message or app shutdown, in OS development, a crash usually means a full system reboot. You’re working with memory directly, which means if you mess up memory management, you can corrupt system data, overwrite important structures, or cause a kernel panic.

The kernel needs to implement memory protection to prevent one process from overwriting another’s memory. This is done using paging, which maps each process to its own virtual memory space. If you get this wrong, the entire system becomes unstable, and you’ll be chasing down memory bugs for days. Trust me, I’ve been there.


5. Optimizing for Speed

Speed is a key factor in making your OS feel responsive. A slow kernel means a slow system, so optimizing for performance is crucial. Here are a few key areas where speed matters:

  • Interrupt Handling: Instead of constantly polling for input (which wastes CPU cycles), you should set up hardware interrupts. This way, the CPU only responds when there’s actual input, like a keypress or a network packet arriving.
  • Task Scheduling: A good scheduler will balance CPU time between processes efficiently, making sure no process hogs all the CPU time while others starve. There are many different scheduling algorithms to choose from, like round-robin or priority-based scheduling.
  • Lazy Loading: Don’t load everything into memory at once. Implement demand paging, where only the parts of a program that are actually being used get loaded into memory. This helps conserve memory and speeds up system performance.

6. Setting Up Basic Drivers

Now that you’ve got the kernel running, it’s time to build drivers to interact with hardware. Drivers are the bridge between your OS and the hardware, allowing the OS to communicate with things like the keyboard, display, and disk drives.

6.1 Video Driver

At first, your OS will likely start in text mode, where you’re printing characters directly to video memory (usually at address 0xB8000). This is fine for debugging and basic output, but eventually, you’ll want to move to a graphical user interface (GUI). This requires a video driver that can manage pixel-level control, screen resolution, and color depth.

Setting up a video driver is a big step toward creating a graphical OS, but it’s also one of the more complex tasks because it involves understanding how your display hardware works and managing large amounts of data for each frame.

6.2 Keyboard Driver

The keyboard driver is one of the most important parts of an interactive OS. When you press a key, the keyboard sends a scancode to the CPU. The job of the keyboard driver is to translate that scancode into a character or action that the OS can understand. This involves setting up an interrupt handler for IRQ1, the hardware interrupt that the keyboard generates.

Once you’ve got the keyboard driver working, you can start building more complex user interfaces, taking input from the user, and processing commands.

6.3 I/O Driver

The I/O driver is what lets your OS read and write to disk. This is critical for things like loading programs, saving files, and storing data. At first, you’ll probably interact with the disk using BIOS interrupts, but as your OS matures, you’ll want to move to more

advanced I/O methods that don’t rely on the BIOS, like directly communicating with the disk controller.


7. Writing a Shell: The User Interface

Once you’ve got your basic drivers working, it’s time to build a shell – the command-line interface (CLI) that lets users interact with the OS. The shell is where users can type commands, execute programs, and interact with the filesystem.

Implementing a shell is an exciting step because it’s one of the first places where your OS really starts to feel interactive. You’ll need to handle user input (from the keyboard), process commands, and execute programs. This is also where you start to see the importance of your kernel’s ability to multitask and manage processes efficiently.


8. Building a Custom Filesystem

The filesystem is what allows your OS to store and retrieve data on the disk. While you could use an existing filesystem (like FAT or ext4), building your own custom filesystem gives you more control and can be a fun challenge.

A basic filesystem should:

  • Allocate space on the disk for new files.
  • Keep track of filenames, file sizes, and metadata.
  • Allow reading and writing files efficiently.

As your OS grows, you’ll also need to handle more advanced features like:

  • Directories: Organizing files into a hierarchy.
  • Permissions: Controlling who can read, write, or execute files.
  • Fragmentation: Dealing with files that get split across multiple areas of the disk.

Designing a filesystem is tricky because it involves balancing performance, reliability, and ease of use. A poorly designed filesystem can lead to data corruption, slow performance, or wasted space on the disk.


9. Adding a Mouse Driver: Click and Move

Now that your OS has a CLI and can handle keyboard input, it’s time to add mouse support. The mouse driver is responsible for tracking the movement of the mouse and translating that into on-screen actions like moving a cursor or clicking buttons.

Building a mouse driver involves handling IRQ12, the hardware interrupt generated by the mouse, and processing the movement data. Once you have the mouse driver in place, you can start thinking about building a graphical user interface (GUI).


10. Building a Simple GUI

A graphical user interface (GUI) takes your OS from a command-line interface to something that looks and feels more like a modern desktop environment. At this stage, you’ll want to build windows, buttons, menus, and other interactive elements that the user can click on with the mouse.

Creating a GUI involves managing graphics rendering (drawing windows and icons), handling input events (clicks, keypresses, etc.), and implementing a system to manage multiple windows and applications.

At first, your GUI might be super basic – just a single window that the user can interact with. But as your OS matures, you’ll want to add more advanced features like window resizing, drag-and-drop functionality, and animations.


11. Handling Windows and Events

Once you’ve got the basics of a GUI in place, the next step is to build a system for managing windows and events. This involves handling multiple windows at once, each potentially running a different application, and making sure that each window receives the correct input events (like mouse clicks or keyboard presses).

You’ll also need to implement window z-ordering (which window is on top), minimizing/maximizing, and dragging. This is where things start to feel more like a traditional desktop environment.


12. Creating a Notepad App: From Click to Type

To make your GUI more functional, you’ll want to build basic applications, like a Notepad app. The Notepad app is a simple text editor that allows users to type, edit, and save files. Building an app like this involves:

  • Handling text input from the keyboard.
  • Rendering text to the screen.
  • Allowing basic file operations like open, save, and close.

This is a great exercise in putting everything together: your GUI, your filesystem, and your input handling all come into play here. Once you’ve got a Notepad app working, you’ll have the basics of a fully functioning OS.


13. Final Touches: Making it Feel Like an OS

At this point, your OS is functional, but there are always little details that make it feel more polished. Things like:

  • User accounts and permissions: Allowing multiple users to have their own settings and files.
  • Networking: Adding support for TCP/IP so your OS can connect to the internet.
  • System calls: Creating an interface that applications can use to interact with the kernel.

Every little detail you add brings your OS closer to feeling like a complete system. It’s a long and challenging process, but by the end, you’ll have created something truly unique – an operating system built from scratch.

版本聲明 本文轉載於:https://dev.to/mr-3/os-development-the-truth-1cc2如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 如何使用組在MySQL中旋轉數據?
    如何使用組在MySQL中旋轉數據?
    在關係數據庫中使用mySQL組使用mySQL組進行查詢結果,在關係數據庫中使用MySQL組,轉移數據的數據是指重新排列的行和列的重排以增強數據可視化。在這裡,我們面對一個共同的挑戰:使用組的組將數據從基於行的基於列的轉換為基於列。 Let's consider the following ...
    程式設計 發佈於2025-04-08
  • 為什麼使用Firefox後退按鈕時JavaScript執行停止?
    為什麼使用Firefox後退按鈕時JavaScript執行停止?
    導航歷史記錄問題:JavaScript使用Firefox Back Back 此行為是由瀏覽器緩存JavaScript資源引起的。要解決此問題並確保在後續頁面訪問中執行腳本,Firefox用戶應設置一個空功能。 警報'); }; alert('inline Alert')...
    程式設計 發佈於2025-04-08
  • 如何使用FormData()處理多個文件上傳?
    如何使用FormData()處理多個文件上傳?
    )處理多個文件輸入時,通常需要處理多個文件上傳時,通常是必要的。 The fd.append("fileToUpload[]", files[x]); method can be used for this purpose, allowing you to send multi...
    程式設計 發佈於2025-04-08
  • 在細胞編輯後,如何維護自定義的JTable細胞渲染?
    在細胞編輯後,如何維護自定義的JTable細胞渲染?
    在JTable中維護jtable單元格渲染後,在JTable中,在JTable中實現自定義單元格渲染和編輯功能可以增強用戶體驗。但是,至關重要的是要確保即使在編輯操作後也保留所需的格式。 在設置用於格式化“價格”列的“價格”列,用戶遇到的數字格式丟失的“價格”列的“價格”之後,問題在設置自定義單元...
    程式設計 發佈於2025-04-08
  • 為什麼不使用CSS`content'屬性顯示圖像?
    為什麼不使用CSS`content'屬性顯示圖像?
    在Firefox extemers屬性為某些圖像很大,&& && && &&華倍華倍[華氏華倍華氏度]很少見,卻是某些瀏覽屬性很少,尤其是特定於Firefox的某些瀏覽器未能在使用內容屬性引用時未能顯示圖像的情況。這可以在提供的CSS類中看到:。 googlepic { 內容:url(&...
    程式設計 發佈於2025-04-08
  • 如何干淨地刪除匿名JavaScript事件處理程序?
    如何干淨地刪除匿名JavaScript事件處理程序?
    刪除匿名事件偵聽器將匿名事件偵聽器添加到元素中會提供靈活性和簡單性,但是當要刪除它們時,可以構成挑戰,而無需替換元素本身就可以替換一個問題。 element? element.addeventlistener(event,function(){/在這里工作/},false); 要解決此問題,請考...
    程式設計 發佈於2025-04-08
  • 在Ghost中製作自定義的車把助手!
    在Ghost中製作自定義的車把助手!
    方法1(修改核心代码) 我发现可以使用其他帮助者扩展Ghost的源代码。我通过在Current/Core/core/Frontend/Apps中添加一个新目录来实现这一目标。我使用了一个名为AMP的现有“应用程序”的示例,该示例非常简单,以开始创建主题中可用的新帮手。在这些现有的...
    程式設計 發佈於2025-04-08
  • 如何使用Python理解有效地創建字典?
    如何使用Python理解有效地創建字典?
    在python中,詞典綜合提供了一種生成新詞典的簡潔方法。儘管它們與列表綜合相似,但存在一些顯著差異。 與問題所暗示的不同,您無法為鑰匙創建字典理解。您必須明確指定鍵和值。 For example:d = {n: n**2 for n in range(5)}This creates a dict...
    程式設計 發佈於2025-04-08
  • 為什麼PHP的DateTime :: Modify('+1個月')會產生意外的結果?
    為什麼PHP的DateTime :: Modify('+1個月')會產生意外的結果?
    使用php dateTime修改月份:發現預期的行為在使用PHP的DateTime類時,添加或減去幾個月可能並不總是會產生預期的結果。正如文檔所警告的那樣,“當心”這些操作的“不像看起來那樣直觀。 考慮文檔中給出的示例:這是內部發生的事情: 現在在3月3日添加另一個月,因為2月在2001年只有2...
    程式設計 發佈於2025-04-08
  • 為什麼儘管有效代碼,為什麼在PHP中捕獲輸入?
    為什麼儘管有效代碼,為什麼在PHP中捕獲輸入?
    在php ;?>" method="post">The intention is to capture the input from the text box and display it when the submit button is clicked.但是,輸出...
    程式設計 發佈於2025-04-08
  • 哪種在JavaScript中聲明多個變量的方法更可維護?
    哪種在JavaScript中聲明多個變量的方法更可維護?
    在JavaScript中聲明多個變量:探索兩個方法在JavaScript中,開發人員經常遇到需要聲明多個變量的需要。對此的兩種常見方法是:在單獨的行上聲明每個變量: 當涉及性能時,這兩種方法本質上都是等效的。但是,可維護性可能會有所不同。 第一個方法被認為更易於維護。每個聲明都是其自己的語句,使...
    程式設計 發佈於2025-04-08
  • 如何實時捕獲和流媒體以進行聊天機器人命令執行?
    如何實時捕獲和流媒體以進行聊天機器人命令執行?
    在開發能夠執行命令的chatbots的領域中,實時從命令執行實時捕獲Stdout,一個常見的需求是能夠檢索和顯示標準輸出(stdout)在cath cath cant cant cant cant cant cant cant cant interfaces in Chate cant inter...
    程式設計 發佈於2025-04-08
  • 如何有效地轉換PHP中的時區?
    如何有效地轉換PHP中的時區?
    在PHP 利用dateTime對象和functions DateTime對象及其相應的功能別名為時區轉換提供方便的方法。例如: //定義用戶的時區 date_default_timezone_set('歐洲/倫敦'); //創建DateTime對象 $ dateTime = ne...
    程式設計 發佈於2025-04-08
  • 如何使用替換指令在GO MOD中解析模塊路徑差異?
    如何使用替換指令在GO MOD中解析模塊路徑差異?
    在使用GO MOD時,在GO MOD 中克服模塊路徑差異時,可能會遇到衝突,其中3個Party Package將另一個PAXPANCE帶有導入式套件之間的另一個軟件包,並在導入式套件之間導入另一個軟件包。如迴聲消息所證明的那樣: go.etcd.io/bbolt [&&&&&&&&&&&&&&&&...
    程式設計 發佈於2025-04-08

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3