To start our Linux Programming journey, we need to understand what Linux is and how it is organized. Let’s investigate it with me in this post.
1. What is Linux Operating System?
First, Operating system is the software that controls the hardware resource of the computer and provides an environment for program running above.
Linux Operating System is a Unix-like OS, which consist of kernel as well as system utilities, applications shells, libraries of common functions … to make a computer useful and gives the computer its personality.
- Application: programs designed for end user as set-top box channel viewer, game, entertainment on car …
- Libraries: special functions, that are used to implement the functionality of the operating system
- Shell: interface between the user and the kernel. It gathers input from you and executes programs based on that input.
- System call: a method for application to interact with the kernel
- Kernel: the core of operating system, which is loaded first and responsible for all the major activities of the LINUX operating system such as memory, process and task management.
The Linux OS is frequently packaged as a Linux distribution for both desktop and server use. Some popular Linux OS distributions are Debian, Ubuntu, Fedora, Red Hat.
2. Linux OS components running location
We can separate Linux OS Memory to User Space and Kernel Space
- User Space: the memory area where all applications and tools are executed.
- Kernel Space: reserved for running the kernel, kernel extensions and most device drivers.
I will show you more detail about Linux Memory System in other post.
3. What are Linux kernel roles?
Although the distinction between the different kernel tasks isn’t always clearly marked, the kernels role can be split into following parts:
- Process management: the Kernel is in charge of creating and destroying process and handling their connection to the outside world.
- Memory management: the computer’s memory is a major resource, and the policy used to deal with it is a critical one for system performance.
- Filesystems: everything in Linux can be treated as file. The kernel builds a structured filesystem on top of unstructured hardware, and the resulting file abstraction is heavily used throughout the whole system.
- Device control: almost system operation maps to physical devices. Linux OS use device driver to perform device control operations. We will go deep into device driver in next step.
- Networking: Networking must be managed by the operating system, because most network operations are not specific to a process: incoming packets are asynchronous events.
4. What is device driver roles?
As a programmer, you are able to make your own choices about your driver, and choose an acceptable trade off between the programming time required and the flexibility of the result.
We can say driver is flexible because it provides mechanism, which means what the hardware capabilities are provided. Then the user, who don’t need to know about hardware, can provide the policy on application. The same policy can be applied in different hardware environment by using mechanism provided from kernel then the software package is much easier to develop and to adapt to particular needs.
I assume that we are working with a USB WIFI and we have to write a driver to make it works with our system. Then the work can be split into 2 part:
- Device Driver: provide all capabilities of USB WIFI mechanism
- USB info
- Scan network:
- By SSID
- By MAC Address
- Connect network:
- By SSID
- By MAC Address
- Hot plugged
- Application: provide policy to work with this driver. This is an example scenario:
- Get USB Vendor ID
- Scan network by SSID
- Connect network by SSID
As you can see, ther USB WIFI device drivers should:
- Support hardware mechanism as much as possible, because they don’t know what user need.
- Support different branches of hardware, as you can see we have a lot of USB WIFI types, and the driver should work with almost of them.
Embedded device type number is very huge and increasing, then as a Linux kernel programmer, you should know how to implement Linux device drivers to adapt customer requirement when your project is changed or scaled up.
5. Device driver classes
Now focus on device driver, we have 3 classes based on device types:
- Character devices: can be accessed as a stream of bytes (like a file). Such a driver usually implements at least open, close, read and write system calls. The difference between a char device and a regular file:
- Regular file: can always move back and forth data
- Char devices: are just data channels, you can only access sequentially
- Block devices: devices that can host a filesystem. It can only handle I/O operations that transfer one or more whole blocks, which are usually 512 bytes.
- Network interfaces: A network interface is in charge of sending and receiving data packets, driven by the network subsystem of the kernel. Communication between the kernel and a network device driver is completely different from that used with char and block drivers. Instead of read and write, the kernel calls functions related to packet transmission.
6. What should we do next?
I hope that you can understand the overview of some important parts in Linux kernel. In this Linux programming series, I will show you the theory and the implementation about Linux kernel roles and these fundamental parts.
Hope to see you soon.
I referred a list of book for this post:
- Linux Device Drive 3rd Edition – Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman
- Linux Kernel Development 3rd Edition – Robert Love
- Professional Linux Kernel Architecture – Wolfgang Mauerer
These are very amazing books about Linux kernel to help us go deep into Linux Programming worlds.