C++ System Programming Cookbook
eBook - ePub

C++ System Programming Cookbook

Practical recipes for Linux system-level programming using the latest C++ features

  1. 292 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

C++ System Programming Cookbook

Practical recipes for Linux system-level programming using the latest C++ features

About this book

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.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access C++ System Programming Cookbook by Onorato Vaticone in PDF and/or ePUB format, as well as other popular books in Computer Science & Operating Systems. We have over one million books available in our catalogue for you to explore.

Information

Revisiting C++

This chapter acts as a refresher on C++ 11-20, which will be used throughout this book. We'll explain why C++ represents a great opportunity that shouldn't be missed when it comes to writing good quality code that's concise and more portable than ever.
This chapter does not contain all the new features introduced by C++ (11 through 20) – just the ones we will be using for the rest of this book. Specifically, you'll get a refresher (if you already know) or learn (if you are new) about the most essential new C++ skills needed to write modern code. You'll work, hands-on, with lambda expressions, atomics, and move semantics, just to mention a few.
This chapter will cover the following recipes:
  • Understanding C++ primitive types
  • Lambda expressions
  • Automatic type deduction and decltype
  • Learning how atomic works
  • Learning how nullptr works
  • Smart pointers unique_ptr and shared_ptr
  • Learning how semantics works
  • Understanding concurrency
  • Understanding the filesystem
  • The C++ Core Guidelines
  • Adding GSL to your makefile
  • Understanding concepts
  • Using span
  • Learning how Ranges work
  • Learning how modules work

Technical requirements

To let you try out the programs in this chapter immediately, we've set up a Docker image that has all the tools and libraries we'll need throughout this book. It's based on Ubuntu 19.04.
In order to set it up, follow these steps:
  1. Download and install the Docker Engine from www.docker.com.
  2. Pull the image from Docker Hub: docker pull kasperondocker/system_programming_cookbook:latest.
  3. The image should now be available. Type in the following command to view the image: docker images.
  4. Now, you should have the following image: kasperondocker/system_programming_cookbook.
  5. 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.
  6. The shell on the running container is now available. Use root@39a5a8934370/# cd /BOOK/ to get all the programs that have been developed for the chapters in this book.
The --cap-add sys_ptrace argument is needed to allow GDB to set breakpoints in the Docker container which, by default, Docker does not allow.
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.

Understanding C++ primitive types

This recipe will show all the primitive data types defined by the C++ standard, as well as their size.

How to do it...

In this section, we'll have a closer look at what primitives the C++ standard defines and what other information is important. We'll also learn that although the standard does not define a size for each, it defines another important parameter:
  1. First, open a new Terminal and type in the following program:
#include <iostream>
#include <limits>

int main ()
{
// integral types section
std::cout << "char " << int(std::numeric_limits<char>::min())
<< "-" << int(std::numeric_limits<char>::max())
<< " size (Byte) =" << sizeof (char) << std::endl;
std::cout << "wchar_t " << std::numeric_limits<wchar_t>::min()
<< "-" << std::numeric_limits<wchar_t>::max()
<< " size (Byte) ="
<< sizeof (wchar_t) << std::endl;
std::cout << "int " << std::numeric_limits<int>::min() << "-"
<< std::numeric_limits<int>::max() << " size
(Byte) ="
<< sizeof (int) << std::endl;
std::cout << "bool " << std::numeric_limits<bool>::min() << "-"
<< std::numeric_limits<bool>::max() << "
size (Byte) ="
<< sizeof (bool) << std::endl;

// floating point types
std::cout << "float " << std::numeric_limits<float>::min() <<
"-"
<< std::numeric_limits<float>::max() << " size
(Byte) ="
<< sizeof (float) << std::endl;
std::cout << "double " << std::numeric_limits<double>::min()
<< "-"
<< std::numeric_limits<double>::max() << " size
(Byte) ="
<< sizeof (double) << std::endl;
return 0;
}
  1. Next, build (compile and link) g++ primitives.cpp.
  2. This will produce an executable fil...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. About Packt
  4. Contributors
  5. Preface
  6. Getting Started with System Programming
  7. Revisiting C++
  8. Dealing with Processes and Threads
  9. Deep Dive into Memory Management
  10. Using Mutexes, Semaphores, and Condition Variables
  11. Pipes, First-In First-Out (FIFO), Message Queues, and Shared Memory
  12. Network Programming
  13. Dealing with Console I/O and Files
  14. Dealing with Time Interfaces
  15. Managing Signals
  16. Scheduling
  17. Other Books You May Enjoy