46,44 €
Push the limits of what C - and you - can do, with this high-intensity guide to the most advanced capabilities of C
Key Features
Book Description
There's a lot more to C than knowing the language syntax. The industry looks for developers with a rigorous, scientific understanding of the principles and practices. Extreme C will teach you to use C's advanced low-level power to write effective, efficient systems. This intensive, practical guide will help you become an expert C programmer.
Building on your existing C knowledge, you will master preprocessor directives, macros, conditional compilation, pointers, and much more. You will gain new insight into algorithm design, functions, and structures. You will discover how C helps you squeeze maximum performance out of critical, resource-constrained applications.
C still plays a critical role in 21st-century programming, remaining the core language for precision engineering, aviations, space research, and more. This book shows how C works with Unix, how to implement OO principles in C, and fully covers multi-processing.
In Extreme C, Amini encourages you to think, question, apply, and experiment for yourself. The book is essential for anybody who wants to take their C to the next level.
What you will learn
Who this book is for
Extreme C is for C programmers who want to dig deep into the language and its capabilities. It will help you make the most of the low-level control C gives you.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 1237
Veröffentlichungsjahr: 2019
Extreme C
Taking you to the limit in Concurrency, OOP, and the most advanced capabilities of C
Kamran Amini
BIRMINGHAM - MUMBAI
Extreme C
Copyright © 2019 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.
Acquisition Editor: Andrew Waldron
Acquisition Editor – Peer Reviews: Suresh Jain
Content Development Editor: Ian Hough
Technical Editor: Gaurav Gavas
Project Editor: Kishor Rit
Proofreader: Safis Editing
Indexer: Rekha Nair
Presentation Designer: Sandip Tadge
First published: October 2019
Production reference: 1301019
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78934-362-5
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.
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 [email protected] 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.
Kamran Amini is a senior professional specialized in embedded and kernel development. He has worked for numerous well-known Iranian companies as a senior engineer, architect, consultant, and CTO. In 2017, he moved to Europe to work as a senior architect and engineer for highly reputable companies such as Jeppesen, Adecco, TomTom, and ActiveVideo Networks. While residing in Amsterdam, he wrote this book. His main areas of interest are computation theory, distributed systems, machine learning, information theory, and quantum computation. Parallel to his professional career, he is studying astronomy and planetary sciences. His areas of academic interest are related to the early development of the universe, the geometry of black holes, quantum field theory, and string theory.
I want to thank my mother, Ehteram, who devoted her life to raising me and my brother, Ashkan. I’m certain that she is always cheering me on.
And I want to thank my beautiful and beloved wife, Afsaneh, who has supported me at every step, especially while working on this book. Without her patience and encouragement, I’m sure I wouldn’t have made it this far.
Aliakbar Abbasi is a software developer with more than six years of experience using multiple technologies and programming languages. He is an expert in OOP, C/C++, and Python. He likes studying technical books and broadening his knowledge of software development. These days, he lives in Amsterdam with his wife and works for TomTom as a senior software engineer.
Rohit Talwalkar is a vastly experienced software developer expert in the C, C++, and Java languages. He has worked on proprietary RTOS (Real Time OS), Windows and Windows Mobile devices, and the Android platform to develop applications, drivers, and services.
He received his B. Tech from the prestigious Indian Institute of Technology in Mumbai and has a master’s degree in computer science and currently works in the mixed reality field as application development lead engineer. He has worked for Motorola and BlackBerry in the past and is currently working at Magic Leap, which makes mixed reality glasses and specializes in spatial computing. In the past, Rohit was involved in reviewing C++ for the Impatient by Brian Overland.
I would like to thank Dr. Clovis Tondo, who has taught me C, C++, Java, and many other things in life.
Why subscribe?
Contributors
About the author
About the reviewers
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Get in touch
Reviews
Essential Features
Preprocessor directives
Macros
Defining a macro
Variadic macros
Advantages and disadvantages of macros
Conditional compilation
Variable pointers
Syntax
Arithmetic on variable pointers
Generic pointers
Size of a pointer
Dangling pointers
Some details about functions
Anatomy of a function
Importance in design
Stack management
Pass-by-value versus pass-by-reference
Function pointers
Structures
Why structures?
Why user-defined types?
What do structures do?
Memory layout
Nested structures
Structure pointers
Summary
From Source to Binary
Compilation pipeline
Building a C project
Header files versus source files
Example source files
Building the example
Step 1 – Preprocessing
Step 2 – Compilation
Step 3 – Assembly
Step 4 – Linking
Supporting new architectures
Step details
Preprocessor
Compiler
Abstract syntax tree
Assembler
Linker
How does the linker work?
Linker can be fooled!
C++ name mangling
Summary
Object Files
Application binary interface (ABI)
Object file formats
Relocatable object files
Executable Object Files
Static libraries
Dynamic libraries
Manual loading of shared libraries
Summary
Process Memory Structure
Process memory layout
Discovering memory structure
Probing static memory layout
BSS segment
Data segment
Text segment
Probing dynamic memory layout
Memory mappings
Stack segment
Heap segment
Summary
Stack and Heap
Stack
Probing the Stack
Points on using the Stack memory
Heap
Heap memory allocation and deallocation
Heap memory principles
Memory management in constrained environments
Memory-constrained environments
Packed structures
Compression
External data storage
Performant environments
Caching
Allocation and deallocation cost
Memory pools
Summary
OOP and Encapsulation
Object-oriented thinking
Mental concepts
Mind maps and object models
Objects are not in code
Object attributes
Domain
Relations among objects
Object-oriented operations
Objects have behaviors
C is not object-oriented, but why?
Encapsulation
Attribute encapsulation
Behavior encapsulation
Information hiding
Summary
Composition and Aggregation
Relations between classes
Object versus class
Composition
Aggregation
Summary
Inheritance and Polymorphism
Inheritance
The nature of inheritance
The first approach for having inheritance in C
The second approach to inheritance in C
Comparison of two approaches
Polymorphism
What is polymorphism?
Why do we need polymorphism?
How to have polymorphic behavior in C
Summary
Abstraction and OOP in C++
Abstraction
Object-oriented constructs in C++
Encapsulation
Inheritance
Polymorphism
Abstract classes
Summary
Unix – History and Architecture
Unix history
Multics OS and Unix
BCPL and B
The way to C
Unix architecture
Philosophy
Unix onion
Shell interface to user applications
Kernel interface to shell ring
Kernel
Hardware
Summary
System Calls and Kernels
System calls
System calls under the microscope
Bypassing standard C – calling a system call directly
Inside the syscall function
Add a system call to Linux
Kernel development
Writing a Hello World system call for Linux
Building the kernel
Unix kernels
Monolithic kernels versus microkernels
Linux
Kernel modules
Adding a kernel module to Linux
Summary
The Most Recent C
C11
Finding a supported version of C standard
Removal of the gets function
Changes to fopen function
Bounds-checking functions
No-return functions
Type generic macros
Unicode
Anonymous structures and anonymous unions
Multithreading
A bit about C18
Summary
Concurrency
Introducing concurrency
Parallelism
Concurrency
Task scheduler unit
Processes and threads
Happens-before constraint
When to use concurrency
Shared states
Summary
Synchronization
Concurrency issues
Intrinsic concurrency issues
Post-synchronization issues
Synchronization techniques
Busy-waits and spin locks
Sleep/notify mechanism
Semaphores and mutexes
Multiple processor units
Spin locks
Condition variables
Concurrency in POSIX
Kernels supporting concurrency
Multi-processing
Multithreading
Summary
Thread Execution
Threads
POSIX threads
Spawning POSIX threads
Example of race condition
Example of data race
Summary
Thread Synchronization
POSIX concurrency control
POSIX mutexes
POSIX condition variables
POSIX barriers
POSIX semaphores
POSIX threads and memory
Stack memory
Heap memory
Memory visibility
Summary
Process Execution
Process execution APIs
Process creation
Process execution
Comparing process creation and process execution
Process execution steps
Shared states
Sharing techniques
POSIX shared memory
Data race example using shared memory
File system
Multithreading versus multi-processing
Multithreading
Single-host multi-processing
Distributed multi-processing
Summary
Process Synchronization
Single-host concurrency control
Named POSIX semaphores
Named mutexes
The first example
The second example
Step 1 – Global declarations
Step 2 – Cancellation flag's shared memory
Step 3 – Named mutex's shared memory
Step 4 – Setting the cancellation flag
Step 5 – The main function
Named condition variables
Step 1 – Class of shared memory
Step 2 – Class of shared 32-bit integer counter
Step 3 – Class of shared mutex
Step 4 – Class of shared condition variable
Step 5 – The main logic
Distributed concurrency control
Summary
Single-Host IPC and Sockets
IPC techniques
Communication protocols
Protocol characteristics
Content type
Length of messages
Sequentiality
Single-host communication
File descriptors
POSIX signals
POSIX pipes
POSIX message queues
Unix domain sockets
Introduction to socket programming
Computer networks
Physical layer
Link layer
Network layer
Transport layer
Application layer
Internet protocol suite
What is socket programming?
What is a socket?
POSIX socket library
Sockets have their own descriptors!
Summary
Socket Programming
Socket programming review
Calculator project
Source hierarchy
Build the project
Run the project
Application protocol
Serialization/deserialization library
Server-side serializer/deserializer functions
Client-side serializer/deserializer functions
Calculator service
Unix domain sockets
UDS stream server
UDS stream client
UDS datagram server
UDS datagram client
Network sockets
TCP server
TCP client
UDP server
The UDP client
Summary
Integration with Other Languages
Why integration is possible?
Obtaining the necessary materials
Stack library
Integration with C++
Name mangling in C++
C++ code
Integration with Java
Writing the Java part
Writing the native part
Integration with Python
Integration with Go
Summary
Unit Testing and Debugging
Software testing
Unit testing
Test doubles
Component testing
Testing libraries for C
CMocka
Google Test
Debugging
Bug categories
Debuggers
Memory checkers
Thread debuggers
Performance profilers
Summary
Build Systems
What is a build system?
Make
CMake – not a build system!
Ninja
Bazel
Comparing build systems
Summary
Epilogue
Other Books You May Enjoy
Leave a review - let other readers know what you think
Index
Cover
Index