VibeKoding / Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat / Operating Systems Principles: Processes, Memory, and File SystemsOperating Systems Principles: Processes, Memory, and File Systems
VK

Operating Systems Principles: Processes, Memory, and File SystemsOperating Systems Principles: Processes, Memory, and File Systems

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: Operating Systems Principles: Processes, Memory, and File Systems.Ensiklopedia VibeKoding: Operating Systems Principles: Processes, Memory, and File Systems.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

With a perfect CPU and unlimited memory, is a computer ready to use? In the previous chapter, we witnessed how transistors combine to form a powerful CPU. But even with the most top-tier hardware, if you let it work directly, even displaying a single letter on screen would require writing hundreds of lines of obscure machine instructions. Not only is this cumbersome, it's also extremely dangerous โ€” one small mistake and your code could overwrite someone else's data. To solve these nightmares, the Operating System (OS) was born. It is the most magnificent layer of "software" standing between you and the cold hardware. In this chapter, we'll set aside obscure code and use accessible analogies to see how this "super butler" tames chaotic hardware into submission.With a perfect CPU and unlimited memory, is a computer ready to use? In the previous chapter, we witnessed how transistors combine to form a powerful CPU. But even with the most top-tier hardware, if you let it work directly, even displaying a single letter on screen would require writing hundreds of lines of obscure machine instructions. Not only is this cumbersome, it's also extremely dangerous โ€” one small mistake and your code could overwrite someone else's data. To solve these nightmares, the Operating System (OS) was born. It is the most magnificent layer of "software" standing between you and the cold hardware. In this chapter, we'll set aside obscure code and use accessible analogies to see how this "super butler" tames chaotic hardware into submission.

What will you learn from this article?What will you learn from this article?

After completing this chapter, you will gain:After completing this chapter, you will gain:

ChapterContentCore Concepts
Chapter 1Process ManagementCPU time-division multiplexing, round-robin scheduling
Chapter 2Memory ManagementVirtual memory, paging mechanism
Chapter 3File SystemsFile organization, directory structure

------

0. Big Picture: The Role and Position of Operating Systems0. Big Picture: The Role and Position of Operating Systems

Imagine you open a "computing factory" (your computer) with immense potential. The factory has one all-powerful, tireless top worker (CPU), a vast warehouse (memory), and countless shipping containers (hard drives).Imagine you open a "computing factory" (your computer) with immense potential. The factory has one all-powerful, tireless top worker (CPU), a vast warehouse (memory), and countless shipping containers (hard drives).

If you don't hire a factory manager (operating system):If you don't hire a factory manager (operating system):

  1. CPU Monopoly Crisis: The CPU can only do one thing at a time. If someone is using it to play music, anyone else wanting to browse the web? Sorry โ€” everyone must wait until the music player voluntarily yields the CPU.CPU Monopoly Crisis: The CPU can only do one thing at a time. If someone is using it to play music, anyone else wanting to browse the web? Sorry โ€” everyone must wait until the music player voluntarily yields the CPU.
  2. Memory Stampede Accidents: WeChat and a game are both using the warehouse (memory). Without a security guard designating areas, the game might accidentally place its equipment data in WeChat's box, causing WeChat to crash instantly.Memory Stampede Accidents: WeChat and a game are both using the warehouse (memory). Without a security guard designating areas, the game might accidentally place its equipment data in WeChat's box, causing WeChat to crash instantly.
  3. Hard Drive Maze: The hard drive hardware is just giant discs etched with 0s and 1s. To find yesterday's photo, you'd have to precisely remember it's stored at "platter 1, track 56, sector 8." Nobody can memorize such anti-human coordinates.Hard Drive Maze: The hard drive hardware is just giant discs etched with 0s and 1s. To find yesterday's photo, you'd have to precisely remember it's stored at "platter 1, track 56, sector 8." Nobody can memorize such anti-human coordinates.
  4. To solve these three nightmares, the operating system deploys its three magic weapons: process management, memory management, and file systems.To solve these three nightmares, the operating system deploys its three magic weapons: process management, memory management, and file systems.

    ------

    1. Process Management: CPU Time-Division Multiplexing1. Process Management: CPU Time-Division Multiplexing

    When you use your computer, you often have WeChat running, music playing, and typing all at the same time. But if your computer actually has only one CPU core, how does it do all three things simultaneously?When you use your computer, you often have WeChat running, music playing, and typing all at the same time. But if your computer actually has only one CPU core, how does it do all three things simultaneously?

    The answer: It doesn't do them simultaneously. Instead, the operating system performs frantic "time management."The answer: It doesn't do them simultaneously. Instead, the operating system performs frantic "time management."

    1.1 Process Overview1.1 Process Overview

    Every running program is called a process. You can think of it as a "project team" with its own code (task list), its own memory data (project budget), queuing up for CPU attention.Every running program is called a process. You can think of it as a "project team" with its own code (task list), its own memory data (project budget), queuing up for CPU attention.

    1.2 Round-Robin Scheduling1.2 Round-Robin Scheduling

    To prevent any rogue software from monopolizing the CPU, the operating system slices CPU time into extremely small segments (about 10 milliseconds) and distributes them to each process in turn. Because the switching is so fast, it feels like "simultaneous execution."To prevent any rogue software from monopolizing the CPU, the operating system slices CPU time into extremely small segments (about 10 milliseconds) and distributes them to each process in turn. Because the switching is so fast, it feels like "simultaneous execution."

    ------

    2. Memory Management: Virtual Address Space2. Memory Management: Virtual Address Space

    Having solved the problem of taking turns using the CPU, the next issue is memory space. Without management, if all software writes data directly to physical RAM, mutual overwriting stampedes are inevitable.Having solved the problem of taking turns using the CPU, the next issue is memory space. Without management, if all software writes data directly to physical RAM, mutual overwriting stampedes are inevitable.

    2.1 Virtual Memory2.1 Virtual Memory

    The operating system tells each process a big lie: "Hey, you have exclusive use of all available memory on this entire computer. Use it freely!"The operating system tells each process a big lie: "Hey, you have exclusive use of all available memory on this entire computer. Use it freely!"

    From the process's perspective, its memory space is always contiguous and clean. It peacefully writes data into it.From the process's perspective, its memory space is always contiguous and clean. It peacefully writes data into it.

    2.2 Page Table Mapping2.2 Page Table Mapping

    In reality? The operating system secretly stuffs data into various fragmented gaps in real physical memory. This has two absolutely genius benefits:In reality? The operating system secretly stuffs data into various fragmented gaps in real physical memory. This has two absolutely genius benefits:

    1. Absolute security: WeChat can only ever see its own space and can't tamper with other programs' dataAbsolute security: WeChat can only ever see its own space and can't tamper with other programs' data
    2. Fragment utilization: No matter how fragmented physical memory gets, the virtual space mapped to each process remains orderlyFragment utilization: No matter how fragmented physical memory gets, the virtual space mapped to each process remains orderly
    3. ------

      3. File Systems: Organizing Persistent Storage3. File Systems: Organizing Persistent Storage

      If you buy a brand-new hard drive, it's essentially a barren expanse of storage units. If you want to save a photo, the hard drive will only ask: "Tell me which byte you want to store it at?"If you buy a brand-new hard drive, it's essentially a barren expanse of storage units. If you want to save a photo, the hard drive will only ask: "Tell me which byte you want to store it at?"

      3.1 File System Functions3.1 File System Functions

      1. Partition the hard drive: Cut the hard drive into countless fixed-size blocks (typically 4KB)Partition the hard drive: Cut the hard drive into countless fixed-size blocks (typically 4KB)
      2. Maintain a ledger: Record which blocks are full and which are emptyMaintain a ledger: Record which blocks are full and which are empty
      3. Translate paths: Convert D drive/Photos/Pet.jpg into "blocks 3, 7, and 11"Translate paths: Convert D drive/Photos/Pet.jpg into "blocks 3, 7, and 11"
      4. This is why renaming a file is instant (only the name in the ledger changes), while copying a file takes a long time (actual hard drive data blocks must be read and written).This is why renaming a file is instant (only the name in the ledger changes), while copying a file takes a long time (actual hard drive data blocks must be read and written).

        ------

        4. Three-Way Coordination: The Complete Process of Launching a Program4. Three-Way Coordination: The Complete Process of Launching a Program

        We've now learned about the operating system's three major modules separately. Let's see how they work together when you double-click to open a program:We've now learned about the operating system's three major modules separately. Let's see how they work together when you double-click to open a program:

        Whether you click a desktop icon or execute print("Hello World") in code, it all depends on this complex behind-the-scenes machinery. The reason we can surf the digital world so effortlessly is entirely because the underlying operating system bears the burden for us.Whether you click a desktop icon or execute print("Hello World") in code, it all depends on this complex behind-the-scenes machinery. The reason we can surf the digital world so effortlessly is entirely because the underlying operating system bears the burden for us.

        ------

        Further ReadingFurther Reading

        If you find the operating system's various "management tricks and deception tactics" fascinating, you can explore these advanced topics:If you find the operating system's various "management tricks and deception tactics" fascinating, you can explore these advanced topics:

        • Processes and Threads: If a process is a project team, then "threads" are the employees doing the work within itProcesses and Threads: If a process is a project team, then "threads" are the employees doing the work within it
        • Concurrency and Locks: When two processes compete for the same resource simultaneously, how to prevent deadlocksConcurrency and Locks: When two processes compete for the same resource simultaneously, how to prevent deadlocks
        • System Calls: The "service windows" the operating system provides to upper-layer applicationsSystem Calls: The "service windows" the operating system provides to upper-layer applications