C++ Programming for Linux Systems....2
Foreword....7
Contributors....8
About the author(s)....8
About the reviewer(s)....9
Preface....19
Who this book is for....20
What this book covers....21
To get the most out of this book....23
Download the example code files....24
Conventions used....24
Get in touch....24
Share Your Thoughts....25
Download a free PDF copy of this book....25
Part 1:Securing the Fundamentals....27
Chapter 1: Getting Started with Linux Systems and the POSIX Standard....28
Technical requirements....29
Getting familiar with the concept of OSs....29
Types of OSs....30
Linux in short....31
Getting to know the Linux kernel....32
Introducing the system call interface and system programming....34
Navigating through files, processes, and threads....37
File....37
Process and thread....38
Types of processes based on their running mode....41
Running services with init and systemd....42
Portable Operating System Interface (POSIX)....48
Summary....49
Chapter 2: Learning More about Process Management....51
Technical requirements....51
Disassembling process creation....52
Memory segments....55
Continuing with process states and some scheduling mechanisms....59
Scheduling mechanisms....62
Scheduling at a high level....62
Scheduling at a low level....62
Learning more about process creation....65
Introducing fork()....66
exec and clone()....67
Terminating a process....70
Blocking a calling process....71
Introducing the system calls for thread manipulation in C++....73
Joining and detaching threads....74
Thread termination....75
Summary....75
Chapter 3: Navigating through the Filesystems....77
Technical requirements....77
Going through Linux’s filesystem fundamentals....78
Linux’s FS....79
Directory structure and partitioning....81
Linux FS objects....84
Executing FS operations with C++....89
IPC through anonymous pipes and named pipes....93
Anonymous or unnamed pipes....94
Named pipes....100
Briefly observing signal handling....107
Summary....108
Chapter 4: Diving Deep into the C++ Object....109
Technical requirements....110
Understanding the C++ object model....110
Declaration versus definition....110
Scope, storage duration, and lifetimes....111
The object....113
The reference....116
Understanding why initialization matters....116
Functors and lambdas....122
Exploring functional objects....122
Lambda expressions....128
Summary....133
Chapter 5: Handling Errors with C++....134
Technical requirements....134
Handling errors from POSIX APIs with C++....135
Using std::error_code and std::error_condition....137
From error codes to exceptions....142
std::system_error....143
Throw by value, catch by reference....147
try/catch … finally....150
Summary....153
Part 2:Advanced Techniques for System Programming....154
Chapter 6: Concurrent System Programming with C++....155
Technical requirements....155
What is concurrency?....156
Threads versus processes....159
Concurrency with C++....161
Demystifying race conditions and data races....162
How do we avoid them?....167
Practical multithreading....167
Hello C++ jthread....167
Canceling threads – is this really possible?....171
std::stop_source....174
Sharing data during parallel execution....176
Barriers and latches....178
Summary....185
Chapter 7: Proceeding with Inter-Process Communication....187
Technical requirements....187
Introducing MQs and the pub/sub mechanism....188
The pub/sub mechanism....192
Guaranteeing atomic operations through semaphores and mutual exclusions....194
Semaphore....196
Mutual exclusion (mutex)....200
Using shared memory....204
Learning about mmap() and shm_open()....207
Communicating through the network with sockets....210
Overview of the OSI model....210
Getting familiar with networking through UDP....212
Thinking about robustness through TCP....218
Summary....225
Chapter 8: Using Clocks, Timers, and Signals in Linux....226
Technical requirements....226
Handling time in Linux....227
Linux epoch....228
Using timers in Linux....228
POSIX timer characteristics....231
Handling time in C++....231
Using clocks, timers, and ratios....234
More about clocks in C++20....237
Using calendar and time zone capabilities....239
Working with time zones in C++....242
Summary....243
Chapter 9: Understanding the C++ Memory Model....245
Technical requirements....245
Getting to know smart pointers and optionals in C++....246
Retracing RAII via smart pointers....247
Doing a lazy initialization in C++....250
Learning about condition variables, read-write locks, and ranges in C++....253
Cooperative cancellation through condition variables....254
Combining smart pointers, condition variables, and shared memory....256
Implementing read-write locks and ranges with C++....258
Discussing multiprocessor systems – cache locality and cache friendliness in C++....262
Considering cache locality through cache-friendly code....263
A glance at false sharing....265
Sharing resources larger than a cache line in C++....268
Revisiting shared resources through the C++ memory model via spinlock implementation....270
Introducing the memory_order type in C++....270
Designing spinlocks for multiprocessor systems in C++....272
Summary....276
Chapter 10: Using Coroutines in C++ for System Programming....277
Technical requirements....277
Introducing coroutines....278
The coroutine facility in C++....281
Network programming and coroutines in C++....288
Revisiting the shared memory problem through coroutines in C++....291
Final thoughts on coroutines and their implementations in C++....294
Summary....295
Index....297
Why subscribe?....318
Other Books You May Enjoy....318
Packt is searching for authors like you....322
Share Your Thoughts....322
Download a free PDF copy of this book....322
Around 35 million Linux and almost 2 billion Android users rely on C++ for everything from the simplest embedded and IoT devices to cloud services, supercomputing, and space exploration. To help you produce high-quality software, two industry experts have transformed their knowledge and experience into practical examples in system programming with C++ Programming for Linux Systems.
In this book, you'll explore the latest C++20 features, while working on multiple specific use cases. You'll get familiar with the coroutines and modern approaches in concurrent and multithreaded programming. You'll also learn to reshape your thinking when analyzing system behavior in Linux (POSIX) environments. Additionally, you'll discover advanced discussions and novel solutions for complex challenges, while approaching trivial system operations with a new outlook and learning to choose the best design for your particular case.
You can use this workbook as an introduction to system programming and software design in Linux or any Unix-based environment. You'll also find it useful as a guideline or a supplement to any C++ book.
By the end of this book, you'll have gained advanced knowledge and skills for working with Linux or any Unix-based environment.
This book is for every software developer looking to improve and update their C++ development skills. Both students and professionals will find this book useful as the examples are curated to match any area of expertise and are easily adaptable. At the same time, they don't lose focus of the system specifics. A basic understanding of operating systems' interfaces is a must along with experience in software development.