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.
Author(s): Dr. Rian Quinn
Publisher: Packt Publishing
Year: 2018
Language: English
Pages: 673
Tags: C++ System Programming
Title Page......Page 2
Copyright and Credits......Page 3
Hands-On System Programming with C++......Page 4
Dedication......Page 5
About Packt......Page 6
Why subscribe?......Page 7
Packt.com......Page 8
Contributors......Page 9
About the author......Page 10
About the reviewer......Page 11
Packt is searching for authors like you......Page 12
Preface......Page 21
Who this book is for......Page 22
What this book covers......Page 23
To get the most out of this book......Page 25
Download the example code files......Page 26
Conventions used......Page 27
Get in touch......Page 28
Reviews......Page 29
Getting Started with System Programming......Page 30
Technical requirements......Page 31
Understanding system calls......Page 32
The anatomy of a system call......Page 33
Learning about different types of system calls......Page 36
Console input/output......Page 37
Memory allocation......Page 39
File input/output......Page 40
Networking......Page 41
Time......Page 42
Threading and process creation......Page 44
System call security risks......Page 45
SYSRET......Page 46
Meltdown and Spectre......Page 47
Benefits of using C++ when system programming......Page 49
Type safety in C++......Page 50
Objects of C++......Page 52
Templates used in C++......Page 55
Functional programming associated with C++......Page 58
Error handling mechanism in C++......Page 61
APIs and C++ containers in C++......Page 63
Summary......Page 64
Questions......Page 65
Further reading......Page 66
Learning the C, C++17, and POSIX Standards......Page 67
Technical requirements......Page 68
Beginning with the C standard language......Page 69
How the standard is organized......Page 70
Environment......Page 71
Language......Page 72
Libraries......Page 73
How a C program starts......Page 74
All about linking......Page 77
Static libraries......Page 79
Dynamic libraries......Page 80
Scope......Page 81
Pointers and arrays......Page 83
Libraries......Page 87
Learning about the C++ standard......Page 88
How the standard is organized......Page 89
General conventions and concepts......Page 90
Language syntax......Page 91
Libraries......Page 92
Linking C++ applications......Page 93
Scope......Page 97
Pointers versus references......Page 98
Libraries......Page 100
Beginning with the POSIX standard......Page 101
Memory management......Page 102
Filesystems......Page 104
Sockets......Page 105
Threading......Page 106
Summary......Page 107
Questions......Page 108
Further reading......Page 109
System Types for C and C++......Page 110
Technical requirements......Page 111
Exploring C and C++ default types......Page 112
Character types......Page 113
Integer types......Page 117
Floating – point numbers......Page 122
Boolean......Page 124
Learning standard integer types......Page 126
Structure packing......Page 131
Summary......Page 139
Questions......Page 140
Further reading......Page 141
C++, RAII, and the GSL Refresher......Page 142
Technical requirements......Page 143
A brief overview of C++17......Page 144
Language changes......Page 145
Initializers in if/switch statements......Page 146
Additions to compile-time facilities......Page 148
Namespaces......Page 151
Structured bindings......Page 152
Inline variables......Page 154
Changes in the library......Page 156
String View......Page 157
std::any, std::variant, and std::optional......Page 162
Resource Acquisition Is Initialization (RAII) ......Page 165
The Guideline Support Library (GSL)......Page 169
Pointer ownership......Page 170
Pointer arithmetic......Page 172
Contracts......Page 174
Utilities......Page 176
Summary......Page 178
Questions......Page 179
Further Reading......Page 180
Programming Linux/Unix Systems......Page 181
Technical requirements......Page 182
The Linux ABI......Page 183
The System V ABI......Page 184
The register layout......Page 186
The stack frame......Page 187
Function prologs and epilogs......Page 189
The calling convention......Page 192
Exception handling and debugging......Page 196
Virtual memory layout......Page 202
Executable and Linkable Format (ELF)......Page 203
ELF sections......Page 205
ELF segments......Page 211
The Unix filesystem......Page 215
Unix processes......Page 218
The fork() function......Page 219
The wait() function......Page 225
Interprocess communication (IPC)......Page 229
Unix pipes......Page 231
Unix shared memory......Page 235
The exec() function......Page 238
Output redirection......Page 243
Unix signals......Page 246
Summary......Page 250
Questions......Page 251
Further reading......Page 252
Learning to Program Console Input/Output......Page 253
Technical requirements......Page 254
Learning about stream-based IO......Page 255
The basics of stream......Page 256
Advantages and disadvantages of C++ stream-based IO......Page 258
Advantages of C++ stream-based IO......Page 259
Disadvantages of C++ stream-based IO......Page 260
Beginning with user-defined types......Page 261
Safety and implicit memory management......Page 263
Common debugging patterns......Page 266
Performance of C++ streams......Page 274
Learning about manipulators ......Page 276
Recreating the echo program......Page 284
Understanding the Serial Echo server example......Page 292
Summary......Page 299
Questions......Page 300
Further reading......Page 301
A Comprehensive Look at Memory Management......Page 302
Technical requirements......Page 303
Learning about the new and delete functions......Page 304
The basics for writing a program......Page 305
Aligning memory......Page 309
nothrow......Page 314
Placement of new......Page 316
Overloading......Page 319
Understanding smart pointers and ownership......Page 324
The std::unique_ptr{} pointer......Page 325
The std::shared_ptr pointer......Page 333
Learning about mapping and permissions......Page 343
The basics......Page 344
Permissions......Page 347
Smart pointers and mmap()......Page 349
Shared memory......Page 352
Learning importance of memory fragmentation......Page 355
External fragmentation......Page 356
Internal fragmentation......Page 357
Internal over external fragmentation......Page 358
External over internal fragmentation......Page 359
Summary......Page 360
Questions......Page 361
Further reading......Page 362
Learning to Program File Input/Output......Page 363
Technical requirements......Page 364
Opening a file......Page 365
Different ways to open a file......Page 366
Modes for opening a file......Page 369
Reading and writing to a file......Page 373
Reading from a file......Page 374
Reading by field......Page 375
Reading bytes......Page 378
Reading by line......Page 382
Writing to a file......Page 384
Writing by field......Page 385
Writing bytes......Page 387
Understanding file utilities......Page 395
Paths......Page 396
Understanding the logger example......Page 408
Learning about the tail file example......Page 411
Comparing C++ versus mmap benchmark......Page 414
Summary......Page 418
Questions......Page 419
Further reading......Page 420
A Hands-On Approach to Allocators......Page 421
Technical requirements......Page 422
Introducing the C++ allocators......Page 423
Learning about the basic allocator......Page 424
Understanding the allocator's properties and options......Page 429
Learning the properties......Page 430
The value pointer type......Page 431
Equality......Page 432
Different allocation types......Page 434
Copying equal allocators......Page 439
Moving equal allocators......Page 441
Exploring some optional properties......Page 443
Optional functions......Page 455
Studying an example of stateless, cache–aligned allocator......Page 456
Compiling and testing......Page 459
Studying an example of a stateful, memory–pool allocator......Page 460
Compiling and testing......Page 469
Summary......Page 470
Questions......Page 471
Further reading......Page 472
Programming POSIX Sockets Using C++......Page 473
Technical requirements......Page 474
Beginning with POSIX sockets......Page 475
Beginning with APIs......Page 476
The socket() API......Page 477
The bind() and connect() APIs......Page 479
The listen() and accept() APIs......Page 481
The send(), recv(), sendto(), and recvfrom() APIs......Page 483
Studying an example on the UDP echo server......Page 485
Server......Page 486
The client logic......Page 492
Compiling and testing......Page 497
Studying an example on the TCP echo server......Page 498
Server......Page 499
The client logic......Page 504
Compiling and testing......Page 507
Exploring an example on TCP Logger......Page 508
Server......Page 509
The client logic......Page 512
Compiling and testing......Page 516
Trying out an example for processing packets......Page 518
The client logic......Page 521
Compiling and testing......Page 523
Processing an example of processing JSON......Page 524
Server......Page 525
The client logic......Page 527
Compiling and testing......Page 529
Summary......Page 530
Questions......Page 531
Further reading......Page 532
Time Interfaces in Unix......Page 533
Technical requirements......Page 534
Learning about POSIX time.h APIs......Page 535
Learning about the types of APIs......Page 536
The time() API......Page 537
The ctime() typedef......Page 539
The localtime() and gmtime() APIs......Page 540
The asctime() function......Page 541
The strftime() function......Page 542
The difftime() function......Page 544
The mktime() function......Page 545
The clock() function......Page 546
Exploring C++ Chrono APIs......Page 548
The system_clock() API......Page 549
The time_point API......Page 550
Duration......Page 553
The steady_clock function......Page 557
The high_resolution_clock function......Page 558
Studying an example on the read system clock......Page 559
Compiling and testing......Page 561
Studying an example on high-resolution timer......Page 562
Compiling and testing......Page 564
Summary......Page 565
Questions......Page 566
Further reading......Page 567
Learning to Program POSIX and C++ Threads......Page 568
Technical requirements......Page 569
Understanding POSIX threads......Page 570
The basics of POSIX threads......Page 571
Yielding......Page 575
Synchronization......Page 578
Exploring C++ threads......Page 584
The basics of C++ threads......Page 585
Yielding......Page 589
Synchronization......Page 591
Studying an example on parallel computation......Page 603
Compiling and testing......Page 607
Studying an example on benchmarking with threads......Page 608
Compiling and testing......Page 611
Studying an example on thread logging......Page 612
Compiling and testing......Page 617
Summary......Page 619
Questions......Page 620
Further reading......Page 621
Error – Handling with Exceptions......Page 622
Technical requirements......Page 623
Error handling POSIX-style ......Page 624
Learning about set jump exceptions......Page 631
Understanding exception support in C++......Page 637
Studying an example on exception benchmark......Page 648
Compiling and testing......Page 653
Summary......Page 654
Questions......Page 655
Further reading......Page 656
Assessments......Page 657
Chapter 1......Page 658
Chapter 2......Page 659
Chapter 3......Page 660
Chapter 4......Page 661
Chapter 5......Page 662
Chapter 6......Page 663
Chapter 7......Page 664
Chapter 8......Page 665
Chapter 9......Page 666
Chapter 10......Page 667
Chapter 11......Page 668
Chapter 12......Page 669
Chapter 13......Page 670
Other Books You May Enjoy......Page 671
Leave a review - let other readers know what you think......Page 673