A story about porting the code of "Try and understand how Linux works" to Rust

"Try and understand how Linux works"

In Amazon, it is named "[Try and understand] How Linux works ~ Basic knowledge of OS and hardware learned through experiments and illustrations". .. It is often called a sat book because of the handle name of author Takeuchi-san.

The C code distributed by Mr. Takeuchi is here, and the code I rewrote in Rust is [here](https: //) You can find it at github.com/gyu-don/linux-in-practice).

Below is the poem.

Impressions of the book

I was interested in Linux and memory to some extent, so there wasn't much new content. However, I feel very good to read it. As it is labeled "Try and Understand," you can actually try what is written to deepen your understanding. I've heard it somehow, but I'm really glad that I was able to actually try something I hadn't actually tried.

Why ported to Rust

  1. My theory is that it is better to write by yourself because it is okay to copy sutras rather than just running the program. However, it's boring if it's just a sutra copy, so I moved it to Rust.
  2. I wanted to experience "Rust is suitable for system programming, but how is it actually? Is it easier to do than C?"

Was it easy to do?

pros: --There is no doubt (at least for me) that Rust is easier to write than C for anything other than system call calls. ――The crate called nix is a very nice wrapper, and you can handle it with Rust's Result type without saying" See if the return value is -1, read errno ... "(nix document) --You could do something like make a mess of struct and immplate Drop so you don't forget to close the file (although this is difficult in C but can be done in C ++).

cons: ――I decided to do my best to write the parts that didn't have a rapper or didn't feel very good. --C NUL-terminated strings are hard to make without using the heap (CString :: new (...) looks good if heap is fine) --The nix ʻioctlmacro is awkward. I didn't know how to handle theBLKSSZGET required in the sat book with the ʻioctl macro, so I used the libc's ʻioctl. --It was a hassle twice because I often wanted to refer to both the Rust docs such as nix and libcand the C docs. ――The handling of types is strict, and the cast was troublesome because they didn't give special attention tovoid *`.

How about after all?

I wanted to end it with Rust hurray, but after all, as long as the interface was originally based on C, it wouldn't be convenient if I didn't rely on the wrapper, and I felt that the wrapper wasn't perfect. Still, I think it's really convenient to be able to handle errors with Result, and even if you don't like the wrapper, you can make it yourself without much hassle, and it doesn't seem to be bad.

Recommended Posts

A story about porting the code of "Try and understand how Linux works" to Rust
The story of porting code from C to Go and getting hooked (and to the language spec)
A story about how to deal with the CORS problem
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
How to make VS Code aware of the venv environment and its benefits
How to count the number of elements in Django and output to a template
How to access the contents of a Linux disk on a Mac (but read-only)
[CleanArchitecture with Python] Apply CleanArchitecture step by step to a simple API and try to understand "what kind of change is strong" in the code base.
A memo to visually understand the axis of pandas.Panel
A story about changing the master name of BlueZ
[Linux] I learned LPIC lv1 in 10 days and tried to understand the mechanism of Linux.
The story of Airflow's webserver and DAG, which takes a long time to load
The story of Linux that I want to teach myself half a year ago
Now in Singapore The story of creating a LineBot and wanting to do a memorable job
[Ubuntu] How to delete the entire contents of a directory
A rough summary of the differences between Windows and Linux
How to find the scaling factor of a biorthogonal wavelet
After all, the story of returning from Linux to Windows
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
Commands and files to check the version of CentOS Linux
How to insert a specific process at the start and end of spider with scrapy
How to connect the contents of a list into a string
A story about predicting prefectures from the names of cities, wards, towns and villages with Jubatus
How to display the CPU usage, pod name, and IP address of a pod created with Kubernetes
Learn the flow of Bayesian estimation and how to use Pystan through a simple regression model
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
How to display a specified column of files in Linux (awk)
Overview of how to create a server socket and how to establish a client socket
How to determine the existence of a selenium element in Python
How to implement Java code in the background of RedHat (LinuxONE)
[EC2] How to install chrome and the contents of each command
[Python] How to get the first and last days of the month
How to check the memory size of a dictionary in Python
[Linux] How to disable the automatic update of the /etc/resolv.conf file (AmazonLinux2)
How to find the memory address of a Pandas dataframe value
Try to write a program that abuses the program and sends 100 emails
How to use machine learning for work? 01_ Understand the purpose of machine learning
I summarized how to change the boot parameters of GRUB and GRUB2
Try to implement and understand the segment tree step by step (python)
[python] A note that started to understand the behavior of matplotlib.pyplot
The story of writing a program
A story about what to do when a bad interpreter: Not such file or directory appears in Anaconda3 and how to investigate the cause.
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
[AtCoder for beginners] A story about the amount of calculation that you want to know very roughly
How to copy and paste the contents of a sheet in Google Spreadsheet in JSON format (using Google Colab)
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
A story about trying to run JavaScripthon on Windows and giving up.
[Python] How to specify the window display position and size of matplotlib
How to divide and process a data frame using the groupby function
Think about how to write a filter with the Shotgun API-Contact Versions
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Linux] Command to get a list of commands executed in the past
[NNabla] How to add a quantization layer to the middle layer of a trained model
How to put a line number at the beginning of a CSV file
A story about trying to connect to MySQL using Heroku and giving up
Try to write python code to generate go code --Try porting JSON-to-Go and so on
The story of making a sound camera with Touch Designer and ReSpeaker
A note about the functions of the Linux standard library that handles time
Verification of how to periodically execute a script on a Linux server on Windows
Python code to determine the monthly signal of a relative strength investment
How to play a video while watching the number of frames (Mac)