Hands-On System Programming with C++

Book description

A hands-on guide to making system programming with C++ easy

Key Features

  • Write system-level code leveraging C++17
  • Learn the internals of the Linux Application Binary Interface (ABI) and apply it to system programming
  • Explore C++ concurrency to take advantage of server-level constructs

Book Description

C++ is a general-purpose programming language with a bias toward system programming as it provides ready access to hardware-level resources, efficient compilation, and a versatile approach to higher-level abstractions.

This book will help you understand the benefits of system programming with C++17. You will gain a firm understanding of various C, C++, and POSIX standards, as well as their respective system types for both C++ and POSIX. After a brief refresher on C++, Resource Acquisition Is Initialization (RAII), and the new C++ Guideline Support Library (GSL), you will learn to program Linux and Unix systems along with process management. As you progress through the chapters, you will become acquainted with C++'s support for IO. You will then study various memory management methods, including a chapter on allocators and how they benefit system programming. You will also explore how to program file input and output and learn about POSIX sockets. This book will help you get to grips with safely setting up a UDP and TCP server/client.

Finally, you will be guided through Unix time interfaces, multithreading, and error handling with C++ exceptions. By the end of this book, you will be comfortable with using C++ to program high-quality systems.

What you will learn

  • Understand the benefits of using C++ for system programming
  • Program Linux/Unix systems using C++
  • Discover the advantages of Resource Acquisition Is Initialization (RAII)
  • Program both console and file input and output
  • Uncover the POSIX socket APIs and understand how to program them
  • Explore advanced system programming topics, such as C++ allocators
  • Use POSIX and C++ threads to program concurrent systems
  • Grasp how C++ can be used to create performant system applications

Who this book is for

If you are a fresh developer with intermediate knowledge of C++ but little or no knowledge of Unix and Linux system programming, this book will help you learn system programming with C++ in a practical way.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On System Programming with C++
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with System Programming
    1. Technical requirements
    2. Understanding system calls
      1. The anatomy of a system call
      2. Learning about different types of system calls
        1. Console input/output
        2. Memory allocation
        3. File input/output
        4. Networking
        5. Time
        6. Threading and process creation
      3. System call security risks
        1. SYSRET
        2. Meltdown and Spectre
    3. Benefits of using C++ when system programming
      1. Type safety in C++
      2. Objects of C++
      3. Templates used in C++
      4. Functional programming associated with C++
      5. Error handling mechanism in C++
      6. APIs and C++ containers in C++
    4. Summary
    5. Questions
    6. Further reading
  8. Learning the C, C++17, and POSIX Standards
    1. Technical requirements
    2. Beginning with the C standard language
      1. How the standard is organized
        1. Environment
        2. Language
        3. Libraries
      2. How a C program starts
      3. All about linking
        1. Static libraries
        2. Dynamic libraries
      4. Scope
      5. Pointers and arrays
      6. Libraries
    3. Learning about the C++ standard
      1. How the standard is organized
        1. General conventions and concepts
        2. Language syntax
        3. Libraries
      2. Linking C++ applications
      3. Scope
      4. Pointers versus references
      5. Libraries
    4. Beginning with the POSIX standard
      1. Memory management
      2. Filesystems
      3. Sockets
      4. Threading
    5. Summary
    6. Questions
    7. Further reading
  9. System Types for C and C++
    1. Technical requirements
    2. Exploring C and C++ default types
      1. Character types
      2. Integer types
      3. Floating – point numbers
      4. Boolean
    3. Learning standard integer types
      1. Structure packing
    4. Summary
    5. Questions
    6. Further reading
  10. C++, RAII, and the GSL Refresher
    1. Technical requirements
    2. A brief overview of C++17
      1. Language changes
        1. Initializers in if/switch statements
        2. Additions to compile-time facilities
        3. Namespaces
        4. Structured bindings
        5. Inline variables
      2. Changes in the library
        1. String View
        2. std::any, std::variant, and std::optional
    3. Resource Acquisition Is Initialization (RAII)
    4. The Guideline Support Library (GSL)
      1. Pointer ownership
      2. Pointer arithmetic
      3. Contracts
      4. Utilities
    5. Summary
    6. Questions
    7. Further Reading
  11. Programming Linux/Unix Systems
    1. Technical requirements
    2. The Linux ABI
      1. The System V ABI
        1. The register layout
        2. The stack frame
        3. Function prologs and epilogs
        4. The calling convention
        5. Exception handling and debugging
        6. Virtual memory layout
      2. Executable and Linkable Format (ELF)
        1. ELF sections
        2. ELF segments
    3. The Unix filesystem
    4. Unix processes
      1. The fork() function
      2. The wait() function
      3. Interprocess communication (IPC)
        1. Unix pipes
        2. Unix shared memory
      4. The exec() function
      5. Output redirection
    5. Unix signals
    6. Summary
    7. Questions
    8. Further reading
  12. Learning to Program Console Input/Output
    1. Technical requirements
    2. Learning about stream-based IO
      1. The basics of stream
      2. Advantages and disadvantages of C++ stream-based IO
        1. Advantages of C++ stream-based IO
        2. Disadvantages of C++ stream-based IO
    3. Beginning with user-defined types
      1. Safety and implicit memory management
      2. Common debugging patterns
      3. Performance of C++ streams
    4. Learning about manipulators
    5. Recreating the echo program
    6. Understanding the Serial Echo server example
    7. Summary
    8. Questions
    9. Further reading
  13. A Comprehensive Look at Memory Management
    1. Technical requirements
    2. Learning about the new and delete functions
      1. The basics for writing a program
      2. Aligning memory
      3. nothrow
      4. Placement of new
      5. Overloading
    3. Understanding smart pointers and ownership
      1. The std::unique_ptr{} pointer
      2. The std::shared_ptr pointer
    4. Learning about mapping and permissions
      1. The basics
      2. Permissions
      3. Smart pointers and mmap()
      4. Shared memory
    5. Learning importance of memory fragmentation
      1. External fragmentation
      2. Internal fragmentation
        1. Internal over external fragmentation
        2. External over internal fragmentation
    6. Summary
    7. Questions
    8. Further reading
  14. Learning to Program File Input/Output
    1. Technical requirements
    2. Opening a file
      1. Different ways to open a file
      2. Modes for opening a file
    3. Reading and writing to a file
      1. Reading from a file
        1. Reading by field
        2. Reading bytes
        3. Reading by line
      2. Writing to a file
        1. Writing by field
        2. Writing bytes
    4. Understanding file utilities
      1. Paths
    5. Understanding the logger example
    6. Learning about the tail file example
    7. Comparing C++ versus mmap benchmark
    8. Summary
    9. Questions
    10. Further reading
  15. A Hands-On Approach to Allocators
    1. Technical requirements
    2. Introducing the C++ allocators
      1. Learning about the basic allocator
      2. Understanding the allocator's properties and options
        1. Learning the properties
          1. The value pointer type
          2. Equality
        2. Different allocation types
          1. Copying equal allocators
          2. Moving equal allocators
        3. Exploring some optional properties
        4. Optional functions
    3. Studying an example of stateless, cache–aligned allocator
      1. Compiling and testing
    4. Studying an example of a stateful, memory–pool allocator
      1. Compiling and testing
    5. Summary
    6. Questions
    7. Further reading
  16. Programming POSIX Sockets Using C++
    1. Technical requirements
    2. Beginning with POSIX sockets
    3. Beginning with APIs
      1. The socket() API
        1. The bind() and connect() APIs
        2. The listen() and accept() APIs
        3. The send(), recv(), sendto(), and recvfrom() APIs
    4. Studying an example on the UDP echo server
      1. Server
      2. The client logic
      3. Compiling and testing
    5. Studying an example on the TCP echo server
      1. Server
      2. The client logic
      3. Compiling and testing
    6. Exploring an example on TCP Logger
      1. Server
      2. The client logic
      3. Compiling and testing
    7. Trying out an example for processing packets
      1. The client logic
      2. Compiling and testing
    8. Processing an example of processing JSON
      1. Server
      2. The client logic
      3. Compiling and testing
    9. Summary
    10. Questions
    11. Further reading
  17. Time Interfaces in Unix
    1. Technical requirements
    2. Learning about POSIX time.h APIs
      1. Learning about the types of APIs
      2. The time() API
      3. The ctime() typedef
      4. The localtime() and gmtime() APIs
      5. The asctime() function
      6. The strftime() function
      7. The difftime() function
      8. The mktime() function
      9. The clock() function
    3. Exploring C++ Chrono APIs
      1. The system_clock() API
      2. The time_point API
      3. Duration
      4. The steady_clock function
      5. The high_resolution_clock function
    4. Studying an example on the read system clock
      1. Compiling and testing
    5. Studying an example on high-resolution timer
      1. Compiling and testing
    6. Summary
    7. Questions
    8. Further reading
  18. Learning to Program POSIX and C++ Threads
    1. Technical requirements
    2. Understanding POSIX threads
      1. The basics of POSIX threads
      2. Yielding
      3. Synchronization
    3. Exploring C++ threads
      1. The basics of C++ threads
      2. Yielding
      3. Synchronization
    4. Studying an example on parallel computation
      1. Compiling and testing
    5. Studying an example on benchmarking with threads
      1. Compiling and testing
    6. Studying an example on thread logging
      1. Compiling and testing
    7. Summary
    8. Questions
    9. Further reading
  19. Error – Handling with Exceptions
    1. Technical requirements
    2. Error handling POSIX-style
    3. Learning about set jump exceptions
    4. Understanding exception support in C++
    5. Studying an example on exception benchmark
      1. Compiling and testing
    6. Summary
    7. Questions
    8. Further reading
  20. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On System Programming with C++
  • Author(s): Dr. Rian Quinn
  • Release date: December 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789137880