mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23:30 +00:00
Fixing markdown to convert to pdf
This commit is contained in:
parent
81498906fe
commit
5e9d9147d1
4 changed files with 53 additions and 59 deletions
|
|
@ -15,7 +15,7 @@ estimatedReadTime: 30
|
||||||
|
|
||||||
*"What begins as curiosity becomes skill, and what becomes skill empowers the next generation."* - Edson Brandi
|
*"What begins as curiosity becomes skill, and what becomes skill empowers the next generation."* - Edson Brandi
|
||||||
|
|
||||||
# Chapter 1: Introduction - From Curiosity to Contribution
|
# Introduction - From Curiosity to Contribution
|
||||||
|
|
||||||
## My Journey: From Curiosity to Career
|
## My Journey: From Curiosity to Career
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ translator: "TBD"
|
||||||
estimatedReadTime: 60
|
estimatedReadTime: 60
|
||||||
---
|
---
|
||||||
|
|
||||||
# Chapter 2: Setting Up Your Lab
|
# Setting Up Your Lab
|
||||||
|
|
||||||
Before we can begin writing code or exploring FreeBSD's internals, we need a place where it's safe to experiment, make mistakes, and learn. That place is your **lab environment**. In this chapter, we'll create the foundation you'll use throughout the rest of the book: a FreeBSD system configured for driver development.
|
Before we can begin writing code or exploring FreeBSD's internals, we need a place where it's safe to experiment, make mistakes, and learn. That place is your **lab environment**. In this chapter, we'll create the foundation you'll use throughout the rest of the book: a FreeBSD system configured for driver development.
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ Above all, remember: mistakes here are not failures, they are part of the proces
|
||||||
|
|
||||||
**Estimated time to complete this chapter:** 1-2 hours, depending on your choice of virtual machine or bare-metal installation, and whether you already have experience installing operating systems.
|
**Estimated time to complete this chapter:** 1-2 hours, depending on your choice of virtual machine or bare-metal installation, and whether you already have experience installing operating systems.
|
||||||
|
|
||||||
## 2.1 Why a Lab Environment Matters
|
## Why a Lab Environment Matters
|
||||||
|
|
||||||
Before we start typing commands and writing our first pieces of code, we need to pause for a moment and think about *where* we are going to do all this work. Kernel programming and device driver development are not like writing a simple script or a web page. When you experiment with the kernel, you are experimenting with the **heart of the operating system**. A small mistake in your code can cause your machine to freeze, reboot unexpectedly, or even corrupt data if you're not careful.
|
Before we start typing commands and writing our first pieces of code, we need to pause for a moment and think about *where* we are going to do all this work. Kernel programming and device driver development are not like writing a simple script or a web page. When you experiment with the kernel, you are experimenting with the **heart of the operating system**. A small mistake in your code can cause your machine to freeze, reboot unexpectedly, or even corrupt data if you're not careful.
|
||||||
|
|
||||||
|
|
@ -93,7 +93,7 @@ We've introduced the idea of your **lab environment** and why it's so important
|
||||||
|
|
||||||
In the next section, we'll dive deeper into the **pros and cons of virtual machines versus bare metal setups**. By the end of that section, you'll know exactly which setup makes sense for you to begin your FreeBSD journey.
|
In the next section, we'll dive deeper into the **pros and cons of virtual machines versus bare metal setups**. By the end of that section, you'll know exactly which setup makes sense for you to begin your FreeBSD journey.
|
||||||
|
|
||||||
## 2.2 Choosing Your Setup: Virtual Machine or Bare Metal
|
## Choosing Your Setup: Virtual Machine or Bare Metal
|
||||||
|
|
||||||
Now that you understand why a dedicated lab environment is important, the next question is: **where should you build it?** FreeBSD can run in two main ways for your experiments:
|
Now that you understand why a dedicated lab environment is important, the next question is: **where should you build it?** FreeBSD can run in two main ways for your experiments:
|
||||||
|
|
||||||
|
|
@ -178,7 +178,7 @@ We've compared virtual machines and bare metal setups and seen the strengths and
|
||||||
|
|
||||||
In the next section, we'll roll up our sleeves and perform the actual **FreeBSD 14.3 installation**, first in a VM, and then we'll cover the key points for bare-metal installs. This is where your lab truly begins to take shape.
|
In the next section, we'll roll up our sleeves and perform the actual **FreeBSD 14.3 installation**, first in a VM, and then we'll cover the key points for bare-metal installs. This is where your lab truly begins to take shape.
|
||||||
|
|
||||||
## 2.3 Installing FreeBSD (VM and Bare Metal)
|
## Installing FreeBSD (VM and Bare Metal)
|
||||||
|
|
||||||
At this point, you've chosen whether to set up FreeBSD in a **virtual machine** or on **bare metal**. Now it's time to actually install the operating system that will serve as the foundation for all our experiments. We'll focus on **FreeBSD 14.3**, the latest stable release at the time of writing, so that everything you do matches the examples in this book.
|
At this point, you've chosen whether to set up FreeBSD in a **virtual machine** or on **bare metal**. Now it's time to actually install the operating system that will serve as the foundation for all our experiments. We'll focus on **FreeBSD 14.3**, the latest stable release at the time of writing, so that everything you do matches the examples in this book.
|
||||||
|
|
||||||
|
|
@ -442,7 +442,7 @@ You've just completed one of the most important milestones: installing FreeBSD 1
|
||||||
|
|
||||||
In the next section, we'll walk through the **initial configuration** you should do right after installation: setting up networking, enabling essential services, and preparing the system for development work.
|
In the next section, we'll walk through the **initial configuration** you should do right after installation: setting up networking, enabling essential services, and preparing the system for development work.
|
||||||
|
|
||||||
## 2.4 First Boot and Initial Configuration
|
## First Boot and Initial Configuration
|
||||||
|
|
||||||
When your FreeBSD system finishes its first reboot after installation, you're greeted with something that feels very different from Windows or macOS. There's no flashy desktop, no icons, no "getting started" wizard. Instead, you're dropped at a **login prompt**.
|
When your FreeBSD system finishes its first reboot after installation, you're greeted with something that feels very different from Windows or macOS. There's no flashy desktop, no icons, no "getting started" wizard. Instead, you're dropped at a **login prompt**.
|
||||||
|
|
||||||
|
|
@ -675,7 +675,7 @@ Your FreeBSD system is no longer a bare skeleton, it now has a hostname, working
|
||||||
|
|
||||||
In the next section, we'll install the **development tools** needed for driver programming, including compilers, debuggers, editors, and the FreeBSD source tree itself. This is where your lab transforms from a blank canvas into a real development workstation.
|
In the next section, we'll install the **development tools** needed for driver programming, including compilers, debuggers, editors, and the FreeBSD source tree itself. This is where your lab transforms from a blank canvas into a real development workstation.
|
||||||
|
|
||||||
## 2.5 Preparing the System for Development
|
## Preparing the System for Development
|
||||||
|
|
||||||
Now that your FreeBSD lab is installed, updated, and lightly hardened, it's time to turn it into a proper **driver development environment**. Think of this step as stocking your workshop with the right tools; compiler, debugger, version control, and the FreeBSD source tree. Without these, you won't be able to build or test the code we'll write in later chapters.
|
Now that your FreeBSD lab is installed, updated, and lightly hardened, it's time to turn it into a proper **driver development environment**. Think of this step as stocking your workshop with the right tools; compiler, debugger, version control, and the FreeBSD source tree. Without these, you won't be able to build or test the code we'll write in later chapters.
|
||||||
|
|
||||||
|
|
@ -801,7 +801,7 @@ Verify with:
|
||||||
|
|
||||||
You should see directories like `dev`, `kern`, `net`, and `vm`. These are where the FreeBSD kernel lives.
|
You should see directories like `dev`, `kern`, `net`, and `vm`. These are where the FreeBSD kernel lives.
|
||||||
|
|
||||||
#### **Warning: Match your headers to your running kernel.**
|
#### Warning: Match your headers to your running kernel.
|
||||||
|
|
||||||
FreeBSD is very strict about building loadable kernel modules against the exact header set that matches the kernel you are running. If your kernel was built from 14.3-RELEASE but `/usr/src` points to a different branch or version, you can hit confusing compile or load errors. To avoid trouble in the exercises presented in this book, please make sure you have the **FreeBSD 14.3** source tree installed in `/usr/src` and that it matches your running kernel. A quick check is `freebsd-version -k` which should print `14.3-RELEASE`, and your `/usr/src` should be on the `releng/14.3` branch as instructed above.
|
FreeBSD is very strict about building loadable kernel modules against the exact header set that matches the kernel you are running. If your kernel was built from 14.3-RELEASE but `/usr/src` points to a different branch or version, you can hit confusing compile or load errors. To avoid trouble in the exercises presented in this book, please make sure you have the **FreeBSD 14.3** source tree installed in `/usr/src` and that it matches your running kernel. A quick check is `freebsd-version -k` which should print `14.3-RELEASE`, and your `/usr/src` should be on the `releng/14.3` branch as instructed above.
|
||||||
|
|
||||||
|
|
@ -938,7 +938,7 @@ You've now stocked your FreeBSD workshop with the essential tools: compiler, deb
|
||||||
|
|
||||||
In the next section, we'll look at **using snapshots and backups** so that you can experiment freely without fear of losing your progress. This will give you the confidence to take bigger risks and recover quickly when things break.
|
In the next section, we'll look at **using snapshots and backups** so that you can experiment freely without fear of losing your progress. This will give you the confidence to take bigger risks and recover quickly when things break.
|
||||||
|
|
||||||
## 2.6 Using Snapshots and Backups
|
## Using Snapshots and Backups
|
||||||
|
|
||||||
One of the biggest advantages of setting up a **lab environment** is that you can experiment without fear. When you're writing kernel code, mistakes are inevitable: a wrong pointer, an infinite loop, or a bad unload routine can crash the entire operating system. Instead of worrying, you can treat crashes as part of the learning process, *if* you have a way to recover quickly.
|
One of the biggest advantages of setting up a **lab environment** is that you can experiment without fear. When you're writing kernel code, mistakes are inevitable: a wrong pointer, an infinite loop, or a bad unload routine can crash the entire operating system. Instead of worrying, you can treat crashes as part of the learning process, *if* you have a way to recover quickly.
|
||||||
|
|
||||||
|
|
@ -1065,7 +1065,7 @@ By learning to use **snapshots and backups**, you've added one of the most impor
|
||||||
|
|
||||||
In the next section, we'll set up **version control with Git** so you can track your progress, manage your experiments, and share your drivers.
|
In the next section, we'll set up **version control with Git** so you can track your progress, manage your experiments, and share your drivers.
|
||||||
|
|
||||||
## 2.7 Setting Up Version Control
|
## Setting Up Version Control
|
||||||
|
|
||||||
Up to now, you've prepared your FreeBSD lab, installed the tools, and even built your first kernel module. But imagine this: you make a change to your driver, test it, and suddenly nothing works. You wish you could go back to the last working version. Or maybe you want to keep two different experiments without mixing them up.
|
Up to now, you've prepared your FreeBSD lab, installed the tools, and even built your first kernel module. But imagine this: you make a change to your driver, test it, and suddenly nothing works. You wish you could go back to the last working version. Or maybe you want to keep two different experiments without mixing them up.
|
||||||
|
|
||||||
|
|
@ -1209,7 +1209,7 @@ You've now taken the first steps with Git, one of the most important tools in yo
|
||||||
|
|
||||||
In the next section, we'll discuss **documenting your work** another key habit of professional developers. A well-written README or commit message can be the difference between code you understand a year later and code you have to rewrite from scratch.
|
In the next section, we'll discuss **documenting your work** another key habit of professional developers. A well-written README or commit message can be the difference between code you understand a year later and code you have to rewrite from scratch.
|
||||||
|
|
||||||
## 2.8 Documenting Your Work
|
## Documenting Your Work
|
||||||
|
|
||||||
Software development isn't just about writing code it's also about making sure that *you* (and sometimes others) can understand that code later. When working on FreeBSD drivers, you'll often come back to a project weeks or months later and ask yourself: *"Why did I write this? What was I testing? What did I change?"*
|
Software development isn't just about writing code it's also about making sure that *you* (and sometimes others) can understand that code later. When working on FreeBSD drivers, you'll often come back to a project weeks or months later and ask yourself: *"Why did I write this? What was I testing? What did I change?"*
|
||||||
|
|
||||||
|
|
@ -1323,7 +1323,7 @@ You've now learned how to document your FreeBSD driver experiments so that you n
|
||||||
|
|
||||||
In the next section, we'll wrap up this chapter, reviewing everything you've built: a safe FreeBSD lab with the right tools, backups, version control, and documentation, all ready for the deeper journey into FreeBSD itself in Chapter 3.
|
In the next section, we'll wrap up this chapter, reviewing everything you've built: a safe FreeBSD lab with the right tools, backups, version control, and documentation, all ready for the deeper journey into FreeBSD itself in Chapter 3.
|
||||||
|
|
||||||
### 2.9 Bringing It All Together
|
### Bringing It All Together
|
||||||
|
|
||||||
Congratulations! You've built your FreeBSD lab!
|
Congratulations! You've built your FreeBSD lab!
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ translator: "TBD"
|
||||||
estimatedReadTime: 120
|
estimatedReadTime: 120
|
||||||
---
|
---
|
||||||
|
|
||||||
# Chapter 3: A Gentle Introduction to UNIX
|
# A Gentle Introduction to UNIX
|
||||||
|
|
||||||
Now that your FreeBSD system is installed and running, it's time to get comfortable living inside it. FreeBSD isn't just an operating system, it's part of a long tradition that started with UNIX more than fifty years ago.
|
Now that your FreeBSD system is installed and running, it's time to get comfortable living inside it. FreeBSD isn't just an operating system, it's part of a long tradition that started with UNIX more than fifty years ago.
|
||||||
|
|
||||||
|
|
@ -31,7 +31,7 @@ This chapter is not just something to skim, it's designed to be both a **referen
|
||||||
|
|
||||||
Recommendation: Don't try to do everything in one sitting. Break the chapter into sections, and after each one, run the lab before moving on. Save the challenges for when you feel confident and want to test your mastery.
|
Recommendation: Don't try to do everything in one sitting. Break the chapter into sections, and after each one, run the lab before moving on. Save the challenges for when you feel confident and want to test your mastery.
|
||||||
|
|
||||||
## 3.1 Introduction: Why UNIX Matters
|
## Introduction: Why UNIX Matters
|
||||||
|
|
||||||
Before we start writing device drivers for FreeBSD, we need to pause and talk about the foundation they stand on: **UNIX**.
|
Before we start writing device drivers for FreeBSD, we need to pause and talk about the foundation they stand on: **UNIX**.
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ In this opening section, we looked at why UNIX matters for anyone who wants to w
|
||||||
|
|
||||||
With that motivation in mind, it's time to ask the natural next question: **what exactly is UNIX?** To move forward, we'll take a closer look at its history, its guiding principles, and the key concepts that still shape FreeBSD today.
|
With that motivation in mind, it's time to ask the natural next question: **what exactly is UNIX?** To move forward, we'll take a closer look at its history, its guiding principles, and the key concepts that still shape FreeBSD today.
|
||||||
|
|
||||||
## 3.2 What Is UNIX?
|
## What Is UNIX?
|
||||||
|
|
||||||
Before you can get comfortable using FreeBSD, it helps to understand what UNIX is and why it matters. UNIX isn't just a piece of software, it's a family of operating systems, a set of design choices, and even a philosophy that has shaped computing for more than fifty years. FreeBSD is one of its most important modern descendants, so learning UNIX is like studying the family tree to see where FreeBSD fits.
|
Before you can get comfortable using FreeBSD, it helps to understand what UNIX is and why it matters. UNIX isn't just a piece of software, it's a family of operating systems, a set of design choices, and even a philosophy that has shaped computing for more than fifty years. FreeBSD is one of its most important modern descendants, so learning UNIX is like studying the family tree to see where FreeBSD fits.
|
||||||
|
|
||||||
|
|
@ -221,7 +221,7 @@ In this section, you learned that UNIX is not just an operating system but a fam
|
||||||
|
|
||||||
But knowing what UNIX is only gets us halfway. To really use FreeBSD, you need a way to **interact with it**. That's where the shell comes in, the command interpreter that lets you speak the system's language. In the next section, we'll start using the shell to run commands, explore the filesystem, and get hands-on experience with the tools that every FreeBSD developer depends on daily.
|
But knowing what UNIX is only gets us halfway. To really use FreeBSD, you need a way to **interact with it**. That's where the shell comes in, the command interpreter that lets you speak the system's language. In the next section, we'll start using the shell to run commands, explore the filesystem, and get hands-on experience with the tools that every FreeBSD developer depends on daily.
|
||||||
|
|
||||||
## 3.3 The Shell: Your Window Into FreeBSD
|
## The Shell: Your Window Into FreeBSD
|
||||||
|
|
||||||
Now that you know what UNIX is and why it matters, it's time to start **talking to the system**. The way you do that in FreeBSD (and other UNIX-like systems) is through the **shell**.
|
Now that you know what UNIX is and why it matters, it's time to start **talking to the system**. The way you do that in FreeBSD (and other UNIX-like systems) is through the **shell**.
|
||||||
|
|
||||||
|
|
@ -488,7 +488,7 @@ Once you get comfortable typing commands, you'll quickly discover that the shell
|
||||||
- The more minimal **`/bin/sh`** shell is excellent for scripting and system use, but it does not provide conveniences like tab completion or arrow-key history out of the box.
|
- The more minimal **`/bin/sh`** shell is excellent for scripting and system use, but it does not provide conveniences like tab completion or arrow-key history out of the box.
|
||||||
- So if some shortcuts below don't seem to work, check which shell you are using (`echo $SHELL`).
|
- So if some shortcuts below don't seem to work, check which shell you are using (`echo $SHELL`).
|
||||||
|
|
||||||
#### **Tab completion (tcsh)**
|
#### Tab completion (tcsh)
|
||||||
|
|
||||||
Start typing a command or filename and press `Tab`. The shell will try to complete it for you.
|
Start typing a command or filename and press `Tab`. The shell will try to complete it for you.
|
||||||
|
|
||||||
|
|
@ -505,7 +505,7 @@ Becomes:
|
||||||
If there's more than one match, press `Tab` twice to see a list of possibilities.
|
If there's more than one match, press `Tab` twice to see a list of possibilities.
|
||||||
This feature is not available in `/bin/sh`.
|
This feature is not available in `/bin/sh`.
|
||||||
|
|
||||||
#### **Command history (tcsh)**
|
#### Command history (tcsh)
|
||||||
|
|
||||||
Press the **Up arrow** to recall the last command, and keep pressing it to walk further back in time. Press the **Down arrow** to move forward again.
|
Press the **Up arrow** to recall the last command, and keep pressing it to walk further back in time. Press the **Down arrow** to move forward again.
|
||||||
|
|
||||||
|
|
@ -516,7 +516,7 @@ This feature is not available in `/bin/sh`.
|
||||||
You don't have to retype it, just hit the Up arrow and press Enter.
|
You don't have to retype it, just hit the Up arrow and press Enter.
|
||||||
In `/bin/sh`, you don't get arrow-key navigation (but you can still run commands again with `!!`).
|
In `/bin/sh`, you don't get arrow-key navigation (but you can still run commands again with `!!`).
|
||||||
|
|
||||||
#### **Wildcards (globbing)**
|
#### Wildcards (globbing)
|
||||||
|
|
||||||
Works in *all* shells, including `/bin/sh`.
|
Works in *all* shells, including `/bin/sh`.
|
||||||
|
|
||||||
|
|
@ -532,7 +532,7 @@ Lists all files that start with `host` and end with `.conf`.
|
||||||
|
|
||||||
Matches files like `host1.conf`, `hostA.conf`, but not `hosts.conf`.
|
Matches files like `host1.conf`, `hostA.conf`, but not `hosts.conf`.
|
||||||
|
|
||||||
#### **Editing on the command line (tcsh)**
|
#### Editing on the command line (tcsh)
|
||||||
|
|
||||||
In `tcsh` you can move the cursor left and right with arrow keys, or use shortcuts:
|
In `tcsh` you can move the cursor left and right with arrow keys, or use shortcuts:
|
||||||
|
|
||||||
|
|
@ -612,7 +612,7 @@ The shell is your **gateway into FreeBSD**. Every interaction with the system, w
|
||||||
|
|
||||||
Next, we'll explore **how FreeBSD organizes its filesystem**. Understanding the layout of directories like `/etc`, `/usr`, and `/dev` will give you a mental map of the system, which is especially important when we start dealing with device drivers that live under `/dev`.
|
Next, we'll explore **how FreeBSD organizes its filesystem**. Understanding the layout of directories like `/etc`, `/usr`, and `/dev` will give you a mental map of the system, which is especially important when we start dealing with device drivers that live under `/dev`.
|
||||||
|
|
||||||
## 3.4 The FreeBSD Filesystem Layout
|
## The FreeBSD Filesystem Layout
|
||||||
|
|
||||||
In Windows, you may be used to drives like `C:\` and `D:\`. In UNIX and FreeBSD, there are no drive letters. Instead, everything lives in a **single tree of directories** that starts at the root `/`.
|
In Windows, you may be used to drives like `C:\` and `D:\`. In UNIX and FreeBSD, there are no drive letters. Instead, everything lives in a **single tree of directories** that starts at the root `/`.
|
||||||
|
|
||||||
|
|
@ -776,7 +776,7 @@ In this section, you've learned that FreeBSD uses a **single, hierarchical files
|
||||||
|
|
||||||
But files and directories aren't just about structure; they're also about **who can access them**. UNIX is a multi-user system, and every file has an owner, a group, and permission bits that control what can be done with it. In the next section, we'll explore **users, groups, and permissions**, and you'll learn how FreeBSD keeps the system both secure and flexible.
|
But files and directories aren't just about structure; they're also about **who can access them**. UNIX is a multi-user system, and every file has an owner, a group, and permission bits that control what can be done with it. In the next section, we'll explore **users, groups, and permissions**, and you'll learn how FreeBSD keeps the system both secure and flexible.
|
||||||
|
|
||||||
## 3.5 Users, Groups, and Permissions
|
## Users, Groups, and Permissions
|
||||||
|
|
||||||
One of the biggest differences between UNIX and systems like early Windows is that UNIX was designed from the beginning as a **multi-user operating system**. That means it assumes multiple people (or services) can use the same machine at once, and it enforces rules about who can do what.
|
One of the biggest differences between UNIX and systems like early Windows is that UNIX was designed from the beginning as a **multi-user operating system**. That means it assumes multiple people (or services) can use the same machine at once, and it enforces rules about who can do what.
|
||||||
|
|
||||||
|
|
@ -992,7 +992,7 @@ These rules may seem simple, but they are the backbone of FreeBSD's security mod
|
||||||
|
|
||||||
Next, we'll look at **processes**, the running programs that make the system alive. You'll learn how to see what's running, how to manage processes, and how FreeBSD keeps everything organized behind the scenes.
|
Next, we'll look at **processes**, the running programs that make the system alive. You'll learn how to see what's running, how to manage processes, and how FreeBSD keeps everything organized behind the scenes.
|
||||||
|
|
||||||
## 3.6 Processes and System Monitoring
|
## Processes and System Monitoring
|
||||||
|
|
||||||
So far, you've learned how to navigate the filesystem and manage files. But an operating system isn't just about files on disk; it's about **programs running in memory**. These running programs are called **processes**, and understanding them is essential for both daily use and driver development.
|
So far, you've learned how to navigate the filesystem and manage files. But an operating system isn't just about files on disk; it's about **programs running in memory**. These running programs are called **processes**, and understanding them is essential for both daily use and driver development.
|
||||||
|
|
||||||
|
|
@ -1085,9 +1085,9 @@ The `top` screen shows:
|
||||||
- **Memory and swap usage**.
|
- **Memory and swap usage**.
|
||||||
- **A list of processes**, sorted by CPU usage, so you can see which programs are working hardest.
|
- **A list of processes**, sorted by CPU usage, so you can see which programs are working hardest.
|
||||||
|
|
||||||
##### Example of `top` Output (simplified):
|
**Example of `top` Output (simplified):** .
|
||||||
|
|
||||||
```yaml
|
```text
|
||||||
last pid: 3124; load averages: 0.06, 0.12, 0.14 up 0+20:43:11 11:45:09
|
last pid: 3124; load averages: 0.06, 0.12, 0.14 up 0+20:43:11 11:45:09
|
||||||
17 processes: 1 running, 16 sleeping
|
17 processes: 1 running, 16 sleeping
|
||||||
CPU: 0.0% user, 0.0% nice, 0.0% system, 0.0% interrupt, 100% idle
|
CPU: 0.0% user, 0.0% nice, 0.0% system, 0.0% interrupt, 100% idle
|
||||||
|
|
@ -1306,7 +1306,7 @@ Processes are essential because they make the system alive, and as a driver deve
|
||||||
|
|
||||||
But monitoring processes is only part of the story. To do real work, you'll need more tools than those included in the base system. FreeBSD provides a clean and powerful way to install and manage extra software, from simple utilities like `nano` to large applications like web servers. In the next section, we'll dive into the **FreeBSD package system and the Ports Collection**, so you can extend your system with the software you need.
|
But monitoring processes is only part of the story. To do real work, you'll need more tools than those included in the base system. FreeBSD provides a clean and powerful way to install and manage extra software, from simple utilities like `nano` to large applications like web servers. In the next section, we'll dive into the **FreeBSD package system and the Ports Collection**, so you can extend your system with the software you need.
|
||||||
|
|
||||||
## 3.7 Installing and Managing Software
|
## Installing and Managing Software
|
||||||
|
|
||||||
FreeBSD is designed as a lean and reliable operating system. Out of the box, you get a rock-solid **base system**, the kernel, system libraries, essential tools, and configuration files. Everything beyond that, editors, compilers, servers, monitoring tools, and even desktop environments, is considered **third-party software**, and FreeBSD provides two excellent ways to install it:
|
FreeBSD is designed as a lean and reliable operating system. Out of the box, you get a rock-solid **base system**, the kernel, system libraries, essential tools, and configuration files. Everything beyond that, editors, compilers, servers, monitoring tools, and even desktop environments, is considered **third-party software**, and FreeBSD provides two excellent ways to install it:
|
||||||
|
|
||||||
|
|
@ -1389,8 +1389,6 @@ The Ports Collection lives under `/usr/ports`, but on a fresh FreeBSD system thi
|
||||||
|
|
||||||
If you see categories such as `archivers`, `editors`, `net`, `security`, `sysutils`, and `www`, then Ports is installed. If the directory is missing, you'll need to fetch the Ports tree yourself.
|
If you see categories such as `archivers`, `editors`, `net`, `security`, `sysutils`, and `www`, then Ports is installed. If the directory is missing, you'll need to fetch the Ports tree yourself.
|
||||||
|
|
||||||
------
|
|
||||||
|
|
||||||
#### Installing the Ports Collection with Git
|
#### Installing the Ports Collection with Git
|
||||||
|
|
||||||
The official and recommended way is to use **Git**:
|
The official and recommended way is to use **Git**:
|
||||||
|
|
@ -1587,7 +1585,7 @@ Understanding this ecosystem is key to FreeBSD culture. Many administrators inst
|
||||||
|
|
||||||
But applications are only part of the story. The FreeBSD **base system**, the kernel and core utilities also need regular updates to stay secure and reliable. In the next section, we'll learn how to use `freebsd-update` to keep the operating system itself current, so you always have a solid foundation to build on.
|
But applications are only part of the story. The FreeBSD **base system**, the kernel and core utilities also need regular updates to stay secure and reliable. In the next section, we'll learn how to use `freebsd-update` to keep the operating system itself current, so you always have a solid foundation to build on.
|
||||||
|
|
||||||
## 3.8 Keeping FreeBSD Up to Date
|
## Keeping FreeBSD Up to Date
|
||||||
|
|
||||||
One of the most important habits you can develop as a FreeBSD user is keeping your system up to date. Updates fix security issues, squash bugs, and sometimes add support for new hardware. Unlike the command `pkg update && pkg upgrade`, which update your applications, **`freebsd-update` command is used to update the base operating system itself**, including the kernel and core utilities.
|
One of the most important habits you can develop as a FreeBSD user is keeping your system up to date. Updates fix security issues, squash bugs, and sometimes add support for new hardware. Unlike the command `pkg update && pkg upgrade`, which update your applications, **`freebsd-update` command is used to update the base operating system itself**, including the kernel and core utilities.
|
||||||
|
|
||||||
|
|
@ -1776,7 +1774,7 @@ Later, when we begin working on the kernel and writing drivers, we'll also learn
|
||||||
|
|
||||||
And since checking for updates is something you may want to do regularly, wouldn't it be nice if the system could take care of some of these chores for you automatically? That's exactly what we'll look at next: **scheduling and automation** with tools like `cron`, `at`, and `periodic`.
|
And since checking for updates is something you may want to do regularly, wouldn't it be nice if the system could take care of some of these chores for you automatically? That's exactly what we'll look at next: **scheduling and automation** with tools like `cron`, `at`, and `periodic`.
|
||||||
|
|
||||||
## 3.9 Scheduling and Automation
|
## Scheduling and Automation
|
||||||
|
|
||||||
One of UNIX's greatest strengths is that it was designed to let the computer handle repetitive tasks for you. Instead of waiting until midnight to run a backup or logging in every morning to start a monitoring script, you can tell FreeBSD:
|
One of UNIX's greatest strengths is that it was designed to let the computer handle repetitive tasks for you. Instead of waiting until midnight to run a backup or logging in every morning to start a monitoring script, you can tell FreeBSD:
|
||||||
|
|
||||||
|
|
@ -2148,7 +2146,7 @@ You practiced creating jobs, checked their output, and learned how FreeBSD itsel
|
||||||
|
|
||||||
Automation is powerful, but sometimes you need to go beyond fixed schedules. You might want to chain commands together, use loops, or add logic to decide what happens. That's where **shell scripting** comes in. In the next section, we'll write your first scripts and see how to create custom automation tailored to your needs.
|
Automation is powerful, but sometimes you need to go beyond fixed schedules. You might want to chain commands together, use loops, or add logic to decide what happens. That's where **shell scripting** comes in. In the next section, we'll write your first scripts and see how to create custom automation tailored to your needs.
|
||||||
|
|
||||||
## 3.10 Introduction to Shell Scripting
|
## Introduction to Shell Scripting
|
||||||
|
|
||||||
You have learned to run commands one by one. Shell scripting lets you **save those commands into a reusable program**. On FreeBSD, the native and recommended shell for scripting is **`/bin/sh`**. This shell follows the POSIX standard and is available on every FreeBSD system.
|
You have learned to run commands one by one. Shell scripting lets you **save those commands into a reusable program**. On FreeBSD, the native and recommended shell for scripting is **`/bin/sh`**. This shell follows the POSIX standard and is available on every FreeBSD system.
|
||||||
|
|
||||||
|
|
@ -2345,8 +2343,6 @@ case "$action" in
|
||||||
esac
|
esac
|
||||||
```
|
```
|
||||||
|
|
||||||
------
|
|
||||||
|
|
||||||
### 7) Functions to organize your script
|
### 7) Functions to organize your script
|
||||||
|
|
||||||
Functions keep code readable and reusable.
|
Functions keep code readable and reusable.
|
||||||
|
|
@ -2586,7 +2582,7 @@ But remember: you don't need to memorize every construct or command option. Part
|
||||||
|
|
||||||
That's exactly what we'll cover next. In the following section, you'll learn how to use FreeBSD's built-in documentation, the famous FreeBSD Handbook, and the community around the project. These resources will become your companions as you continue your journey into device driver development.
|
That's exactly what we'll cover next. In the following section, you'll learn how to use FreeBSD's built-in documentation, the famous FreeBSD Handbook, and the community around the project. These resources will become your companions as you continue your journey into device driver development.
|
||||||
|
|
||||||
## 3.11 Seeking Help and Documentation in FreeBSD
|
## Seeking Help and Documentation in FreeBSD
|
||||||
|
|
||||||
No one, not even the most experienced developer, remembers every command, option, or system call. The real strength of a UNIX system like FreeBSD is that it ships with **excellent documentation** and has a supportive community that can help when you get stuck.
|
No one, not even the most experienced developer, remembers every command, option, or system call. The real strength of a UNIX system like FreeBSD is that it ships with **excellent documentation** and has a supportive community that can help when you get stuck.
|
||||||
|
|
||||||
|
|
@ -2674,8 +2670,6 @@ man -k disk | less
|
||||||
|
|
||||||
This will show you tools, drivers, and system calls related to disks.
|
This will show you tools, drivers, and system calls related to disks.
|
||||||
|
|
||||||
------
|
|
||||||
|
|
||||||
### The FreeBSD Handbook
|
### The FreeBSD Handbook
|
||||||
|
|
||||||
The **FreeBSD Handbook** is the official, comprehensive guide to the operating system.
|
The **FreeBSD Handbook** is the official, comprehensive guide to the operating system.
|
||||||
|
|
@ -2802,7 +2796,7 @@ Later, as you write drivers, you'll rely heavily on man pages (especially sectio
|
||||||
|
|
||||||
Next, we will look inside the system to **peek at kernel messages and tunables**. Tools like `dmesg` and `sysctl` let you see what the kernel is doing and will become essential when you start loading and testing your own device drivers.
|
Next, we will look inside the system to **peek at kernel messages and tunables**. Tools like `dmesg` and `sysctl` let you see what the kernel is doing and will become essential when you start loading and testing your own device drivers.
|
||||||
|
|
||||||
## 3.12 Peeking into the Kernel and System State
|
## Peeking into the Kernel and System State
|
||||||
|
|
||||||
At this point, you know how to move around in FreeBSD, manage files, control processes, and even write scripts. That makes you a capable user. But writing drivers means stepping into the **mind of the kernel**. You'll need to see what FreeBSD itself sees:
|
At this point, you know how to move around in FreeBSD, manage files, control processes, and even write scripts. That makes you a capable user. But writing drivers means stepping into the **mind of the kernel**. You'll need to see what FreeBSD itself sees:
|
||||||
|
|
||||||
|
|
@ -3087,7 +3081,7 @@ These aren't just abstract tools; they're exactly how you'll see your **own driv
|
||||||
|
|
||||||
Before we move on to the next chapter and start learning about C programming, let's pause to consolidate everything you've learned in this chapter. In the next section, we'll review the key ideas and give you a set of challenges to practice, exercises that will help lock in these new skills and prepare you for the journey ahead.
|
Before we move on to the next chapter and start learning about C programming, let's pause to consolidate everything you've learned in this chapter. In the next section, we'll review the key ideas and give you a set of challenges to practice, exercises that will help lock in these new skills and prepare you for the journey ahead.
|
||||||
|
|
||||||
## 3.13 Putting It All Together: Your FreeBSD Journey So Far
|
## Putting It All Together: Your FreeBSD Journey So Far
|
||||||
|
|
||||||
Congratulations! You've just taken your **first deep dive into UNIX and FreeBSD**. What started as abstract ideas is now becoming practical skills. You can move around the system, manage files, edit and install software, control processes, automate tasks, and even peek into the kernel's inner workings.
|
Congratulations! You've just taken your **first deep dive into UNIX and FreeBSD**. What started as abstract ideas is now becoming practical skills. You can move around the system, manage files, edit and install software, control processes, automate tasks, and even peek into the kernel's inner workings.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ This chapter is not just a quick read; it is both a **reference** and a **hands-
|
||||||
|
|
||||||
This chapter is deliberately long because **C is the foundation** for everything else you'll do in FreeBSD device driver programming. Treat it as your **toolbox**. Once you master it, the material in later chapters will click into place much more naturally.
|
This chapter is deliberately long because **C is the foundation** for everything else you'll do in FreeBSD device driver programming. Treat it as your **toolbox**. Once you master it, the material in later chapters will click into place much more naturally.
|
||||||
|
|
||||||
## 4.1 Introduction
|
## Introduction
|
||||||
|
|
||||||
Let's start at the beginning: what is C, and why is it important to us?
|
Let's start at the beginning: what is C, and why is it important to us?
|
||||||
|
|
||||||
|
|
@ -85,7 +85,7 @@ Here's a quick preview of what's coming up:
|
||||||
|
|
||||||
Are you ready? Let's jump into the next section and get your environment set up so you can run your first C program on FreeBSD.
|
Are you ready? Let's jump into the next section and get your environment set up so you can run your first C program on FreeBSD.
|
||||||
|
|
||||||
## 4.2 Setting Up Your Environment
|
## Setting Up Your Environment
|
||||||
|
|
||||||
Before we can start writing C code, we need to set up a working development environment. The good news? If you're running FreeBSD, **you already have most of what you need**.
|
Before we can start writing C code, we need to set up a working development environment. The good news? If you're running FreeBSD, **you already have most of what you need**.
|
||||||
|
|
||||||
|
|
@ -254,7 +254,7 @@ Here's what you've accomplished:
|
||||||
|
|
||||||
These tools are all you need to start learning C, and later, to build your own kernel modules and drivers. In the next section, we'll look at what makes up a typical C program and how it's structured.
|
These tools are all you need to start learning C, and later, to build your own kernel modules and drivers. In the next section, we'll look at what makes up a typical C program and how it's structured.
|
||||||
|
|
||||||
## 4.3 Anatomy of a C Program
|
## Anatomy of a C Program
|
||||||
|
|
||||||
Now that you've compiled your first "Hello, World!" program, let's take a closer look at what's actually going on inside that code. In this section, we'll break down the basic structure of a C program and explain what each part does, step by step.
|
Now that you've compiled your first "Hello, World!" program, let's take a closer look at what's actually going on inside that code. In this section, we'll break down the basic structure of a C program and explain what each part does, step by step.
|
||||||
|
|
||||||
|
|
@ -540,7 +540,7 @@ In this section, you've learned:
|
||||||
|
|
||||||
The more C code you read, both your own and from FreeBSD, the more these patterns will become second nature.
|
The more C code you read, both your own and from FreeBSD, the more these patterns will become second nature.
|
||||||
|
|
||||||
## 4.4 Variables and Data Types
|
## Variables and Data Types
|
||||||
|
|
||||||
In any programming language, variables are how you store and manipulate data. In C, variables are a little more "manual" than in higher-level languages, but they give you the control you need to write fast, efficient programs, and that's precisely what operating systems like FreeBSD require.
|
In any programming language, variables are how you store and manipulate data. In C, variables are a little more "manual" than in higher-level languages, but they give you the control you need to write fast, efficient programs, and that's precisely what operating systems like FreeBSD require.
|
||||||
|
|
||||||
|
|
@ -715,7 +715,7 @@ In this section, you've learned:
|
||||||
|
|
||||||
You now have the tools to store and work with data in C, and you've already seen how FreeBSD uses the same concepts in production-quality kernel code.
|
You now have the tools to store and work with data in C, and you've already seen how FreeBSD uses the same concepts in production-quality kernel code.
|
||||||
|
|
||||||
## 4.5 Operators and Expressions
|
## Operators and Expressions
|
||||||
|
|
||||||
So far, we've learned how to declare and initialise variables. Now it's time to make them do something! In this section, we'll dive into operators and expressions, the mechanisms in C that allow you to compute values, compare them, and control program logic.
|
So far, we've learned how to declare and initialise variables. Now it's time to make them do something! In this section, we'll dive into operators and expressions, the mechanisms in C that allow you to compute values, compare them, and control program logic.
|
||||||
|
|
||||||
|
|
@ -935,7 +935,7 @@ In this section, you've learned:
|
||||||
|
|
||||||
This section builds the foundation for conditional execution and looping, which we'll explore next.
|
This section builds the foundation for conditional execution and looping, which we'll explore next.
|
||||||
|
|
||||||
## 4.6 Control Flow
|
## Control Flow
|
||||||
|
|
||||||
So far, we've learned how to declare variables and write expressions. But programs need to do more than compute values; they need to **make decisions** and **repeat actions**. This is where **control flow comes** in.
|
So far, we've learned how to declare variables and write expressions. But programs need to do more than compute values; they need to **make decisions** and **repeat actions**. This is where **control flow comes** in.
|
||||||
|
|
||||||
|
|
@ -1629,7 +1629,7 @@ In this section, you've learned:
|
||||||
|
|
||||||
You now have the tools to control the logic and flow of your programs, which is the core of programming itself.
|
You now have the tools to control the logic and flow of your programs, which is the core of programming itself.
|
||||||
|
|
||||||
## 4.7 Functions
|
## Functions
|
||||||
|
|
||||||
In C, a **function** is like a dedicated workshop in a large factory; it is a self-contained area where a specific task is carried out, start to finish, without disturbing the rest of the production line. When you need that task done, you simply send the work there, and the function delivers the result.
|
In C, a **function** is like a dedicated workshop in a large factory; it is a self-contained area where a specific task is carried out, start to finish, without disturbing the rest of the production line. When you need that task done, you simply send the work there, and the function delivers the result.
|
||||||
|
|
||||||
|
|
@ -3344,7 +3344,7 @@ You now understand **scope**, **storage duration**, and **linkage**, the three p
|
||||||
|
|
||||||
Next, we'll see what happens when you pass those variables into a function. In C, function parameters are copies of the original values, so changes inside the function won't affect the originals unless you pass their addresses. Understanding this behaviour is key to writing driver code that updates state intentionally, avoids subtle bugs, and communicates data effectively between functions.
|
Next, we'll see what happens when you pass those variables into a function. In C, function parameters are copies of the original values, so changes inside the function won't affect the originals unless you pass their addresses. Understanding this behaviour is key to writing driver code that updates state intentionally, avoids subtle bugs, and communicates data effectively between functions.
|
||||||
|
|
||||||
## 4.8 Parameters Are Copies
|
## Parameters Are Copies
|
||||||
|
|
||||||
When you call a function in C, the values you pass to it are **copied** into the function's parameters. The function then works with those copies, not the originals. This is known as **call by value**, and it means that any changes made to the parameter inside the function are lost when the function returns; the caller's variables remain untouched.
|
When you call a function in C, the values you pass to it are **copied** into the function's parameters. The function then works with those copies, not the originals. This is known as **call by value**, and it means that any changes made to the parameter inside the function are lost when the function returns; the caller's variables remain untouched.
|
||||||
|
|
||||||
|
|
@ -3497,7 +3497,7 @@ You've now seen that parameters in C work by **value**: every function receives
|
||||||
|
|
||||||
Next, we'll shift focus from individual variables to collections of data that C programmers (and FreeBSD drivers) use constantly: **arrays and strings**.
|
Next, we'll shift focus from individual variables to collections of data that C programmers (and FreeBSD drivers) use constantly: **arrays and strings**.
|
||||||
|
|
||||||
## 4.9 Arrays and Strings in C
|
## Arrays and Strings in C
|
||||||
|
|
||||||
In the previous section, you learned that function parameters are passed by value. That lesson sets the stage for working with **arrays and strings**, two of the most common structures in C. Arrays give you a way to handle collections of elements in contiguous memory. In contrast, strings are simply arrays of characters with a special terminator.
|
In the previous section, you learned that function parameters are passed by value. That lesson sets the stage for working with **arrays and strings**, two of the most common structures in C. Arrays give you a way to handle collections of elements in contiguous memory. In contrast, strings are simply arrays of characters with a special terminator.
|
||||||
|
|
||||||
|
|
@ -3550,7 +3550,7 @@ name[0] = 'A'; // Now the string reads "Adson"
|
||||||
|
|
||||||
If the terminating `'\0'` is missing, functions that expect a string will continue reading memory until they hit a zero byte somewhere else. This often results in garbage output, memory corruption, or kernel crashes.
|
If the terminating `'\0'` is missing, functions that expect a string will continue reading memory until they hit a zero byte somewhere else. This often results in garbage output, memory corruption, or kernel crashes.
|
||||||
|
|
||||||
### 4.9.3 Common String Functions (`<string.h>`)
|
### Common String Functions (`<string.h>`)
|
||||||
|
|
||||||
The C standard library provides helper functions for strings. Although you cannot use the full standard library within the FreeBSD kernel, many equivalents are available. It is important to know the standard ones first:
|
The C standard library provides helper functions for strings. Although you cannot use the full standard library within the FreeBSD kernel, many equivalents are available. It is important to know the standard ones first:
|
||||||
|
|
||||||
|
|
@ -4027,7 +4027,7 @@ If you feel confident, try these challenges. They are designed to push your skil
|
||||||
|
|
||||||
In the next section, we will connect arrays and strings to the deeper concept of **pointers and memory**. You will learn how arrays decay into pointers, how memory addresses are manipulated, and how the FreeBSD kernel allocates and frees memory safely. This is where you begin to see how data structures and memory management form the backbone of every device driver.
|
In the next section, we will connect arrays and strings to the deeper concept of **pointers and memory**. You will learn how arrays decay into pointers, how memory addresses are manipulated, and how the FreeBSD kernel allocates and frees memory safely. This is where you begin to see how data structures and memory management form the backbone of every device driver.
|
||||||
|
|
||||||
## 4.10 Pointers and Memory
|
## Pointers and Memory
|
||||||
|
|
||||||
Welcome to one of the most mysterious and magical topics in your C journey: **pointers**.
|
Welcome to one of the most mysterious and magical topics in your C journey: **pointers**.
|
||||||
|
|
||||||
|
|
@ -5797,7 +5797,7 @@ By now you can clearly see the difference between an array of pointers and a poi
|
||||||
|
|
||||||
With this foundation in place, we are ready to take the next step: moving from fixed arrays to dynamically allocated memory. In the following section on **Dynamic Memory Allocation**, you will learn how to use functions such as `malloc`, `calloc`, `realloc`, and `free` to create arrays at runtime. We will connect this to pointers by showing how to allocate each element separately, how to request one contiguous block when you need a pointer to an array, and how to clean up properly if something goes wrong. This transition from static to dynamic memory is essential for real systems programming and will prepare you for the way memory is managed inside the FreeBSD kernel.
|
With this foundation in place, we are ready to take the next step: moving from fixed arrays to dynamically allocated memory. In the following section on **Dynamic Memory Allocation**, you will learn how to use functions such as `malloc`, `calloc`, `realloc`, and `free` to create arrays at runtime. We will connect this to pointers by showing how to allocate each element separately, how to request one contiguous block when you need a pointer to an array, and how to clean up properly if something goes wrong. This transition from static to dynamic memory is essential for real systems programming and will prepare you for the way memory is managed inside the FreeBSD kernel.
|
||||||
|
|
||||||
## 4.11 Dynamic Memory Allocation
|
## Dynamic Memory Allocation
|
||||||
|
|
||||||
So far, most of the memory we used in examples was **fixed-size**: arrays with a known length or structures allocated on the stack. But when writing system-level code like FreeBSD device drivers, you often don't know in advance how much memory you'll need. Maybe a device reports the number of buffers only after probing, or the amount of data depends on user input. That's when **dynamic memory allocation** comes into play.
|
So far, most of the memory we used in examples was **fixed-size**: arrays with a known length or structures allocated on the stack. But when writing system-level code like FreeBSD device drivers, you often don't know in advance how much memory you'll need. Maybe a device reports the number of buffers only after probing, or the amount of data depends on user input. That's when **dynamic memory allocation** comes into play.
|
||||||
|
|
||||||
|
|
@ -6239,7 +6239,7 @@ Dynamic allocation gives your driver the flexibility to adapt to hardware and wo
|
||||||
|
|
||||||
In the next section, we will build directly on this foundation by looking at **memory safety in kernel code**. There, you will learn techniques to protect against leaks, overflows, and use-after-free errors, making your driver not only functional but also reliable and secure.
|
In the next section, we will build directly on this foundation by looking at **memory safety in kernel code**. There, you will learn techniques to protect against leaks, overflows, and use-after-free errors, making your driver not only functional but also reliable and secure.
|
||||||
|
|
||||||
## 4.12 Memory Safety in Kernel Code
|
## Memory Safety in Kernel Code
|
||||||
|
|
||||||
When writing kernel code, especially device drivers, we are working in a privileged and unforgiving environment. There is no safety net. In user-space programming, a crash usually terminates only your process. In kernel-space, a single bug can panic or reboot the entire operating system. That is why memory safety is not optional. It is the foundation of stable and secure FreeBSD driver development.
|
When writing kernel code, especially device drivers, we are working in a privileged and unforgiving environment. There is no safety net. In user-space programming, a crash usually terminates only your process. In kernel-space, a single bug can panic or reboot the entire operating system. That is why memory safety is not optional. It is the foundation of stable and secure FreeBSD driver development.
|
||||||
|
|
||||||
|
|
@ -6888,7 +6888,7 @@ By following FreeBSD's allocation patterns, checking results, freeing memory dil
|
||||||
|
|
||||||
In the next section, we'll cover **structures** and **typedefs** in C. Structures allow you to group related data, making your code more organized and expressive. Typedefs will enable you to assign meaningful names to complex types, improving readability. Together, they form the basis of almost every real kernel subsystem and are a natural step after mastering pointers.
|
In the next section, we'll cover **structures** and **typedefs** in C. Structures allow you to group related data, making your code more organized and expressive. Typedefs will enable you to assign meaningful names to complex types, improving readability. Together, they form the basis of almost every real kernel subsystem and are a natural step after mastering pointers.
|
||||||
|
|
||||||
## 4.13 Structures and typedef in C
|
## Structures and typedef in C
|
||||||
|
|
||||||
So far, you have worked with single variables, arrays, and pointers. These are helpful tools, but real programs and especially operating system kernels need a way to organize related pieces of information together. Imagine trying to keep track of a device's name, its state, and its configuration using separate variables scattered everywhere. The result would be messy and hard to maintain.
|
So far, you have worked with single variables, arrays, and pointers. These are helpful tools, but real programs and especially operating system kernels need a way to organize related pieces of information together. Imagine trying to keep track of a device's name, its state, and its configuration using separate variables scattered everywhere. The result would be messy and hard to maintain.
|
||||||
|
|
||||||
|
|
@ -7416,7 +7416,7 @@ Try extending this program:
|
||||||
5. **New field**
|
5. **New field**
|
||||||
Add a boolean field `is_console` and print `[CONSOLE]` next to those devices.
|
Add a boolean field `is_console` and print `[CONSOLE]` next to those devices.
|
||||||
|
|
||||||
### **What did you learn in these labs?**
|
### What did you learn in these labs?
|
||||||
|
|
||||||
The three labs you just completed began with the basics of defining a single struct, progressed to simplifying code with `typedef`, and concluded with managing an array of devices that could easily be part of a real driver. Along the way, you learned how C structures can group related fields into one logical unit, how typedef can make code more readable, and how arrays and pointers extend these ideas to represent and manage multiple devices at once.
|
The three labs you just completed began with the basics of defining a single struct, progressed to simplifying code with `typedef`, and concluded with managing an array of devices that could easily be part of a real driver. Along the way, you learned how C structures can group related fields into one logical unit, how typedef can make code more readable, and how arrays and pointers extend these ideas to represent and manage multiple devices at once.
|
||||||
|
|
||||||
|
|
@ -7555,7 +7555,7 @@ You have now learned how to define and use structures, how to make code cleaner
|
||||||
|
|
||||||
In the next section we will take this one step further by looking at **header files and modular code in C**. This will show you how structs and typedefs are shared across multiple source files, which is precisely how large projects like FreeBSD stay organized and maintainable.
|
In the next section we will take this one step further by looking at **header files and modular code in C**. This will show you how structs and typedefs are shared across multiple source files, which is precisely how large projects like FreeBSD stay organized and maintainable.
|
||||||
|
|
||||||
## 4.14 Header Files and Modular Code
|
## Header Files and Modular Code
|
||||||
|
|
||||||
So far, all of our programs have lived in a **single `.c` file**. That's fine for small examples, but real software quickly outgrows this model. The FreeBSD kernel itself is made up of thousands of small files, each with a clear responsibility. To manage this complexity, C provides a way to build **modular code**: splitting definitions into `.c` files and declarations into `.h` files.
|
So far, all of our programs have lived in a **single `.c` file**. That's fine for small examples, but real software quickly outgrows this model. The FreeBSD kernel itself is made up of thousands of small files, each with a clear responsibility. To manage this complexity, C provides a way to build **modular code**: splitting definitions into `.c` files and declarations into `.h` files.
|
||||||
|
|
||||||
|
|
@ -8263,7 +8263,7 @@ By now, you've:
|
||||||
|
|
||||||
In the next section, we'll see how the compiler and linker combine all these separate `.c` and `.h` files into a single program and how debugging tools help when things go wrong. This is where your modular code truly comes to life.
|
In the next section, we'll see how the compiler and linker combine all these separate `.c` and `.h` files into a single program and how debugging tools help when things go wrong. This is where your modular code truly comes to life.
|
||||||
|
|
||||||
## 4.15 Compiling, Linking, and Debugging C Programs
|
## Compiling, Linking, and Debugging C Programs
|
||||||
|
|
||||||
Up to this point you have written short C programs and even split them into more than one file. Now it is time to understand what really happens between writing your code and running it on FreeBSD. This is the step where your text file becomes a living program. It is also the step where you first learn to read the compiler's messages and start using a debugger when things go wrong. These skills are the difference between "just typing code" and truly programming with confidence.
|
Up to this point you have written short C programs and even split them into more than one file. Now it is time to understand what really happens between writing your code and running it on FreeBSD. This is the step where your text file becomes a living program. It is also the step where you first learn to read the compiler's messages and start using a debugger when things go wrong. These skills are the difference between "just typing code" and truly programming with confidence.
|
||||||
|
|
||||||
|
|
@ -9219,7 +9219,7 @@ In this section, you moved beyond the one-line `cc hello.c` and began thinking l
|
||||||
|
|
||||||
With these foundations in place, you are ready to explore the **C Preprocessor**, the very first stage of the pipeline. This is where `#include`, `#define`, and conditional compilation reshape your source before the compiler ever sees it. Understanding this stage will explain why almost every FreeBSD kernel header begins with a dense block of preprocessor directives, and it will prepare you to read and write them with confidence.
|
With these foundations in place, you are ready to explore the **C Preprocessor**, the very first stage of the pipeline. This is where `#include`, `#define`, and conditional compilation reshape your source before the compiler ever sees it. Understanding this stage will explain why almost every FreeBSD kernel header begins with a dense block of preprocessor directives, and it will prepare you to read and write them with confidence.
|
||||||
|
|
||||||
## 4.16 The C Preprocessor: Directives Before Compilation
|
## The C Preprocessor: Directives Before Compilation
|
||||||
|
|
||||||
Before your C code is compiled, it makes a stop at an earlier stage called **preprocessing**. You can think of this as the **pre-flight checklist for your program**: it prepares the source, pulls in external declarations, expands macros, removes comments, and decides which parts of the code will even reach the compiler. In practice, this means the compiler never works directly on the file you wrote. Instead, it sees a transformed version that has already been “cleaned up” and adapted by the preprocessor.
|
Before your C code is compiled, it makes a stop at an earlier stage called **preprocessing**. You can think of this as the **pre-flight checklist for your program**: it prepares the source, pulls in external declarations, expands macros, removes comments, and decides which parts of the code will even reach the compiler. In practice, this means the compiler never works directly on the file you wrote. Instead, it sees a transformed version that has already been “cleaned up” and adapted by the preprocessor.
|
||||||
|
|
||||||
|
|
@ -9694,7 +9694,7 @@ You have seen how the preprocessor sets the stage before the compiler arrives. I
|
||||||
|
|
||||||
In the next section, **Good Practices for C Programming**, we will shift from mechanisms to habits. You will learn how to choose between macros and `inline`, how to name and document flags, how to structure includes in kernel code, and how to keep your driver readable for future contributors. We will also connect these practices with FreeBSD’s coding style so your code feels at home in the tree.
|
In the next section, **Good Practices for C Programming**, we will shift from mechanisms to habits. You will learn how to choose between macros and `inline`, how to name and document flags, how to structure includes in kernel code, and how to keep your driver readable for future contributors. We will also connect these practices with FreeBSD’s coding style so your code feels at home in the tree.
|
||||||
|
|
||||||
## 4.17 Good Practices for C Programming
|
## Good Practices for C Programming
|
||||||
|
|
||||||
By now you know the basic building blocks of the C language and how they fit together. But writing code that simply compiles and runs is not the same as writing code that others can read, maintain, and trust inside an operating system kernel. In FreeBSD, style and clarity are not cosmetic details; they are part of what keeps the system stable and maintainable over decades.
|
By now you know the basic building blocks of the C language and how they fit together. But writing code that simply compiles and runs is not the same as writing code that others can read, maintain, and trust inside an operating system kernel. In FreeBSD, style and clarity are not cosmetic details; they are part of what keeps the system stable and maintainable over decades.
|
||||||
|
|
||||||
|
|
@ -11367,7 +11367,7 @@ Target function/file: `___________________________________________`
|
||||||
3. Immediately transform one of your own functions using the items in Section 9.
|
3. Immediately transform one of your own functions using the items in Section 9.
|
||||||
4. Keep the before/after diff with this worksheet, it’s proof of learning and a handy reference when you start writing real drivers.
|
4. Keep the before/after diff with this worksheet, it’s proof of learning and a handy reference when you start writing real drivers.
|
||||||
|
|
||||||
## 4.18 Final Practice Labs
|
## Final Practice Labs
|
||||||
|
|
||||||
You have now met the essential C tools you will use over and over again when writing FreeBSD device drivers: operators and expressions, the preprocessor, arrays and strings, function pointers and typedefs, and pointer arithmetic.
|
You have now met the essential C tools you will use over and over again when writing FreeBSD device drivers: operators and expressions, the preprocessor, arrays and strings, function pointers and typedefs, and pointer arithmetic.
|
||||||
|
|
||||||
|
|
@ -11987,7 +11987,7 @@ Each of these small programs mirrors a real kernel pattern. The logging system e
|
||||||
|
|
||||||
By now, you should not only understand these C concepts in the abstract but also have typed them out, compiled them, and watched them work. That muscle memory will be critical when you are deep in kernel space and bugs are subtle.
|
By now, you should not only understand these C concepts in the abstract but also have typed them out, compiled them, and watched them work. That muscle memory will be critical when you are deep in kernel space and bugs are subtle.
|
||||||
|
|
||||||
## 4.19 Final Knowledge Check
|
## Final Knowledge Check
|
||||||
|
|
||||||
You have now walked through a vast landscape: from your very first `printf()` all the way to FreeBSD-flavoured details like error codes, Makefiles, pointers, and kernel coding standards. Before we move on, it is time to pause and assess how much of this knowledge has been absorbed.
|
You have now walked through a vast landscape: from your very first `printf()` all the way to FreeBSD-flavoured details like error codes, Makefiles, pointers, and kernel coding standards. Before we move on, it is time to pause and assess how much of this knowledge has been absorbed.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue