Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

C++ System Programming Cookbook: Practical recipes for Linux system-level programming using the latest C++ features
C++ System Programming Cookbook: Practical recipes for Linux system-level programming using the latest C++ features
C++ System Programming Cookbook: Practical recipes for Linux system-level programming using the latest C++ features
Ebook606 pages3 hours

C++ System Programming Cookbook: Practical recipes for Linux system-level programming using the latest C++ features

Rating: 0 out of 5 stars

()

Read preview

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.

LanguageEnglish
Release dateFeb 21, 2020
ISBN9781838648756
C++ System Programming Cookbook: Practical recipes for Linux system-level programming using the latest C++ features

Related to C++ System Programming Cookbook

Related ebooks

Programming For You

View More

Reviews for C++ System Programming Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    C++ System Programming Cookbook - Onorato Vaticone

    C++ System Programming Cookbook

    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

    Enjoying the preview?
    Page 1 of 1