Find solutions to all your problems related to Linux system programming using practical recipes for developing your own system programs.
Key Features
- Develop a deeper understanding of how Linux system programming works
- Gain hands-on experience of working with different Linux projects with the help of practical examples
- Learn how to develop your own programs for Linux
----------------------
Book Description
Linux is the world's most popular open source operating system (OS). Linux System Programming Techniques will enable you to extend the Linux OS with your own system programs and communicate with other programs on the system.
The book begins by exploring the Linux filesystem, its basic commands, built-in manual pages, the GNU compiler collection (GCC), and Linux system calls. You'll then discover how to handle errors in your programs and will learn to catch errors and print relevant information about them. The book takes you through multiple recipes on how to read and write files on the system, using both streams and file descriptors. As you advance, you'll delve into forking, creating zombie processes, and daemons, along with recipes on how to handle daemons using systemd. After this, you'll find out how to create shared libraries and start exploring different types of interprocess communication (IPC). In the later chapters, recipes on how to write programs using POSIX threads and how to debug your programs using the GNU debugger (GDB) and Valgrind will also be covered.
By the end of this Linux book, you will be able to develop your own system programs for Linux, including daemons, tools, clients, and filters.
----------------------
What you will learn
- Discover how to write programs for the Linux system using a wide variety of system calls
- Delve into the working of POSIX functions
- Understand and use key concepts such as signals, pipes, IPC, and process management
- Find out how to integrate programs with a Linux system
- Explore advanced topics such as filesystem operations, creating shared libraries, and debugging your programs
- Gain an overall understanding of how to debug your programs using Valgrind
----------------------
Who this book is for
This book is for anyone who wants to develop system programs for Linux and gain a deeper understanding of the Linux system.
The book is beneficial for anyone who is facing issues related to a particular part of Linux system programming and is looking for specific recipes or solutions.
----------------------
About the Author
Jack-Benny Persson is a consultant and author based in Sweden. He has written several books about Linux and programming. His passion for Linux and other Unix-like systems started as a hobby more than 20 years ago. Since then, he has spent most of his spare time reading about Linux, tinkering with Linux servers, and writing about Linux administration. Today he has his own IT and media company in Sweden that focuses on Linux.
Jack-Benny holds an Advanced Higher Vocational Education Diploma as a Linux system specialist. He has also studied electronics, networking, and security.
Author(s): Jack-Benny Persson
Edition: 1
Publisher: Packt
Year: ,2021
Language: English
Pages: 432
Tags: c17 posix linux systems
Cover
Title Page
Copyright and Credits
Contributors
Table of Contents
Preface
Chapter 1: Getting the Necessary Tools and Writing Our First Linux Programs
Technical requirements
Installing Git to download the code repository
Installing GCC and GNU Make
Getting ready
How to do it…
How it works…
Installing GDB and Valgrind
Getting ready
How to do it…
How it works…
Writing a simple C program for Linux
Getting ready
How to do it…
How it works…
There's more…
Writing a program that parses command-line options
Getting ready
How to do it…
How it works…
Looking up information in the built-in manual page
Getting ready
How to do it…
How it works…
There's more…
Searching the manual for information
Getting ready
How to do it…
How it works…
There's more…
Chapter 2: Making Your Programs Easy to Script
Technical requirements
Return values and how to read them
Getting ready
How to do it…
How it works…
There's more…
See also
Exiting a program with a relevant return value
Getting ready
How to do it…
How it works…
There's more…
Redirecting stdin, stdout, and stderr
Getting ready
How to do it…
How it works…
There's more…
Connecting programs using pipes
Getting ready
How to do it…
How it works…
There's more…
See also
Writing to stdout and stderr
How to do it…
How it works…
There's more…
Reading from stdin
Getting ready
How to do it…
How it works…
There's more…
Writing a pipe-friendly program
Getting ready
How to do it…
How it works…
There's more…
Redirecting the result to a file
Getting ready
How to do it…
How it works…
There's more…
Reading environment variables
Getting ready
How to do it…
How it works…
Chapter 3: Diving Deep into C in Linux
Technical requirements
Linking against libraries using GCC
Getting ready
How to do it…
How it works…
There's more…
Changing C standards
Getting ready
How to do it…
How it works…
There's more…
Using system calls – and when not to use them
Getting ready
How to do it…
How it works…
There's more…
Getting information about Linux- and Unix-specific header files
Getting ready
How to do it…
How it works…
There's more…
Defining feature test macros
Getting ready
How to do it…
How it works…
There's more…
Looking at the four stages of compilation
Getting ready
How to do it…
How it works…
Compiling with Make
Getting ready
How to do it…
How it works…
Writing a generic Makefile with GCC options
Getting ready
How to do it…
How it works…
There's more…
Writing a simple Makefile
Getting ready
How to do it…
How it works…
Writing a more advanced Makefile
Getting ready
How to do it…
How it works…
Chapter 4: Handling Errors in Your Programs
Technical requirements
Why error handling is important in system programming
Getting ready
How to do it…
How it works…
Handling some common errors
Getting ready
How to do it…
How it works…
Error handling and errno
Getting ready
How to do it…
How it works…
Handling more errno macros
Getting ready
How to do it…
How it works…
There's more…
Using errno with strerror()
Getting ready
How to do it…
There's more…
Using errno with perror()
Getting ready
How to do it…
There's more…
Returning an error value
Getting ready
How to do it…
How it works…
There's more…
Chapter 5: Working with File I/O and Filesystem Operations
Technical requirements
Reading inode information and learning the filesystem
Getting ready
How to do it…
How it works…
Creating soft links and hard links
Getting ready
How to do it…
How it works…
There's more…
Creating files and updating the timestamp
Getting ready
How to do it…
How it works…
There's more…
Deleting files
Getting ready
How to do it…
How it works…
Getting access rights and ownership
Getting ready
How to do it…
How it works…
Setting access rights and ownership
Getting ready
How to do it…
How it works…
There's more…
Writing to files with file descriptors
Getting ready
How to do it…
How it works…
There's more…
Reading from files with file descriptors
Getting ready
How to do it…
How it works…
There's more…
Writing to files with streams
Getting ready
How to do it…
How it works…
See also
Reading from files with streams
Getting ready
How to do it…
How it works…
There's more…
Reading and writing binary data with streams
Getting ready
How to do it…
How it works…
There's more…
Moving around inside a file with lseek()
Getting ready
How to do it…
How it works…
Moving around inside a file with fseek()
Getting ready
How to do it…
How it works…
There's more…
Chapter 6: Spawning Processes and Using Job Control
Technical requirements
Exploring how processes are created
Getting ready
How to do it…
How it works…
There's more…
Using job control in Bash
Getting ready
How to do it…
How it works…
Controlling and terminating processes using signals
Getting ready
How to do it…
How it works…
See also
Replacing the program in a process with execl()
Getting ready
How to do it…
How it works…
See also
Forking a process
Getting ready
How to do it…
How it works…
There's more…
Executing a new program in a forked process
Getting ready
How to do it…
How it works…
Starting a new process with system()
Getting ready
How to do it…
How it works…
Creating a zombie process
Getting ready
How to do it…
How it works…
There's more…
Learning about what orphans are
Getting ready
How to do it…
How it works…
See also
Creating a daemon
Getting ready
How to do it…
How it works…
There's more…
Implementing a signal handler
Getting ready
How to do it…
How it works…
There's more…
Chapter 7: Using systemd to Handle Your Daemons
Technical requirements
Getting to know systemd
Getting ready
How it works...
There's more...
See also
Writing a unit file for a daemon
Getting ready
How to do it...
How it works...
There's more...
See also
Enabling and disabling a service – and starting and stopping it
Getting ready
How to do it...
How it works...
Creating a more modern daemon for systemd
Getting ready
How to do it...
How it works...
See also
Making the new daemon a systemd service
Getting ready
How to do it...
How it works...
Reading the journal
Getting ready
How to do it...
How it works...
See also
Chapter 8: Creating Shared Libraries
Technical requirements
The what and why of libraries
Getting ready
How to do it…
How it works…
There's more…
Creating a static library
Getting ready
How to do it…
How it works…
See also
Using a static library
Getting ready
How to do it…
How it works…
Creating a dynamic library
Getting ready
How to do it…
How it works…
There's more…
See also
Installing the dynamic library on the system
Getting ready
How to do it…
How it works…
Using the dynamic library in a program
Getting ready
How to do it…
How it works…
See also
Compiling a statically linked program
Getting ready
How to do it…
How it works…
Chapter 9: Terminal I/O and Changing Terminal Behavior
Technical requirements
Viewing terminal information
Getting ready
How to do it…
How it works…
See also
Changing terminal settings with stty
Getting ready
How to do it…
How it works…
Investigating TTYs and PTYs and writing to them
How to do it…
How it works…
There's more…
See also
Checking if it's a TTY
Getting ready
How to do it…
How it works…
See also
Creating a PTY
Getting ready
How to do it…
How it works…
See also
Disabling echo for password prompts
Getting ready
How to do it…
How it works…
There's more…
Reading the terminal size
Getting ready
How to do it…
How it works…
There's more…
See also
Chapter 10: Using Different Kinds of IPC
Technical requirements
Using signals for IPC – building a client for the daemon
Getting ready
How to do it…
How it works…
See also
Communicating with a pipe
Getting ready
How to do it…
How it works…
See also
FIFO – using it in the shell
Getting ready
How to do it…
How it works…
See also
FIFO – building the sender
Getting ready
How to do it…
How it works…
See also
FIFO – building the receiver
Getting ready
How to do it…
How it works…
See also
Message queues – creating the sender
Getting ready
How to do it…
How it works…
See also
Message queues – creating the receiver
Getting ready
How to do it…
How it works…
See also
Communicating between child and parent with shared memory
Getting ready
How to do it…
How it works…
See also
Using shared memory between unrelated processes
Getting ready
How to do it…
How it works…
See also
Unix socket – creating the server
Getting ready
How to do it…
How it works…
See also
Unix socket – creating the client
Getting ready
How to do it…
How it works…
See also
Chapter 11: Using Threads in Your Programs
Technical requirements
Writing your first threaded program
Getting ready
How to do it…
How it works…
There's more…
See also
Reading return values from threads
Getting ready
How to do it…
How it works…
Causing a race condition
Getting ready
How to do it…
How it works…
Avoiding race conditions with mutexes
Getting ready
How to do it…
How it works..
See also
Making the mutex program more efficient
Getting ready
How to do it…
How it works…
Using condition variables
Getting ready
How it works…
See also
Chapter 12: Debugging Your Programs
Technical requirements
Starting GDB
Getting ready
How to do it…
How it works…
There's more…
Stepping inside a function with GDB
Getting ready
How to do it…
How it works…
Investigating memory with GDB
Getting ready
How to do it…
How it works…
There's more...
See also
Modifying variables during runtime
Getting ready
How to do it…
How it works…
Using GDB on a program that forks
Getting ready
How to do it…
How it works…
There's more…
Debugging programs with multiple threads
Getting ready
How to do it…
How it works…
There's more…
See also
Finding a simple memory leak with Valgrind
Getting started
How to do it…
How it works…
See also
Finding buffer overflows with Valgrind
Getting ready
How to do it…
How it works…
There's more...
Why subscribe?
About Packt
Other Books You May Enjoy
Index