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 - shell

A shell is a command interpreter that receives commands in an input, redirects them to GNU/Linux, and returns back the output. It is the most common interface between a user and GNU/Linux. There are different shell programs available. The most used ones are Bash shell (part of the GNU Project), tcsh shell, ksh shell, and zsh shell (this is basically an extended Bash shell).

Why would you need a shell? A user needs a shell if they need to interact with the operating system through the command line. In this recipe, we'll show some of the most common shell commands. Quite often, the terms shell and Terminal are used interchangeably, even though, strictly speaking, they are not exactly the same thing.

How to do it...

In this section, we will learn the basic commands to run on the shell—for example, to find a file, grep a text into a file, copy, and delete:

  1. Opening a shell: Depending on the GNU/Linux distribution, opening a new shell command has different shortcuts. On Ubuntu, press Ctrl + Alt + T, or press Alt + F2, then type gnome-terminal.
  2. Closing a shell: To close Terminal, just type exit and press Enter.
  3. The find command: This is used to search files in a directory hierarchy. In its simplest form, it appears like this: 
find . -name file

It supports wildcards, too:

$ find /usr/local "python*"
  1. The grep command prints the lines by matching a pattern: 
 $ grep "text" filename

grep also supports recursive search: 

 $ grep "text" -R /usr/share
  1. Pipe commands: Commands running on the shell can be concatenated, to make the output of one command the input for another. The concatenation is done with the | (pipe) operator:
$ ls -l | grep filename
  1. Editing a file: The most two common tools to edit a file on Linux are vi and emacs (if you're not interested in editing the file, cat filename will print the file to the standard output). While the first is inherited by the Unix operating system, the latter is part of the GNU Project. This book will extensively use vi:
 $ vi filename

Next, we will look at shell commands related to file manipulation.

  1. This is the command to remove files:
$ rm filename
  1. This is the command to remove directories:
$ rm -r directoryName
  1. This is the command to clone a file:
$ cp file1 file2
  1. This is the command to clone a folder:
$ cp -r folder1 folder2  
  1. This is the command to clone a folder using a relative and absolute path:
$ cp -r /usr/local/folder1 relative/folder2

The next section will describe these commands.

How it works...

Let's have a look at the commands discussed in the How to do it... section, in detail:

  1. The first command searches (.) from the current folder and can contain absolute paths (for example, /usr/local) or relative paths (for example, tmp/binaries). For example, here, -name is the file to search.
  2. The second command searches from the /usr/local folder any file or folder that starts with pythonThe find command offers huge flexibility and a wide variety of options. For more information, refer to man page through the man find command.
  3. The grep command searches and prints any line that contains the word text in the filename file.
  4. The grep recursive search command searches and prints any line that contains the word text in any file recursively from the /usr/share folder. 
  5. Pipe command (|): The output of the first command is shown in the following screenshot. A list of all the files and directories is passed as input to the second command (grep), which will be used to grep the filename:

Now, let's look at the commands that perform actions such as editing a file, and adding/removing files and directories.

Editing a file:

  • The vi command will open the filename in edit mode, assuming the current user has writing permissions on it (we will discuss permissions in more detail later).
    The following is a short summary of the most used commands in vi:
    • Shift + : (that is, the Shift key + colon) to switch in edit mode.
    • Shift + :i to insert.
    • Shift + :a to append.
    • Shift + :q! to quit the current session without saving.
    • Shift + :wq to save and quit the current session.
    • Shift + :set nu to show the line numbers on the file.
    • Shift + :23 (Enter) goes at line 23.
    • Press the (Esc) key to switch to command mode.
    • . to repeat the last command.
    • cw to change the word, or do this by pointing the cursor at the beginning of the word.
    • dd to remove the current line.
    • yy to copy the current line. If a number N is selected before the yy command, the N line will be copied.
    • p to paste the copied line with the yy command.
    • u to undo.

Adding and removing files and directories:

  1. The first command removes the file named filename
  2. The second command removes directoryName and its content, recursively.
  3. The third command creates file2, which is an exact copy of file1
  4. The fourth command creates folder2 as a clone of folder1

There is a common pattern in the execution of the commands shown in this recipe. They are listed as follows:

  1. The user types a command and hits Enter.
  2. The command is interpreted by Linux.
  3. Linux interacts with its different parts (memory management, networking, filesystem, and more) to execute the command. This happens in kernel space.
  4. The results are returned to the user.

There's more...

This recipe showed some of the most recurrent commands. Mastering all the options, even just for the most common shell commands, is tricky, and that is why man pages were created. They contain a solid and clear reference for the Linux user.

See also

Chapter 8Dealing with Console I/O and Files, will go deeper into console I/O and file management.