Processes in Linux

Introduction

In operating systems, a process is the way to represent a program in execution. It is the process that uses computer resources – processor, memory, etc – for the realization of the tasks for which the machine is intended to be used. This article will show you the main concepts related the processes on Linux and the tools used to manipulate them and to manage them.

Composition of a process

The operating system deals with a multitude of processes and, therefore, it is necessary to have means to control them. For this, the processes come with a set of characteristics, among which:

– The owner of the process;
– Process status (waiting, running, etc.);
– Execution priority;
– Memory resources.

The work process management needs to rely on the information above, and with others of equal importance to the tasks are performed in the most efficient manner. One of the means used for this is assign each process a PID.

PID and PPID

A PID (Process Identifier) is a number of id that the system gives to each process. For each new process, a new number should be assigned, or is, you can’t have a single PID for two or more processes at the same time.

The Unix-based systems need a process that is already existing if you duplicate, so that the copy can be assigned to a task new. When this occurs, the process is “copied” gets the name “parent process”, while the new one is named “process the son”. It is at this point that the PPID (Parent ProcessIdentifier) shall be used: the PPID of a process is nothing more is the PID of its parent process.

UID and GID

As already mentioned, each process needs an owner, a user who is considered its owner. From there, the system will know, by the permissions provided by the owner, who can and who can not run the the process in question. To deal with the owners, the system uses the numbers for UID and GID.

Linux manages users and groups by numbers known as UID (Ube Identifier) and GID (GroupIdentifier). As you can see, UID are the numbers of users and GID are numbers of groups. The names of the users and of the groups they serve only to facilitate the human use of the computer.

Each user needs to belong to one or more groups. As each the process (and each file) belongs to a user, so this process belongs to the group of its owner. Thus, each process is associated with a UID and a GID.

The numbers for UID and GID range from 0 to 65536. Depending on the system, the threshold value may be greater. In the case of the root user, these values are always 0 (zero). So, to make a user has the same privileges as the root, it is necessary to that your GID is 0.

Signs of processes

The signals are means is used so that processes can communicate and so the system can interfere in their functioning. For example, if the user running the kill command to stop a process, this will be done by means of a signal.

When a process receives a certain signal and account with instructions about what to do with it, such action is placed in practice. If no instructions are pre-programmed, the own Linux can perform the action according to their routines.

Between on the signal, has the following examples:

STOP – this signal has the function of interrupting the execution of a process and only re-enable it after the receipt of the signal CONT;
CONT – this signal has the function of instructing the execution of a process after it has been stopped;
SEGV – this signal informs errors of memory addresses;
TERM – this signal has the function to completely finish the process, that is, it ceases to exist after the termination;
ILL – this signal informs instruction errors illegal, for example, when there is division by zero;
KILL – this signal has the function of “kill” a process and is used in moments of criticality.

The kill is also a command that the user can use to send any signal, however, if it is used in such a way isolated, that is, without the parameter of a signal, the kill by default run the TERM signal.

The syntax for the use of the kill command is the following:

kill-SIGNAL PID

As an example, let’s assume that you want to stop temporarily the execution of the process PID 4220. For this, you can use the following command:

kill-STOP 4220

For the process to 4220 to be re-run, just use the command:

kill-CONT 4220

If the signal needs to be sent to all the processes, you can use the number -1 in place of the PID. For example:

kill-STOP -1

As already said, use the kill command in isolation – for example, kill 4220 – causes this use the TERM signal by default. This signal, however, can be ignored by processes. This is why it is good practice to use the command “kill -9 PID” to “kill” a process, since the number nine represents the signal to kill, and this cannot be ignored. This makes it clear that if you know the number that is assigned to the a signal, you can use it in place of your name. With the exception some of the signs, the numbering of each can change depending with the distribution or with the version of the kernel.

It is also common to use the kill as follows: kill-l PID. The option “-l” (letter L lower case) is used to list the processes that have accepted the kill.

Now, imagine that you do not know the PID of a process and you have forgotten that the ps command (seen later) finds out such information. In this case, you can use the killall command, since you know the name of the process. The syntax is:

killall -SIGNAL process

For example:

killall -STOP vi

State of the processes

When a process is created, this does not mean that he it will be immediately executed. In addition, certain processes can be temporarily paralyzed so that the processor can run a process priority. This means that the processes, in certain times, may be in situations of execution different. The Linux works, essentially, with four types of situation, that is, states:

Executable: the process can be executed immediately;
Dormant: the process needs to wait for something to be executed. Only after this “thing” happen is that it passes to the state executable;
Zombie: the process is considered to be “dead”, but, for some reason, still exists;
Stopped: the process is “frozen”, that is, can not be executed.

The commands nice and renice

When talking about the commands nice and renice it is necessary to understand the concept of kindness. A process may have priority in relation the other in its execution. When a process is gentle,means that it “offers the kindly” allow a process with a higher priority than your run before him. The levels of kindness, also called nice, are determined through the numbers. The higher the nice value, the more gentle is the process. Usually, the range of numbers used in nice are the integers between -19 and 19.

Although determining the priority of a process is not a practice common, after all, the Linux does very well with this task, this it may be necessary in some situation. For this, you use a command that takes the same name of the concept: nice. The syntax is:

nice-n priority process

For example:

nice-n -5 ntpd

In the example, the ntpd receives priority -5. This is a high priority, after all, as already stated, the smaller the number the less for your kindness.

If a particular process is running, this happens with a priority already defined. To change a process in this condition, use the command renice, whose syntax is:

renice priority option process/target

The options of configuration are:

-u – the change occurs in the processes of the the user informed;
-g – the change occurs in the processes of the the group indicated;
-p – the change will occur in the process whose PID is informed.

An example:

renice +19 1000 -u abbreviationfinder

In this case, the command renice changed the priority of the process 1000, as well as the priority of user processes abbreviationfinder.

Checking processes with ps

The ps is a command of the utmost importance to the management processes. For him, it is possible to know which processes currently running, which UIDs and PIDs corresponding, etc.

If only ps is typed in the command line, usually the system shows which user processes. You need to use a combination of options to get more details.

The most important options are the following:

a – shows all the existing processes;
and – displays the environment variables related to the processes;
f – display the tree of running processes;
l – displays more fields in the result;
m – shows the amount of memory occupied by each process;
u – displays the name of the user who started a given process and the time at which this occurred;
x – displays processes that are not associated with terminals,
w – if the result of process does not fit on a line, this option causes the rest to be displayed in line following.

Of the above options, the combination of more used (at least here in the AbbreviationFinder) is aux:

ps aux

As an example, the result obtained with this command was the following:

Note that using the combination of lax, the result shows more details:

ps lax

Below follows the description of the fields shown previously and some that are only shown with the combination lax:

USER – user name of the user owner of the process;
UID – user id number owner of process;
PID – process identification number;
PPID -the process identification number father;
%CPU – percentage of processing used;
%MEM – percentage of memory used;
VSZ – indicates the virtual size of the process;
RSS – acronym for Resident Set Size, indicates the the amount of memory used (in KB);
TTY – indicates the identifier for the terminal of the process;
START – time at which the process was started;
TIME – processing time already consumed by the process;
COMMAND – name of the command that executes the one process;
PRI – priority value of the process;
NI – value have the priority (usually equal to the values PRI);
WCHAN – shows the function of kernel where the process if it is in suspended mode;
STAT – indicates the current state of the process, being represented by a letter: R – executable; D – waiting on the disk; S – Suspended; T – stopped, Z – Zombie. These letters can be combined, and still greater of: the W – process paged to disk; < – process with higher priority than the conventional; N – process with a lower priority than the conventional; L – process with some features blocked in the kernel.

Checking processes with the top

The ps command works as if you take a picture of the situation of the processes at that time. The top command, in turn, collects the information, but the updates regularly. Usually this update occurs every 10 seconds.

The syntax of the top command is the following:

top -option

Among the options, one has the following:

-d – updates the top after a certain period time (in seconds). To do this, enter the amount of seconds after the letter d. For example: top -d 30;
-c – displays the command line instead of the name of the process;
-i – makes the top ignore processes in zombie state;
-s – run top in secure mode.

It is possible to manipulate some of the features of the top command through of the keys on the keyboard. For example, to immediately refresh the result displayed, just press the space key. If you press the key q, the top is finalized. Press the h key while you are using the top to see the full list of options and shortcut keys.

The resources, jobs, fg and bg, fuser, pstree, nohup

To have even more control over the processes running on Linux, you can use the following commands: jobs, fg and bg, fuser, pstree, nohup. Each one is described below:

jobs – used to view the processes that are stopped or running in the background). When a process isin this condition, it means its execution is done by the kernel without that is linked to a terminal. In other words, a background process is one that is executed while the user does something else in the system. A tip to know if the the process is in the background is to verify the existence the & character at the end of the line. If the process is stopped, usually the word “stopped” appears in the line, otherwise, the the word “running” is displayed. The syntax of the jobs is:

jobs-option

The available options are:

-l – list the processes by PID;
-r – list only the processes running;
-o – list only those processes stopped.

If in the line of a process appear in the positive ( + ) sign, it means that this is the most recent to be paralyzed or be in a second plan. If the signal is negative (-), the process was the penultimate. Note also that in the beginning of the line a number is shown between brackets. Many confuse this value with the PID of the process, but, in fact, this is the order number used by the jobs.

fg and bg: fg is a command that allows a process in background (or stopped) pass to the first (foreground), while the bg passes a process from the first plan to the second. To use bg, you should hang the process. This can be done by pressing Ctrl + Z on the keyboard. Then, you type the command in the following way:

bg +number

The listed number corresponds to the order value informed in the the beginning of the line when the command jobs is used.

As for the fg command, the syntax is the same:

fg +number

fuser: the command fuser shows which process is using a particular file or directory. Its syntax is:

fuser -option path (file or directory)

Among the options, we have:

-k – terminates the process which uses the file/directory in question;
-i – should be used in conjunction with the switch k and serves to ask if the completion of the process should be made;
-u – shows the owner of the process;
-v – the result is shown in a default view similar to the ps command.

pstree: this command shows the related processes in the format tree. Its syntax is:

pstree -option PID

Among the options, we have:

-u – shows the owner of the process;
-p – display the PID after the process name;
-c – shows the relationship of active processes;
-G – uses certain characters to display the result in a format the chart.

An important detail: if when you type the command pstree PID not is given, all processes will be listed.

nohup: command nohup allows the process to stay active even when the user logs out. It is the nature of the systems Unix-based stop processes in case your owner does not is the most active, therefore, the nohup can be very useful. Its syntax is:

nohup command

Ending

Know how to deal with processes can be crucial to maintain a computer working and performing their tasks in a critical situation. The subject is essential to systems administrators, but it is important even to the home user. Obviously the control process is not limited to such features here, this article should be interpreted as a basic guide. In addition, if you intend to get a certificate in Linux, to have knowledge about the control processes can give significant points. So, do not hesitate to explore the other commands. If you want more details, use the help resources, such as the traditional “man command”.

Processes in Linux 1