C++ System Programming Cookbook: Practical recipes for Linux system-level programming using the latest C++ features
()
About this ebook
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.
Related to C++ System Programming Cookbook
Related ebooks
Practical C Programming: Solutions for modern C developers to create efficient and well-structured programs Rating: 0 out of 5 stars0 ratingsCompTIA Linux+ Certification Guide: A comprehensive guide to achieving LX0-103 and LX0-104 certifications with mock exams Rating: 0 out of 5 stars0 ratingsLearn C Programming from Scratch: A step-by-step methodology with problem solving approach (English Edition) Rating: 0 out of 5 stars0 ratingsKali Linux Web Penetration Testing Cookbook Rating: 0 out of 5 stars0 ratingsPython for Offensive PenTest: A practical guide to ethical hacking and penetration testing using Python Rating: 0 out of 5 stars0 ratingsC++ Cookbook: How to write great code with the latest C++ releases (English Edition) Rating: 0 out of 5 stars0 ratingsPractical Design Patterns for Java Developers: Hone your software design skills by implementing popular design patterns in Java Rating: 0 out of 5 stars0 ratingsLinux System Programming Techniques: Become a proficient Linux system programmer using expert recipes and techniques Rating: 0 out of 5 stars0 ratingsC++ Programming Cookbook Rating: 0 out of 5 stars0 ratingsPHP Ajax Cookbook Rating: 2 out of 5 stars2/5Ultimate Pentesting for Web Applications Rating: 0 out of 5 stars0 ratingsC Programming For Beginners: The Complete Step-By-Step Guide To Mastering The C Programming Language Like A Pro Rating: 0 out of 5 stars0 ratingsInternet of Things from Scratch: Build IoT solutions for Industry 4.0 with ESP32, Raspberry Pi, and AWS Rating: 0 out of 5 stars0 ratingsFundamentals of Linux: Explore the essentials of the Linux command line Rating: 0 out of 5 stars0 ratingsLearn Angular: 4 Angular Projects Rating: 0 out of 5 stars0 ratingsLearn SQL Database Programming: Query and manipulate databases from popular relational database servers using SQL Rating: 0 out of 5 stars0 ratingsEmbedded Systems Programming with C++: Real-World Techniques Rating: 0 out of 5 stars0 ratingsBeginning Mobile Application Development in the Cloud Rating: 0 out of 5 stars0 ratingsBuilding Your Own JavaScript Framework: Architect extensible and reusable framework systems Rating: 0 out of 5 stars0 ratingsFull Stack Development with Spring Boot 3 and React: Build modern web applications using the power of Java, React, and TypeScript Rating: 0 out of 5 stars0 ratingsModern C++ Programming Rating: 0 out of 5 stars0 ratingsCSS Mastery: Styling Web Pages Like a Pro Rating: 0 out of 5 stars0 ratingsCybersecurity Blue Team Strategies: Uncover the secrets of blue teams to combat cyber threats in your organization Rating: 0 out of 5 stars0 ratingsMastering Lua Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Problem Solving in C and Python: Programming Exercises and Solutions, Part 1 Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Narrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5
Reviews for C++ System Programming Cookbook
0 ratings0 reviews
Book preview
C++ System Programming Cookbook - Onorato Vaticone
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 Tripathi
Acquisition Editor: Karan Gupta
Content Development Editor: Pathikrit Roy
Senior Editor: Rohit Singh
Technical Editor: Gaurav Gala
Copy Editor: Safis Editing
Project Coordinator: Francy Puthiry
Proofreader: Safis Editing
Indexer: Rekha Nair
Production 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 customercare@packtpub.com 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 the job.
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