Book Image

C++ System Programming Cookbook

By : Onorato Vaticone
Book Image

C++ System Programming Cookbook

By: Onorato Vaticone

Overview of this book

C++ is the preferred language for system programming due to its efficient low-level computation, data abstraction, and object-oriented features. System programming is about designing and writing computer programs that interact closely with the underlying operating system and allow computer hardware to interface with the programmer and the user. The C++ System Programming Cookbook will serve as a reference for developers who want to have ready-to-use solutions for the essential aspects of system programming using the latest C++ standards wherever possible. This C++ book starts out by giving you an overview of system programming and refreshing your C++ knowledge. Moving ahead, you will learn how to deal with threads and processes, before going on to discover recipes for how to manage memory. The concluding chapters will then help you understand how processes communicate and how to interact with the console (console I/O). Finally, you will learn how to deal with time interfaces, signals, and CPU scheduling. By the end of the book, you will become adept at developing robust systems applications using C++.
Table of Contents (13 chapters)

Learning the Linux fundamentals - architecture

Linux is a clone of the Unix operating system, developed by Linus Torvalds in the early '90s. It is a multiuser, multitasking operating system that runs on a wide variety of platforms. The Linux kernel has a monolithic architecture for performance reasons. This means that it is self-contained in one binary, and all its services run in kernel space. This was one of the most controversial topics at the beginning. Andy Tanenbaum (professor at the Vrije Universiteit, Amsterdam) argued against its monolithic system, saying: This is a giant step back into the 1970s. He also argued against its portability, saying: LINUX is tied fairly closely to the 80 x 86. Not the way to go. In the minix user group, there still is the thread of full chat involving Torvalds, Tanenbaum, and others.

The following diagram shows the main Linux building blocks:

Let's describe the layers we see in the diagram:

  • On the top layer, there are user applications, processes, compilers, and tools. This layer (which runs in a user space) communicates with the Linux kernel (which runs in kernel space) through system calls.
  • System libraries: These are a set of functions through which an application can interact with the kernel. 
  • Kernel: This component contains the core of the Linux system. Among other things, it has the scheduler, networking, memory management, and filesystems.
  • Kernel modules: These contain pieces of kernel code that still run in kernel space but are fully dynamic (in the sense that they can be loaded and unloaded with the running system). They typically contain device drivers, kernel code that is specific to a particular hardware module implementing a protocol, and so on. One huge advantage of the kernel modules is that users can load them without rebuilding the kernel.

GNU is a recursive acronym that stands for GNU is Not Unix. GNU is an operating system that is free software. Note the term operating system here. Indeed, GNU used alone is meant to represent a full set of tools, software, and kernel parts that an operating system needs. The GNU operating system kernel is called the Hurd. As the Hurd was not production-ready, GNU typically uses the Linux kernel, and this combination is called the GNU/Linux operating system.

So, what are the GNU components on a GNU/Linux operating system? Packages* such as the GNU Compiler Collection (GCC), the GNU C library, GDB, the GNU Bash shell, and the GNU Network Object Model Environment (GNOME) desktop environment, to mention just a few. Richard Stallman and the Free Software Foundation (FSF)—of which Stallman is the founder—authored the free software definition to help respect users' freedom. Free software is considered any package that grants users the following four types of freedoms (so-called essential freedomshttps://isocpp.org/std/the-standard):

  1. The freedom to run the program as you wish, for any purpose (Freedom 0).
  2. The freedom to study how the program works and to change it, so it does your computing as you wish (Freedom 1). Access to the source code is a precondition for this.
  3. The freedom to redistribute copies so that you can help others (Freedom 2).
  4. The freedom to distribute copies of your modified versions to others (Freedom 3). By doing this, you can give the whole community a chance to benefit from your changes. Access to the source code is a precondition for this.

The concrete instantiation of these principles is in the GNU/GPL license, which FSF authored. All of the GNU packages are released under the GNU/GPL license.

How to do it...

Linux has a pretty standard folder structure across the distributions, so knowing this would allow you to easily find programs and install them in the correct place. Let's have a look at it as follows:

  1. Open a Terminal on the Docker image.
  2. Type the command ls -l /.

How it works...

The output of the command will contain the following folders:

As you can see this folder structure is pretty organized and consistent across all the distributions. Under the hood, the Linux filesystem is quite modular and flexible. A user application can interact with the GNU C library (which provides interfaces such as open, read, write, and close) or the Linux system call directly. The system call interface, in this case, talks to the Virtual Filesystem (often referred to as the VFS). The VFS is the abstraction on top of the concrete filesystem implementations (for example, ext3, Journaled File System (JFS), and more). This architecture, as we can imagine, gives a high level of flexibility.