Book Image

C++ System Programming Cookbook

By : Onorato Vaticone
Book Image

C++ System Programming Cookbook

By: Onorato Vaticone

Overview of this book

C++ is the preferred language for system programming due to its efficient low-level computation, data abstraction, and object-oriented features. System programming is about designing and writing computer programs that interact closely with the underlying operating system and allow computer hardware to interface with the programmer and the user. The C++ System Programming Cookbook will serve as a reference for developers who want to have ready-to-use solutions for the essential aspects of system programming using the latest C++ standards wherever possible. This C++ book starts out by giving you an overview of system programming and refreshing your C++ knowledge. Moving ahead, you will learn how to deal with threads and processes, before going on to discover recipes for how to manage memory. The concluding chapters will then help you understand how processes communicate and how to interact with the console (console I/O). Finally, you will learn how to deal with time interfaces, signals, and CPU scheduling. By the end of the book, you will become adept at developing robust systems applications using C++.
Table of Contents (13 chapters)

Learning the Linux fundamentals - processes and threads

Processes and threads are the execution units of any operating system. In this recipe, you'll learn how to deal with processes and threads on GNU/Linux on the command line. A process is a running instance of a program with a well-defined set of resources such as files, processor state, and threads of execution allocated to it.

A process in Linux is defined by the task_struct structure defined in the sched.h header file. On the other hand, a thread is defined by the thread_info structure in the thread_info.h header file. A thread is one possible flow of execution of the main process. A process has at least one thread (the main thread). All the threads of a process run concurrently on a system.

One aspect to keep in mind on Linux is that it doesn't differentiate between processes and threads. A thread is just like a process that shares some resources with some other processes. For this reason, in Linux, threads are often referred to as a lightweight process (LWP).

How to do it...

In this section, we'll learn, step by step, all the most common commands to control processes and threads on a GNU/Linux distribution:

  1. The ps command shows the processes, attributes, and other parameters in the current system:
root@5fd725701f0f:/# ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 4184 3396 pts/0 Ss 17:20 0:00 bash
root 18 0.0 0.1 5832 2856 pts/0 R+ 17:22 0:00 ps u
  1. Another way to get info on a process (and its threads) is to look in the /process/PID folder. This folder contains all the process info, threads of the process (in the form of subfolders with process identifiers (PIDs)), memory, and much more:
root@e9ebbdbe3899:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 4184 3344 pts/0 Ss 16:24 0:00 bash
root 149 0.0 0.1 4184 3348 pts/1 Ss 17:40 0:00 bash
root 172 85.0 0.0 5832 1708 pts/0 R+ 18:02 0:04 ./hello
root 173 0.0 0.1 5832 2804 pts/1 R+ 18:02 0:00 ps aux
root@e9ebbdbe3899:/# ll /proc/172/
total 0
dr-xr-xr-x 9 root root 0 May 12 18:02 ./
dr-xr-xr-x 200 root root 0 May 12 16:24 ../
dr-xr-xr-x 2 root root 0 May 12 18:02 attr/
-rw-r--r-- 1 root root 0 May 12 18:02 autogroup
-r-------- 1 root root 0 May 12 18:02 auxv
-r--r--r-- 1 root root 0 May 12 18:02 cgroup
--w------- 1 root root 0 May 12 18:02 clear_refs
-r--r--r-- 1 root root 0 May 12 18:02 cmdline
-rw-r--r-- 1 root root 0 May 12 18:02 comm
-rw-r--r-- 1 root root 0 May 12 18:02 coredump_filter
-r--r--r-- 1 root root 0 May 12 18:02 cpuset
lrwxrwxrwx 1 root root 0 May 12 18:02 cwd -> /root/Chapter1/
-r-------- 1 root root 0 May 12 18:02 environ
lrwxrwxrwx 1 root root 0 May 12 18:02 exe -> /root/Chapter1/hello*
dr-x------ 2 root root 0 May 12 18:02 fd/
dr-x------ 2 root root 0 May 12 18:02 fdinfo/
-rw-r--r-- 1 root root 0 May 12 18:02 gid_map
-r-------- 1 root root 0 May 12 18:02 io
-r--r--r-- 1 root root 0 May 12 18:02 limits
...
  1. A process can be killed, too. Technically, killing a process means stopping its execution:
root@5fd725701f0f:/# kill -9 PID

This command sends the kill signal (9) to the process identified with the PID. Other signals can be sent to processes—for example, HUP (hangup) and INT (interrupt).

How it works...

In step 1 for each process, we can see the following:

  • The user to whom the process belongs
  • The PID
  • The percentage of CPU and memory in a specific moment
  • When the process started, and its running time
  • The command used to run the process

Through the ps aux command, we can grab the PID of the hello process, which is 172. We can now look into the /proc/172 folder.

Processes and threads are building blocks of an operating system. In this recipe, we've seen how to interact with the kernel on the command line to get info on processes through a command (for example, ps), and by looking into a specific folder that Linux updates as the process runs. Again, every time we invoke a command (to get info on a process, in this case), the command must enter in kernel space to get valid and updated info on it.

There's more...

The ps command has many more parameters than the basic one seen in this recipe. A complete list is available on its Linux man page, man ps

A more advanced and interactive command to consider as an alternative to ps is the top command, man top