Linux is a popular operating system for servers and workstations, but it can also be used as an alternative to Windows on desktops. This tutorial will teach you how to understand processes in Linux from the ground up.
what is process management in linux is a question that has been asked many times. The Linux process management system is quite complex and difficult to understand.
When a program or command is started, a process is generated in memory. It is given a unique identification number, known as the process identifier (PID), which the kernel uses to manage the process until the application or command with which it is connected ends. When a user signs in to the system, for example, the shell (a process) is launched. A process is also formed when a user issues a command or launches an application. As a result, every program, application, or command that executes on the system is referred to as a process.
At system startup, many processes are launched, many of which stay in memory waiting for an event to trigger a request to utilize their service. Daemons are background system programs that are essential to system operation.
Processes of Viewing
To see what processes are presently running, you may use one of two commands. ps (process status) and top are the commands in question. The ps command, when called without any parameters or arguments, displays a list of processes particular to the terminal:
17655 pts/0 00:00:00 bash 17703 pts/0 00:00:00 $ ps PID TTY TIME CMD ps
The output contains four columns: the process’s PID in the first column, the terminal to which the process belongs in the second column, the system CPU’s cumulative time for the process in the third column, and the name of the actual command or program being run in the fourth column.
To produce comprehensive information on every process running on the system, the -e (every) and -f (full) options are often used. With the ps command, you have a variety of extra choices. Details may be found on the man pages.
ps -ef ps -ef ps -ef p C STIME TTY TIME CMD UID PID PPID UID PID PPID UID PID PPID UID PID PPID UID 0 0 0 0 0 0 0 0 0 /sbin/init /sbin/init /sbin/init /sbin/init /sbin/init /sbin 0 0 0 0 0 0 0 0 0 00:00:00 [kthreadd] Feb04 3 2 0 root [ksoftirqd/0] Feb04? 00:00:01 5 2 0 root 00:00:00 [kworker/0:0H] 00:00:00 [kworker/0:0H] 00:00:00 [kworker/0:0H] 00:00:21 [rcu sched] root 7 2 0 Feb04? 00:00:16 [rcuos/0] root 8 2 0 Feb04 …………………
More information about the running processes may be seen in the output. Each column’s content type is described in the table below.
Heading | Description |
---|---|
UID | The process owner’s user ID. |
PID | The process’s unique identifier. |
PPID | The parent process’s process ID. |
C | Priority is given to the procedure. |
STIME | The commencement of the procedure. |
TTY | The terminal from which the procedure began. The? denotes that the process is a daemon, and console represents the system console. |
TIME | The process’s total execution time. |
CMD | The command’s or program’s full name. |
There are many daemon processes operating in the background in the ps output above that have no connection to any terminals. Take note of the PID and PPID values as well. The lower the number, the sooner it begins. At system startup, the process with PID 0 is launched first, followed by the process with PID 1, and so on. In the third column, there is a PPID for each PID. Along with the name of the command or application, the owner of each process is also shown.
A process table stores and maintains information about each running process, which the ps and other commands read to show information. The “top command,” which also shows the CPU, RAM, and swap usage, is the second way for examining process information. The following is an example of the output from a running top session:
To exit top, use q or Ctrl+c.
Other command-line tools, such as htop and atop, may be used to see process information, memory, and CPU usage in addition to the top command.
The following are examples of htop and atop output:
Creating a Process List
If you know the name of the process, you may use the pidof command to get the PID of that process. To list the PID of the crond daemon, for example, execute the command:
Ownership Determines Processes
Processes may be organized by who owns them or who belongs to a group. This is accomplished using the pgrep command. Use any of the following to get a list of all the processes owned by root:
# ps -U root # pgrep -U root ps -U root pgrep -U root pgrep -U root
The first command displays the PID, TTY, Time, and process name for all of the root user’s running processes, while the pgrep command just displays the PIDs. Both programs show all processes owned by the given group when the -G option is used.
List all of the processes owned by the geek user as an example.
-U geek $ ps PID TTY TIME CMD 2770? 00:00:00 gnome-keyring-d 2772? 00:00:00 init 2857? 00:00:00 ssh-agent 2862? 00:00:03 dbus-daemon 2870? 00:00:00 upstart-event-b 2875? 00:00:00 window-stack-br 2885? 00:00:00 upstart-file-br 2887
States of the Process
A process that has been started does not run indefinitely. It may be in a non-running state for a time, or it may be awaiting information from another process before continuing to operate.
Each process has five states, and each process is in one of them at any one moment. These states are described below: running, sleeping, waiting, halted, and zombie.
- The process is presently being performed by the system CPU, as shown by the running status.
- The process is in the sleeping state because it is presently awaiting input from a user or another process.
- The waiting state indicates that the process has gotten the input it was waiting for and is now ready to run when its time comes.
- The paused state implies that the process is presently suspended and will not execute until it receives a signal.
- The process is said to be in a zombie state while it is in this condition. A zombie process has the same status as any other process entry in the process table, but it consumes no resources. A zombie process’ entry is kept until the parent process allows it to die. A defunct process is sometimes known as a zombie process.
Process Pleasantness and How to Achieve It
The pleasant value is used to define a process’s priority (niceness). To set a priority, the system gives a good value to a process at the start. There are 40 pleasant values, with -20 being the most pleasant and +19 being the least pleasant. The default pleasant value of 0 is used by most system-started processes. The pleasant value of a parent process is passed down to its children.
To assess the niceness of running processes, use the ps command with the -l option. Under the NI column, you’ll find the related nicenesses for each process:
$ ps -efl F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME $ ps -efl F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME $ ps -e CMD 1 0 0 80 0 – 8536 poll s 4 S root 1 0 0 80 0 /sbin/init /sbin/init /sbin/init /sbin/init /sbin/init /sbin 00:00:00 [kthreadd] 1 S root 2 0 0 80 0 – 0 kthrea Feb15? 3 2 0 80 0 – 0 smpboo 1 S root [ksoftirqd/0] Feb15? 00:00:03 00:00:00 [kworker/0:0H] 1 S root 5 2 0 60 -20 – 0 worker Feb15? 7 2 0 80 0 – 0 1 S root rcu gp [rcu sched] Feb15? 00:00:07 rcu no Feb15? 00:00:12 [rcuos/0] 1 S root 8 2 0 80 0 – 0 rcu no Feb15? 00:00:03 [rcuos/1] 1 S root 9 2 0 80 0 – 0 rcu no Feb15? 00:00:04 [rcuos/2] 1 S root 10 2 0 80 0 – 0 ………………………………………………
Use the nice command without any parameters or arguments to discover the default niceness:
At startup, a program or instruction may be given a different priority. To execute the top command with a priority of +2, for example:
Validate the process’s niceness by using the ps command with the -l option or seeing the output of the top command. +2 is the correct value. Specify the value with a pair of dashes to execute the identical application at a higher priority with the niceness of -2:
Use the ps command to verify the new value, or look at the output of the top command. It should be a negative number.
Changing the Pleasantness of a Running Process
The renice command may be used to change the niceness of a running application. For example, to alter top’s nice value from -2 to -5 while it’s running, use the renice command with the PID (11908):
old priority 2, new priority -5 $ sudo renice -5 11908 11908 (process ID)
Use the -g option with renice to change the nice values of all processes owned by members of a certain group. Use the -u argument with it to change the nice settings of all the processes controlled by a certain user. To see how renice is used, run it without any parameters.
Files That Are Currently Open
When the process or program contained in it is run, the file is opened, and when it is no longer needed or the associated process or program has ended, the file is closed. The lsof (list open files) tool is used to identify information such as which files are open, which programs are utilizing them, and who the owners are. This command shows a list of all open files without any parameters.
# lsof COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME # lsof COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME /sbin/init 1 root cwd DIR 8,1 4096 2 / init 1 root rtd DIR 8,1 4096 2 / init 1 root txt REG 8,1 265848 3801191 /sbin/init 1 root txt REG 8,1 265848 3801191 /sbin/init 1 root txt REG 8,1 265848 3801191 /sbin/init ………………………………………………………..
In the output, the command produced nine columns, which are listed and described in the table below:
Column | Description |
---|---|
COMMAND | The first nine characters of the command or process name are shown. |
PID | The process’s PID is shown. |
USER | The owner of the process is shown. |
FD | The file’s file descriptor is shown. Some examples of values in this field are: |
FD | rtd = root directory; cwd = current working directory |
FD | txt denotes a text file; mem denotes a memory-mapped file; and pd denotes the parent directory. |
TYPE | The file’s node type is shown. |
DEVICE | The main and minor numbers of the device on which the file is stored are shown. |
SIZE | The file size or offset in bytes is shown. |
NODE | The file’s inode number is shown. |
NAME | The file name or the file system name where the file is stored is shown. |
The ps command in linux is a powerful tool that can be used to understand Linux processes. It is the most commonly used command to view and manipulate processes on Linux systems.
Related Tags
- kill process linux
- ps command unix
- processes in linux
- process management commands in linux
- ps -ef unix example