Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
C++ Programming for Linux Systems

You're reading from  C++ Programming for Linux Systems

Product type Book
Published in Sep 2023
Publisher Packt
ISBN-13 9781805129004
Pages 288 pages
Edition 1st Edition
Languages
Authors (2):
Desislav Andreev Desislav Andreev
Profile icon Desislav Andreev
Stanimir Lukanov Stanimir Lukanov
Profile icon Stanimir Lukanov
View More author details

Table of Contents (15) Chapters

Preface 1. Part 1:Securing the Fundamentals
2. Chapter 1: Getting Started with Linux Systems and the POSIX Standard 3. Chapter 2: Learning More about Process Management 4. Chapter 3: Navigating through the Filesystems 5. Chapter 4: Diving Deep into the C++ Object 6. Chapter 5: Handling Errors with C++ 7. Part 2:Advanced Techniques for System Programming
8. Chapter 6: Concurrent System Programming with C++ 9. Chapter 7: Proceeding with Inter-Process Communication 10. Chapter 8: Using Clocks, Timers, and Signals in Linux 11. Chapter 9: Understanding the C++ Memory Model 12. Chapter 10: Using Coroutines in C++ for System Programming 13. Index 14. Other Books You May Enjoy

Learning More about Process Management

You became familiar with the concept of processes in the previous chapter. Now, it’s time to get into details. It is important to understand how process management is related to the system’s overall behavior. In this chapter, we will emphasize fundamental OS mechanisms that are used specifically for process control and resource access management. We will use this opportunity to show you how to use some C++ features too.

Once we’ve investigated the program and its corresponding process as system entities, we are going to discuss the states that one process goes through during its lifetime. You are going to learn about spawning new processes and threads. You are also going to see the underlying problems of such activities. Later we are going to check out some examples while slowly introducing the multithreaded code. By doing so, you will have the opportunity to learn the basics of some POSIX and C++ techniques that are related...

Technical requirements

To run the code examples in this chapter, you must prepare the following:

Disassembling process creation

As we mentioned in the previous chapter, a process is a running instance of a program that contains its respective metadata, occupied memory, opened files, and so on. It is the main job executor in the OS. Recall that the overall goal of programming is to transform one type of data into another type of data, or count. What we do via programming languages is provide instructions to the hardware. Often, we tell the CPU what to do, including moving pieces of data throughout different portions of memory. In other words, the computer must compute, and we must tell it how to do this. This understanding is crucial and independent of the programming languages or OSs that are used.

With this, we have come back to the topic of system programming and understanding system behavior. Let’s immediately state that process creation and execution is neither simple nor fast. And neither is the process switching. It is rarely observable through the naked eye, but...

Continuing with process states and some scheduling mechanisms

In the previous section, we discussed to how initiate a new process. But what happens with it under the hood? As mentioned in Chapter 1, processes and threads are considered tasks in Linux’s scheduler. Their states are generic, and their understanding is important for correct procedure planning. A task, when expecting a resource, might have to wait or even stopped. We can affect this behavior through synchronization mechanisms as well, such as semaphores and mutexes, which we’ll discuss later in this chapter. We believe that understanding these fundamentals is crucial for system programmers as bad task state management can lead to unpredictability and overall system degradation. This is strongly observable in large-scale systems.

For now, let’s step aside for a bit and try to simplify the code’s goals – it needs to instruct the CPU to perform an operation and modify the data. Our task...

Learning more about process creation

A common practice in system programming is to follow a strict timeline for process creation and execution. Programmers use either daemons, such as systemd and other in-house developed solutions, or startup scripts. We can use the Terminal as well but this is mostly for when we repair the system’s state and restore it, or test a given functionality. Another way to initiate processes from our code is through system calls. You probably know some of them, such as fork() and vfork().

Introducing fork()

Let’s look at an example; we’ll discuss it afterward:

#include <iostream>
#include <unistd.h>
using namespace std;
void process_creator() {
    if (fork() == 0) // {1}
        cout << "Child with pid: " << getpid() << endl;
    else
        cout << "Parent...

Introducing the system calls for thread manipulation in C++

As discussed in Chapter 1, we use threads to execute separate procedures in parallel. They exist only in the scope of a process and their creation overhead is bigger than the thread’s one, so we consider them lightweight, although they have their own stack and task_struct. They are almost self-sufficient, except they rely on the parent process to exist. That process is also known as the main thread. All others that are created by it need to join it to be initiated. You could create thousands of threads simultaneously on the system, but they will not run in parallel. You can run only n parallel tasks, where n is the number of the system’s concurrent ALUs (occasionally, these are the hardware’s concurrent threads). The others will be scheduled according to the OS’s task-scheduling mechanism. Let’s look at the simplest example of a POSIX thread interface:

pthread_t new_thread;
pthread_create...

Summary

In this chapter, we walked through the low-level events that occur during process or thread creation and manipulation. We discussed the processes’ memory layout and its significance. You also learned some important points about the OS’s way of task scheduling and what happens in the background during process and thread state updates. We will use these fundamentals later in this book. The next chapter will cover filesystem management and will provide you with some interesting C++ instruments in that domain.

lock icon The rest of the chapter is locked
You have been reading a chapter from
C++ Programming for Linux Systems
Published in: Sep 2023 Publisher: Packt ISBN-13: 9781805129004
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}