Ps Explained
Ps Explained
Understanding Linux process management can be useful for both developers and
system administrators. The ps command is an excellent way to get information
about processes running on the system.
What is a process?
A process can access and use system resources such as memory, CPUs, �les on
1 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
the �le system, and physical devices. To manage and control access to these
resources, the kernel needs to keep track of which processes are running and
which resources are in use. The kernel does this using a data structure to
represent the processes. This data structure is quite large and complex, so this
article only focuses on a few categories of information.
State
Identi�ers
Each process in the system has a process identi�er, or PID. Process also have
user and group identi�ers used to control access on the system resources.
Links
Each process except for the initial one has a parent process. The kernel keeps
track of the hierarchical relationship between processes.
File system
This category is used to track access to any �les a process has opened.
Virtual Memory
This category is used by the kernel to track the mapping of each process’ virtual
2 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
ps in action
Now that you have a better idea of what a process is, here’s a look at the ps
command and how to use it to get information about our system’s running
processes. To quickly print all the active processes, use the following command:
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 20:31 ? 00:00:03 /usr/lib/systemd/systemd
--switched-root --system --deserialize 24
root 2 0 0 20:31 ? 00:00:00 [kthreadd]
root 4 2 0 20:31 ? 00:00:00 [kworker/0:0H]
....
Note the use of the -e option to display all processes, and -f to display the full
format. You can also use -F for extended full format. The full format prints these
eight columns:
C = CPU utilization
TTY = terminal
TIME = combined system and user CPU time used by the process so far
You can also see that systemd, which is the initial process, has a parent PID of 0.
As the initial process, it has no parent.
The extended full format adds three more columns to the displayed output:
3 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
$ ps -eF
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 1 0 0 57658 11988 3 20:31 ? 00:00:03 /usr/lib
/systemd/systemd --switched-root --system --deserialize 24
root 2 0 0 0 0 0 20:31 ? 00:00:00 [kthreadd]
root 4 2 0 0 0 0 20:31 ? 00:00:00 [kworker/0:0H]
root 6 2 0 0 0 0 20:31 ? 00:00:00 [mm_percpu_wq]
You can also customize the output of the ps command. Use the -o option to
specify which columns you would like to be displayed. This can be useful when
using ps in a shell script.
$ ps -ef -o pid,cmd
PID CMD
1 /usr/lib/systemd/systemd --switched-root --system --deserialize 24
2 [kthreadd]
4 [kworker/0:0H]
Another useful option is -u, which allows you to �lter the processes displayed by
user ID. For example, to display all the processes owned by the user root, run a
command like this:
$ ps -u root -o user,pid,cpu,cmd
USER PID CPU CMD
root 1 - /usr/lib/systemd/systemd --switched-root --system
--deserialize 24
root 2 - [kthreadd]
root 4 - [kworker/0:0H]
root 6 - [mm_percpu_wq]
root 7 - [ksoftirqd/0]
$ ps -u gdm -o user,pid,cpu,cmd
USER PID CPU CMD
gdm 1126 - /usr/lib/systemd/systemd --user
gdm 1139 - (sd-pam)
gdm 1157 - /usr/libexec/gdm-wayland-session gnome-session --autostart
/usr/share/gdm/greeter/autostart
gdm 1180 - /usr/bin/dbus-daemon --session --address=systemd: --nofork
--nopidfile --systemd-activation --syslog-only
gdm 1194 - /usr/libexec/gnome-session-binary --autostart /usr/share
/gdm/greeter/autostart
4 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
Debugging using ps
$ ps -C firefox -L -o pid,tid,pcpu,state,nlwp,cmd
PID TID %CPU S NLWP COMMAND
3015 3015 7.6 S 69 /usr/lib64/firefox/firefox
3015 3040 0.0 S 69 /usr/lib64/firefox/firefox
3015 3041 0.0 S 69 /usr/lib64/firefox/firefox
3015 3042 0.5 S 69 /usr/lib64/firefox/firefox
3015 3043 0.3 S 69 /usr/lib64/firefox/firefox
3015 3044 0.0 S 69 /usr/lib64/firefox/firefox
....
This example uses the -C option to select the processes using the command
name �refox. The -L option shows the threads for that process. The -o option
again displays selected columns, in this case PID and CMD, along with some
other new columns:
STATE = state of the process, as seen earlier in this article. In the example the
threads are in the S state, which means interruptible sleep — these
processes are waiting for an event to run.
Finally, you can use ps to track a memory leak. The presence of a memory leak in
a process would increase the RAM memory the process has. You can monitor the
Resident Set Size (RSS) to have an idea of how much RAM memory a process is
using.
This command makes use of the –sort -rss option to show the process with the
highest RSS at the top. You could sort the processes in the other direction
5 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
Share:
Like this:
Like
One blogger likes this.
Previous post
Cody
February 10, 2018 at 02:02
6 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
Ehm, no. It indicates there was a request for more memory (or memory
at all). A process using more memory doesn’t equate to a memory leak;
it equates to requesting more memory. Correlation does not imply
causation. That’s all there is to it. Even if there is a memory leak it won’t
show you where in the code there is and – critically is this: there is such
a thing as sharing memory so just because you think it should be
free()d doesn’t mean it should be. A memory leak doesn’t mean what
you’re implying at all. If that was the de�nition of a memory leak every
application would have memory leaks – and it would potentially make
naïve programmers try to ‘�x’ it when it’s not needed and more than
likely cause major problems. Memory bugs can be incredibly di�cult to
track down and �x depending on what speci�c type of memory bug but
a memory leak isn’t using more memory. I have no idea where or how
you got that idea but it’s wrong. I could explain it but I have to run so I’ll
just refer you to Wiki: https://en.wikipedia.org/wiki/Memory_leak
Renê
February 11, 2018 at 11:04
Leave a Reply
Your email address will not be published.
7 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
Name
Website
Post Comment
8 of 9 2/12/18, 10:46 AM
Inspect and manage processes with ps - Fedora ... https://fedoramagazine.org/inspect-manage-proce...
S E A R C H FO R M CONTRIBUTE
The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora
Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases.
You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora
logo is a trademark of Red Hat, Inc. Terms and Conditions
9 of 9 2/12/18, 10:46 AM