Processes on GNU Linux systems

What is the process? What is the program and how is it different from the process? How to monitor and manage processes in Linux? We will answer these and other important questions from the administrator’s point of view in this article. The program can generally be called a set of instructions leading to the performance […]

What is the process? What is the program and how is it different from the process? How to monitor and manage processes in Linux? We will answer these and other important questions from the administrator’s point of view in this article.

The program can generally be called a set of instructions leading to the performance of a specific task. It is a static creation saved in the computer’s memory most often as an executable file or shell script. An example of a program can be both a command mvand a much more complicated mail client Thunderbird.

The process will be called the program instance performed during system operation along with additional resources it uses, such as memory space or indicators for open files.

The same program can be executed many times. As a result, he may be responsible for the appearance of many processes in the system. We can distinguish them thanks to the unique numbering of each of them, in Linux systems named pid ( process ID).

init

The first user process run in the system with pid=1 is so-called init initialization). In Enterprise systems, this role has been fulfilled since version seven systemd. Inita’s tasks include creating and managing other processes in the user’s space. It will be „ancestor ” of all other processes and the last process operating in the system.

Zombie Slayer

Every process outside pid also has ppid – identifier of your parent, i.e. the process from which he was created, and gpid, i.e. the process group ID. As we mentioned earlier, the first process in the user space in the system is init. It is run by the nucleus and does not have ppid. If any of the parents ends before all child processes are completed („ orphans ” their children), the role of init will be their „adoption ”, i.e. their ppid will be changed to 1.

In systems with systemd he took over the role of adopting orphaned processes kthreadd run from pid=2

Each process-child at the end of the collapse operation all resources used and waits for the parent to react. We call this process, which is waiting only to return its closing status, the zombie process. Usually, the parent-process deals with the proper killing of their children, but if the zombie has been orphaned, init deals with its correct completion. Because of this, init is usually called in English zombie killer either child reaperwhat can be translated as „zombie slayer ” and „grim reaper of children ”.

Planner scheduler)

GNU / Linux is a multi-threaded, multi-process or multi-task operating system. This means that it strives to maximize the use of CPU – the most valuable resource on your computer. In Linux, the planner is responsible for the optimized use of resources scheduler). He supervises the process queue and allocates processor time to appropriate processes. The processor performs instructions at a dizzying pace, but has a small, physically limited number of cores. Therefore, to effectively distribute processor time, the planner must be able to effectively switch between currently performed processes and those waiting in queue.

In Linux systems, each process has the following attributes:

  • program that performs
  • context
  • permissions
  • resources allocated to him.

The key attribute for the planner is the context. At any time during execution, the process can save its state, i.e. information about the status of processor registers, the location where the program is executed, memory, etc. We call all this information the context. Due to the fact that they are saved by the process, the planner can quickly and efficiently expropriate the process currently assigned to the CPU and return the processor to another process from the queue. This gives you the ability to support multiple processes on only a few processor threads.

Some practice

In order to effectively manage processes, we should be able to view the list of those currently operating in the system. We can obtain this list using the command ps ( paccess status). By default, without providing any options, we will receive information about processes connected to the currently active shell:

┌cmd @ thinkpad-t480 ~
└> ps
PID TTY TIME CMD
6610 pts / 4 00:00:00 fish
7038 pts / 4 00:00:00 ps

To get information on all processes in the system, we can use the following options ps:

  • a– displays all processes, not just those belonging to the user
  • u– display in a more user-friendly way
  • x– displays all processes, regardless of which virtual terminal they are connected to.
┌cmd@thinkpad-t480 ~
└> ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0 193956  8424 ?        Ss   Mar05   0:22 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
root         2  0.0  0.0      0     0 ?        S    Mar05   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   Mar05   0:00 [rcu_gp]
root         4  0.0  0.0      0     0 ?        I<   Mar05   0:00 [rcu_par_gp]
root         6  0.0  0.0      0     0 ?        I<   Mar05   0:00 [kworker/0:0H-kb]
root         8  0.0  0.0      0     0 ?        I<   Mar05   0:00 [mm_percpu_wq]
root         9  0.0  0.0      0     0 ?        S    Mar05   0:18 [ksoftirqd/0]
…output truncated for clarity…

Processes written in square brackets ([]) are processes run directly by the system kernel

Program ps provides us with information about the status of the system when the command is called. In order to obtain a more interactive and real-time system status, we should reach for top be newer htop. Both programs display at the top of some illustrative information about the system, such as memory usage and processor, or a summary of information about the number of processes in the system. We encourage you to read the articles TOP in LinuxHtop – even better system monitor.

Each process has predetermined resource limits that it can use. In order to display them and settings we will reach for the tool ulimit. Call ulimit -a will display all limits for processes run by the current user.

┌cmd@thinkpad-t480 ~
└> ulimit -a
Maximum size of core files created                           (kB, -c) 0
Maximum size of a process’s data segment                     (kB, -d) unlimited
Maximum size of files created by the shell                   (kB, -f) unlimited
Maximum size that may be locked into memory                  (kB, -l) 64
Maximum resident set size                                    (kB, -m) unlimited
Maximum number of open file descriptors                          (-n) 1024
Maximum stack size                                           (kB, -s) 8192
Maximum amount of cpu time in seconds                   (seconds, -t) unlimited
Maximum number of processes available to a single user           (-u) 4096
Maximum amount of virtual memory available to the shell      (kB, -v) unlimited

In order to determine the process identifier under which a specific program was launched, we can use the self-descriptive command program pidof:

┌cmd@thinkpad-t480 ~
└> pidof firefox
26373
┌cmd@thinkpad-t480 ~
└> pidof fish
31826 22418 22188 19924 6650 1455

As you can see in the example above, if more than one instance of the program is running in the system, an ID list of all processes of the program will be displayed.

Background and foreground processes

We work various programs at the terminal. If they are interactive programs such as top, they capture keystrokes and perform various actions. For programs of type cat data will be displayed in the standard output stream or the program will expect input data from the standard stream. We say that these processes are „normally ” running programs. You can also run background processes ( background). Keyboard shortcuts will be key to presenting the example below CTRL+c and CTRL+zwhich successively send a interrupt and stop signal to the process, which effectively means interrupting the first case and stopping the program in the second.

The practical application of knowledge in this subsection will best show examples. We will use three programs for this purpose:

  1. jobs– displays currently running tasks / processes in the shell
  2. bg [PID] ( background) – starts the process with the given one pid in the background
  3. fg [PID] ( foreground) – starts the process with the given one pid as a foreground process.

For bg and fginstead of the process ID, we can specify the task index displayed using the command jobs preceded by a percentage sign.

Then we execute the command that will launch the new bash shell and execute the script that will wait for the set time (in seconds) and then write to the standard output stream text. End the command with a character & will cause the command to be executed in the background.

bash -c 'sleep 3600; echo "NUMER task" '&

Let’s move on to our example.

┌cmd@thinkpad-t480 ~
└> bash -c 'sleep 3600; echo "zadanie1"' &
┌cmd@thinkpad-t480 ~
└> bash -c 'sleep 3600; echo "zadanie2"' &
┌cmd@thinkpad-t480 ~
└> bash -c 'sleep 10; echo "zadanie3"' &
┌cmd@thinkpad-t480 ~
└> jobs
Job Group   CPU State   Command
3   1130    0%  running bash -c 'sleep 10; echo "zadanie3"' &
Job Group   CPU State   Command
2   836 0%  running bash -c 'sleep 3600; echo "zadanie2"' &
Job Group   CPU State   Command
1   520 0%  running bash -c 'sleep 3600; echo "zadanie1"' &

We have launched three processes, which can be seen in a nice table listed by the program jobs. The first column contains: the program index number that we can use in programs fg and bg (remembering their precedence with a percentage sign), pid process, status and finally the command that brought the process to life.

The last command after ten seconds will finish waiting and write to the screen test „task3 ”:

┌cmd@thinkpad-t480 ~
└> zadanie3
Job 3, “bash -c 'sleep 10; echo "zadani…” has ended
┌cmd@thinkpad-t480 ~
└> jobs
Job Group   CPU State   Command
2   836 0%  running bash -c 'sleep 3600; echo "zadanie2"' &
Job Group   CPU State   Command
1   520 0%  running bash -c 'sleep 3600; echo "zadanie1"' &

As you can see, jobs displays only two tasks. We will now launch the second process as a foreground process and terminate it by sending a interrupt signal (keyboard shortcut CTRL+c):

┌cmd@thinkpad-t480 ~
└> fg %2 
^C⏎
┌cmd@thinkpad-t480 ~
└> jobs
Job Group   CPU State   Command
1   520 0%  running bash -c 'sleep 3600; echo "zadanie1"' &

Finally, we will launch the last working task as a foreground program and stop its execution by sending a stop signal (keyboard shortcut CTRL+z):

┌cmd@thinkpad-t480 ~
└> jobs
Job Group   CPU State   Command
1   520 0%  running bash -c 'sleep 3600; echo "zadanie1"' &
┌cmd@thinkpad-t480 ~
└> fg 520
Job 1, “bash -c 'sleep 3600; echo "zada…” has stopped
┌cmd@thinkpad-t480 ~
└> jobs
Job Group   CPU State   Command
1   520 0%  stopped bash -c 'sleep 3600; echo "zadanie1"' &

Program status has changed with running on stopped. To restart this task in the background, we will use bg:

┌cmd@thinkpad-t480 ~
└> bg %1
Send job 1 “bash -c 'sleep 3600; echo "zadanie1"' &” to background
┌cmd@thinkpad-t480 ~
└> jobs
Job Group   CPU State   Command
1   520 0%  running bash -c 'sleep 3600; echo "zadanie1"' &

Finally, we will kill the program with a command kill:

┌cmd@thinkpad-t480 ~
└> kill 520
┌cmd@thinkpad-t480 ~
└> jobs
jobs: There are no jobs
fish: “bash -c 'sleep 3600; echo "zada…” terminated by signal SIGTERM (Polite quit request)

Summary

In today’s article, we learned what processes are and learned the difference between program and process. We also learned the basic tools for obtaining information about the state of processes in the system. Knowledge of processes is crucial from the administrator’s point of view, because system management is done through process manipulation.

blank Authors

The blog articles are written by people from the EuroLinux team. We owe 80% of the content to our developers, the rest is prepared by the sales or marketing department. We make every effort to ensure that the content is the best in terms of content and language, but we are not infallible. If you see anything that needs to be corrected or clarified, we'd love to hear from you.