Introduction
A process ID (PID) is a unique number that identifies a running process in a Linux system. It is used by the operating system to keep track of processes and manage their resources. PIDs are also used by users to monitor and control processes.
In this tutorial, we will show you how to see PID in Linux. We will cover the following topics:
- What is a PID?
- How to find the PID of a process
- How to use PIDs to manage processes
- Finding the PID of a Specific Process
- Using PIDs to Control Processes
What is a PID?
A PID is a unique number that identifies a running process in a Linux system. It is a 32-bit integer, and it is typically displayed as a decimal number.
PIDs are assigned by the kernel when a process is created. The kernel keeps track of all running processes and their PIDs in a data structure called the process table.
How to Find the PID of a Process
There are several ways to find the PID of a process. The most common way is to use the ps
command. The ps
command lists all running processes, and it includes the PID of each process.
To use the ps
command, simply type the following command into a terminal window:
ps
The output of the ps
command will look something like this:
PID TTY STAT TIME COMMAND
1 pts/0 S+ 0:00 /sbin/init
2 pts/0 S+ 0:00 /usr/lib/systemd/systemd
3 pts/0 S+ 0:00 /usr/lib/systemd/systemd-udevd
4 pts/0 S+ 0:00 /usr/lib/systemd/systemd-logind
5 pts/0 S+ 0:00 /usr/lib/systemd/systemd-journald
6 pts/0 S+ 0:00 /usr/lib/systemd/systemd-tmpfiles
7 pts/0 S+ 0:00 /usr/lib/systemd/systemd-udevd-kernel
8 pts/0 S+ 0:00 /usr/lib/systemd/systemd-udevd-control
9 pts/0 S+ 0:00 /usr/lib/systemd/systemd-udevd-rules
10 pts/0 S+ 0:00 /usr/lib/systemd/systemd-udevd-trigger
The first column in the output of the ps
command is the PID column. The PID of the init
process is 1, the PID of the systemd
process is 2, and so on.
You can also use the top
command to find the PID of a process. The top
command displays a real-time list of the running processes, and it includes the PID of each process.
To use the top
command, simply type the following command into a terminal window:
top
The output of the top
command will look something like this:
top - 16:28:34 up 1 day, 4:18, 1 user, load average: 0.33, 0.35, 0.37
Tasks: 188 total, 1 running, 187 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1633940 total, 1548424 used, 85516 free, 15572 buffers
KiB Swap: 2097148 total, 0 used, 2097148 free, 40560 cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
13326 root 20 0 132448 11208 908 S 0.0 0.7 0:00.03 Xorg
14768 root 20 0 6004 4604 392 S 0.0 0.3 0:00.
How to Use PIDs to Manage Processes
PIDs can be used to manage processes in a variety of ways. For example, you can use PIDs to:
- Kill a process: You can use the
kill
command to kill a process. Thekill
command takes the PID of the process as an argument. For example, to kill the process with the PID of 1234, you would type the following command into a terminal window:
kill 1234
- Suspend a process: You can use the
suspend
command to suspend a process. Thesuspend
command takes the PID of the process as an argument. For example, to suspend the process with the PID of 1234, you would type the following command into a terminal window:
suspend 1234
- Resume a process: You can use the
resume
command to resume a suspended process. Theresume
command takes the PID of the process as an argument. For example, to resume the process with the PID of 1234, you would type the following command into a terminal window:
resume 1234
- Change the priority of a process: You can use the
nice
command to change the priority of a process. Thenice
command takes the PID of the process and a priority level as arguments. The priority level can be a number from -20 to 19. A higher priority level means that the process will be given more CPU time. For example, to change the priority of the process with the PID of 1234 to 10, you would type the following command into a terminal window:
nice 10 1234
Finding the PID of a Specific Process
If you know the name of the process you want to find the PID of, you can use the pgrep
command. The pgrep
command takes the name of the process as an argument and returns the PID of the first matching process. For example, to find the PID of the firefox
process, you would type the following command into a terminal window:
pgrep firefox
You can also use the pidof
command to find the PID of a specific process. The pidof
command takes the name of the process as an argument and returns the PID of all matching processes. For example, to find the PID of all firefox
processes, you would type the following command into a terminal window:
pidof firefox
Using PIDs to Control Processes
Once you have found the PID of a process, you can use it to control the process in a variety of ways. For example, you can use the kill
command to terminate a process, the nice
command to change the priority of a process, and the renice
command to change the scheduling policy of a process.
To kill a process, you can use the kill
command followed by the PID of the process. For example, to kill the process with the PID of 1234, you would type the following command into a terminal window:
kill 1234
To change the priority of a process, you can use the nice
command followed by the PID of the process and a priority level. The priority level can be a number from -20 to 19. A higher priority level means that the process will be given more CPU time. For example, to change the priority of the process with the PID of 1234 to 10, you would type the following command into a terminal window:
nice 10 1234
To change the scheduling policy of a process, you can use the renice
command followed by the PID of the process and a scheduling policy. The scheduling policy can be one of the following:
SCHED_OTHER
: The default scheduling policy.SCHED_FIFO
: The first-in, first-out scheduling policy.SCHED_RR
: The round-robin scheduling policy.SCHED_BATCH
: The batch scheduling policy.
For example, to change the scheduling policy of the process with the PID of 1234 to SCHED_FIFO
, you would type the following command into a terminal window:
renice -p 1234 SCHED_FIFO
Conclusion
PIDs are a powerful tool for managing processes in a Linux system. By understanding how to find and use PIDs, you can gain more control over your system and its processes.