C++ System Programming Cookbook - Onorato Vaticone - E-Book

C++ System Programming Cookbook E-Book

Onorato Vaticone

0,0
32,36 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

A problem-solution-based guide to help you overcome hurdles effectively while working with kernel APIs, filesystems, networks, threads, and process communications




Key Features



  • Learn to apply the latest C++ features (from C++11, 14, 17, and 20) to facilitate systems programming


  • Create robust and concurrent systems that make the most of the available hardware resources


  • Delve into C++ inbuilt libraries and frameworks to design robust systems as per your business needs



Book Description



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++.




What you will learn



  • Get up to speed with the fundamentals including makefile, man pages, compilation, and linking and debugging


  • Understand how to deal with time interfaces, signals, and CPU scheduling


  • Develop your knowledge of memory management


  • Use processes and threads for advanced synchronizations (mutexes and condition variables)


  • Understand interprocess communications (IPC): pipes, FIFOs, message queues, shared memory, and TCP and UDP


  • Discover how to interact with the console (console I/O)



Who this book is for



This book is for C++ developers who want to gain practical knowledge of systems programming. Though no experience of Linux system programming is assumed, intermediate knowledge of C++ is necessary.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 319

Veröffentlichungsjahr: 2020

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



C++ System Programming Cookbook
Practical recipes for Linux system-level programming using the latest C++ features
Onorato Vaticone
BIRMINGHAM - MUMBAI

C++ System Programming Cookbook

Copyright © 2020 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Richa TripathiAcquisition Editor:Karan GuptaContent Development Editor:Pathikrit RoySenior Editor: Rohit SinghTechnical Editor:Gaurav GalaCopy Editor:Safis EditingProject Coordinator:Francy PuthiryProofreader: Safis EditingIndexer:Rekha NairProduction Designer:Jyoti Chauhan

First published: February 2020

Production reference: 1210220

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-83864-655-4

www.packt.com

Packt.com

Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Fully searchable for easy access to vital information

Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the author

Onorato Vaticone is a software engineer with over 18 years of experience. A C++ expert, he has deep, system-level programming experience. An Agile coach and XP advocate, TDD and Simple Design are his everyday tools. He has worked on real-time systems (defense and energy transmission) with C++. During this time, he learned to write multiplatform code. Early in his career, he realized that a form of agility was needed. He holds an MSc in cloud computing and a BSc in computer engineering and software. He finds learning how things work under the hood to be fascinating!

About the reviewers

Scott Hutchinson leads a team of C++ and F# developers in Oxnard, California. After a few years as a VB/VBA developer, he started developing with .NET Framework immediately after its launch in 2002. Since 2016, he has done most of his development in C++. He is a mentor for the F# track on Exercism, and teaches functional programming in F# to his team at work. His main professional interests are functional programming and machine learning. When he's not learning some new software development skill, he's usually hiking in the mountains of Southern California.

Daniel Durante is an author and technical editor for Packt Publishing, a consultant and strategist for multiple Fortune 100 companies, and has been a full-stack developer since the age of 12. His code exists in infrastructures such as Hubcash, Stripe, and Walmart.

He has worked on text-based browser games that have reached over 1,000,000 active players, created bin-packing software for CNC machines, embedded programming with Cortex-M and PIC circuits, built high-frequency trading applications, and helped contribute to and maintain one of the oldest ORMs of Node.js (SequelizeJS). He has worked on various books such as Rust Standard Library Cookbook, PostgreSQL Developer's Guide, and Rust Programming By Example, among many others.

I would like to thank my parents, my brother, my mentors, and friends who have all put up with my insanity sitting in front of a computer day in and day out. I would not be here today if it wasn't for their patience, guidance, and love.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

C++ System Programming Cookbook

About Packt

Why subscribe?

Contributors

About the author

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Code in Action

Conventions used

Sections

Getting ready

How to do it...

How it works...

There's more...

See also

Get in touch

Reviews

Getting Started with System Programming

Technical requirements

Learning the Linux fundamentals - architecture

How to do it...

How it works...

Learning the Linux fundamentals - shell

How to do it...

How it works...

There's more...

See also

Learning the Linux fundamentals - users

How to do it...

How it works...

There's more...

See also

Using a makefile to compile and link a program

How to do it...

How it works...

There's more...

Using GDB to debug a program

How to do it...

How it works...

There's more...

Learning the Linux fundamentals - processes and threads

How to do it...

How it works...

There's more...

Handling a Linux bash error

How to do it...

How it works...

There's more...

Handling Linux code error

How to do it...

How it works...

There's more...

Revisiting C++

Technical requirements

Understanding C++ primitive types

How to do it...

How it works...

There's more...

See also

Lambda expressions

How to do it...

How it works...

There's more...

See also

Automatic type deduction and decltype

How to do it...

How it works...

There's more...

See also

Learning how atomic works

How to do it...

How it works...

There's more...

See also

Learning how nullptr works

How to do it...

How it works...

There's more...

See also

Smart pointers – unique_ptr and shared_ptr

How to do it...

How it works...

There's more...

See also

Learning how move semantics works

How to do it...

How it works...

There's more...

See also

Understanding concurrency

How to do it...

How it works...

There's more...

See also

Understanding the filesystem

How to do it...

How it works...

There's more...

See also

The C++ Core Guidelines

Getting ready

How to do it...

How it works...

There's more...

See also

Adding GSL in your makefile

Getting ready

How to do it...

How it works...

There's more...

See also

Understanding concepts

How to do it...

How it works...

There's more...

See also

Using span

How to do it...

How it works...

There's more...

See also

Learning how Ranges work

How to do it...

How it works...

There's more...

See also

Learning how modules work

How to do it...

How it works...

There's more...

See also

Dealing with Processes and Threads

Technical requirements

Starting a new process

How to do it...

How it works...

There's more...

See also

Killing a process

Getting ready

How to do it...

How it works...

There's more...

Creating a new thread

How to do it...

How it works...

There's more...

See also

Creating a daemon process

How to do it...

How it works...

There's more...

See also

Deep Dive into Memory Management

Technical requirements

Learning automatic versus dynamic memory

How to do it...

How it works...

There's more...

See also

Learning when to use unique_ptr, and the implications for size

How to do it...

How it works...

There's more...

See also

Learning when to use shared_ptr, and the implications for size

How to do it...

How it works...

There's more...

See also

Allocating aligned memory

How to do it...

How it works...

There's more...

See also

Checking whether the memory allocated is aligned

How to do it...

How it works...

There's more...

See also

Dealing with memory-mapped I/O

How to do it...

How it works...

There's more...

See also

Dealing with allocators hands-on

How to do it...

How it works...

There's more...

See also

Using Mutexes, Semaphores, and Condition Variables

Technical requirements

Using POSIX mutexes

How to do it...

How it works...

There's more...

See also

Using POSIX semaphores

How to do it...

How it works...

There's more...

See also

POSIX semaphores advanced usage

How to do it...

How it works...

There's more...

See also

Synchronization building blocks

How to do it...

How it works...

There's more...

See also

Learning inter-thread communication with simple events

How to do it...

How it works...

There's more...

See also

Learning inter-thread communication with condition variables

How to do it...

How it works...

There's more...

See also

Pipes, First-In First-Out (FIFO), Message Queues, and Shared Memory

Technical requirements

Learning the different types of IPC

How to do it...

How it works...

There's more...

See also

Learning how to use the oldest form of IPC – pipes

How to do it...

How it works...

There's more...

See also

Learning how to use FIFO

How to do it...

How it works...

There's more...

See also

Learning how to use message queues

How to do it...

How it works...

There's more...

See also

Learning how to use shared memory

How to do it...

How it works...

There's more...

See also

Network Programming

Technical requirements

Learning the basics of connection-oriented communication

How to do it...

How it works...

There's more...

See also

Learning the basics of connectionless-oriented communication

How to do it...

How it works...

There's more...

See also

Learning what a communication endpoint is

How to do it...

How it works...

There's more...

See also

Learning to use TCP/IP to communicate with processes on another machine

How to do it...

How it works...

There's more...

See also

Learning to use UDP/IP to communicate with processes on another machine

How to do it...

How it works...

There's more...

See also

Dealing with endianness

How to do it...

How it works...

There's more...

See also

Dealing with Console I/O and Files

Technical requirements

Implementing I/O to and from the console

How to do it...

How it works...

There's more...

See also

Learning how to manipulate I/O strings

How to do it...

How it works...

There's more...

See also

Working with files

How to do it...

How it works...

There's more...

See also

Dealing with Time Interfaces

Technical requirements

Learning about the C++ time interface

How to do it...

How it works...

There's more...

See also

Using the C++20 calendar and time zone

How to do it...

How it works...

There's more...

See also

Learning the Linux timing

How to do it...

How it works...

There's more...

See also

Dealing with time sleep and overruns

How to do it...

How it works...

There's more...

See also

Managing Signals

Technical requirements

Learning all of the signals and their default actions

How to do it...

How it works...

There's more...

See also

Learning how to ignore a signal

How to do it...

How it works...

There's more...

See also

Learning how to trap a signal

How to do it...

How it works...

There's more...

See also

Learning how to send a signal to another process

How to do it...

How it works...

There's more...

See also

Scheduling

Technical requirements

Learning to set and get a scheduler policy

How to do it...

How it works...

There's more...

See also

Learning to get the timeslice value

How to do it...

How it works...

There's more...

See also

Learning how to set a nice value

How to do it...

How it works...

There's more...

See also

Learning how to yield the processor

How to do it...

How it works...

There's more...

See also

Learning about processor affinity

How to do it...

How it works...

There's more...

See also

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

This book aims to provide ready-to-use solutions (to developers) for the essential aspects of system programming, using the latest C++ standards wherever possible. System programming deals with structuring computer programs that closely interact with the operating system and allow computer hardware to interface with the programmer and the user. Due to its efficient features, namely, low-level computation, data abstraction, and object-oriented features, C++ is the preferred language for system programming. You will learn how to create robust and concurrent systems, and you will also understand the inter-process communication mechanism with shared memory and pipe. Moving forward, you will deep dive into the C++ built-in libraries and frameworks in order to design robust systems as per your requirements.

Who this book is for

This book is for C++ developers who want to gain practical knowledge of systems programming. Though no experience of Linux system programming is assumed, intermediate knowledge of C++ is necessary.

What this book covers

Chapter 1, Getting Started with System Programming, introduces you to the fundamentals such as learning about the shell, users and groups, process IDs, and thread IDs to be able to use a Linux system proficiently and so on that you must know for the rest of the book. For example you will learn how Linux is designed, the shell, users and groups, process ID and thread IDs. Furthermore, you will learn how to develop a simple Hello World program, write its makefile, execute it, and debug it. This knowledge, although basic, is fundamental for the more advanced topics that will appear in later chapters.

Chapter 2, Revisiting C++,refreshes your understanding of C++17, which will be used throughout the entire book. It'll show why C++ represents a great opportunity for writing good quality code that is concise and more portable than ever. This chapter contains all the new features introduced by C++11/17/20 that you will find useful in this book.

Chapter 3, Dealing with Processes and Threads,introduces you to processes and threads that are the foundation of any elaboration. A program is rarely ever made of just one process. This chapter reveals the techniques for dealing with threads and processes in C++. The chapter will demonstrate how easy and convenient it is to deal with threads (and tasks) compared to POSIX. Although C++ does not have a formal way of creating a process, there are rare cases in which a thread cannot do thejob.

Chapter 4, Deep Dive into Memory Management, introduces you to memory, which is one of the core concepts of dealing with system development. Allocating, freeing, and learning how memory is managed and what C++ can offer to simplify and manage memory is crucial. Furthermore, this chapter presents recipes on how to check and allocate aligned memory and how to deal with memory-mapped I/O.

Chapter 5, Using Mutexes, Semaphores, and Condition Variables,shows us the POSIX mechanism solutions and the ones offered by C++ to synchronize threads and processes.

Chapter 6, Pipes, First-In First-Out (FIFO), Message Queues, and Shared Memory, focuses on making the processes communicate with each other. There are different solutions available – pipes, FIFO, message queues, and shared memory. For each inter-process communication mechanism, a recipe is provided.

Chapter 7, Network Programming, demonstrates how communication takes place from the connection to the end. Communication between processes on different machines is the foundation of the internet today, and TCP/IP is the standard de facto. Both TCP (short for Transmission Control Protocol) and UDP (short for User Datagram Protocol) will be described in detail, as the first represents connection-oriented and the latter represents connectionless-oriented. This is quite important these days, especially with the video streaming services that are available online.

Chapter 8, Dealing with Console I/O and Files,presents you with useful recipes for dealing with files, I/O to and from the console, and streams of strings.

Chapter 9, Dealing with Time Interfaces,provides you with a deep understanding of how to deal with and measure time with the features that are provided by both C++ and POSIX. The chapter will offer ready-to-use recipes for each method.

Chapter 10, Managing Signals,introduces us to signals that are software interrupts. They provide a way of managing asynchronous events. For example, a user typing the interrupt key from the terminal, or another process sending a signal that must be managed. Every signal has a name starting with SIG (for example, SIGABRT). This chapter will show the reader how to write code to properly manage software interrupts, what the default actions defined by Linux for each signal are, and how to override them.

Chapter 11, Scheduling, shows you how to use POSIX (the C++ standard does not provide this) to set scheduler parameters, the scheduler policy, and the scheduler priorities. System programming is about interacting with the underlying OS as seen so far. The scheduler is one of the main components of every OS and impacts the way processes are allocated on CPUs. There are cases where the developer needs control over this or, at least, tries to influence the scheduler.

To get the most out of this book

Here is a list of requirements for this book:

Intermediate knowledge of C++.

Any additional requirements are mentioned in the

Technical requirements

section of each chapter.

Disclaimer: The C++20 standard has been approved (that is, technically finalized) by WG21 in a meeting in Prague at the end of February. This means that the GCC compiler version that this book uses, 8.3.0, does not include (or has very, very limited support for) the new and cool C++20 features. For this reason, the Docker image does not include the C++20 recipe code. GCC keeps the development of the newest features in branches (you have to use appropriate flags for that, for example,

-std=c++2a

); therefore, you are encouraged to experiment with them by yourself. So, clone and explore the GCC contracts and module branches and have fun.

Some recipes (especially in

Chapter 11

,

Scheduling

) require the Docker image running with admin privileges to execute properly. Depending on your Docker configuration, you may be required to run the Docker with

sudo

. To avoid that you can create a Linux group (for example,

docker

) and add users to it.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

Support

tab.

Click on

Code Downloads

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/C-System-Programming-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781838646554_ColorImages.pdf.

Code in Action

Please visit the following link to check out the CiA videos: http://bit.ly/2uXftdA

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows.

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Getting Started with System Programming

In this chapter, you will be introduced to the foundations on which the entire book is framed. You will learn (or refresh your knowledge of) how Linux is designed, and you will also learn about the shell, users and groups, process IDs, and thread IDs to be able to use a Linux system proficiently and get prepared for the next chapters. Furthermore, you will also learn how to develop a simple hello world program, and find out about its makefile, and also how to execute and debug it. Another important aspect of this chapter is to learn how Linux deals with errors, from both a shell and a source code point of view. This foundational knowledge is important to understand other advanced topics in the following chapters. You can safely skip this and the next chapters if this refresher is not needed.

This chapter will cover the following recipes:

Learning the Linux fundamentals – architecture

Learning the Linux fundamentals – shell

Learning the Linux fundamentals – users

Using a makefile to compile and link a program

Using the

GNU Project Debugger

(

GDB

) to debug a program

Learning the Linux fundamentals – processes and threads

Handling a

Linux bash error

Handling

Linux code error

Technical requirements

In order to let you try the programs immediately, we've set up a Docker imagethat has all the tools and libraries we'll need throughout the book. This is based on Ubuntu 19.04.

In order to set this up, follow these steps:

Download and install the Docker Engine from

www.docker.com

.

Pull the image from Docker Hub:

docker pull kasperondocker/system_programming_cookbook:latest

.

The image should now be available. Type in the following command to view the image:

docker images

.

You should have at least this image now:

kasperondocker/system_programming_cookbook

.

Run the Docker image with an interactive shell, with the help of the following command:

docker run -it

--cap-add sys_ptrace

kasperondocker/system_programming_cookbook:latest

/bin/bash

.

The shell on the running container is now available. Run

root@39a5a8934370/# cd /BOOK/

to get all the programs developed, by chapters.

The--cap-add sys_ptraceargumentis needed to allow GDB in the Docker container to set breakpoints, which, by default, Docker does not allow.

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 Hurdwas 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 freedoms: https://isocpp.org/std/the-standard):

The freedom to run the program as you wish, for any purpose (Freedom

0

).

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.

The freedom to redistribute copies so that you can help others (Freedom

2

).

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:

Open a Terminal on the Docker image.

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 VirtualFilesystem(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.

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:

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

.

Closing a shell: T

o close Terminal, just type

exit

and press

Enter

.

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*"

The

grep

command prints the lines by matching a pattern:

$ grep "text" filename

grep also supports recursive search:

$ grep "text" -R /usr/share

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

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.

This is the command to remove files:

$ rm filename

This is the command to remove directories:

$ rm -r directoryName

This is the command to clone a file:

$ cp file1 file2

This is the command to clone a folder:

$ cp -r folder1 folder2

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:

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.

The second command

searches from the

/usr/local

folder any file or folder that starts with

python

.

The

find

command offers huge flexibility and a wide variety of options. For more information,

refer to

man page

through the

man find

command

.

The

grep

command searches and prints any line that contains the word

text

in the

filename

file.

The

grep

recursive search command

searches and prints any line that contains the word

text

in any file recursively from the

/usr/share

folder.

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 o

f 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:

The first command removes the file named

filename

.

The second command removes

directoryName

and its content, recursively.

The third command creates

file2

, which is an exact copy of

file1

.

The fourth command

creates

folder2

as a clone of