DEV Community

Cover image for All about processes and how can we view them
yash sugandh
yash sugandh

Posted on

All about processes and how can we view them

Linux is a multitasking operating system, which means that it creates an illusion that multiple programs are running at the same time by rapidly switching from one program to another.

The Linux kernel manages this through the use of processes.

Each process has the illusion that it is the only process on the computer. The tasks share common processing resources (like CPU and memory).

What exactly is a process?

An instance of a program is called a Process. A process can be simply called as a program in execution.

Every time we run a shell command, a program is run and a process is created for it.

Each process in Linux is assigned an ID called process id (PID).

There are two types of processes :

  1. Foreground Processes
    The foreground processes are those which can be seen on UI and require some sort of user input.
    For example, a text editor.

  2. Background Processes
    The background processes are those which do not require any form of input from the user and run automatically in the background.

For example, an antivirus.

There are different state of a process:

  1. Running - The state where a process is either in running or ready to run(waiting for CPU time).

  2. Interruptible - A blocked state of a process that waits for an event or a signal from another process.

  3. Uninterruptible - The process is forced to halt for certain conditions that a hardware status waits and a signal could not be handled.
    It is also known as a blocked state.

  4. Stopped - Once the process is completed, this state occurs. This process can be restarted.

  5. Zombie - In this state, the process will be terminated and the information will still be available in the process table. We get a Zombie process when a parent process dies before child.

The first process that starts when a Linux system boots up is the init process.

The kernel looks for init in /etc. If the kernel can't find init, it tries to run /bin/sh, and if that also fails, the startup of the system fails.

The PID we find out about above is assigned to a process when it is created and since the init is the first process after the Linux system boots up the PID of init is 1.

Till now we have seen what process is and how it works. Now let's look at how to view the processes that are running in our system.

1. pidof command

The pidof command is used to find the process id's of a running application.

pidof-syntax

To get the PID of a process we just use pidof along with the application name.

pidof-example

In the above example, we used the command pidof init which we know should return 1 and it did.

We also tried pidof java which returned multiple processes running for java.

2. ps command

The ps command returns the snapshot of the current processes.

ps-basic

In the above example, the ps command by default shows us all the processes that are associated with the current terminal.

TTY is short for “teletype,” and refers to the controlling terminal for the process.

Unix is showing its age here. The TIME field is the amount of CPU time consumed by the process.

To get the list of all the processes running we use the ps command along with two options e which specifies all processes and f which specifies full description.

ps-ef

In the above example, we used the command ps -ef to get the details of all the processes running.

What if we wanted to find a process id of a specific process?

  • find the PID of firefox application

ps-ef grep

In the above example, we used the command ps -ef | grep firefox to get processes running for firefox so that we can get the PID of firefox.

But, what if I tell that there is a way through which we won't need to write such long command?

3. pgrep command

The pgrep command is used to get the process id of an application.

It is similar to the pidof command is much more powerful as we do not need to provide the exact name of the application.

pgrep-basic

In the above example, we tried to find an application that has "idea" in its path.
When we tried it with pidof we got no response but when we tried the same with pgrep we got the PID.

4. top command

This utility tells the user about all the running processes on the Linux machine(It refreshes the data every 3 seconds by default).

The name top comes from the fact that the top program is used to see the “top” processes on the system.

top-command

In the above example, we can see the following

Field Description
PID The process ID of each task
User The username of task owner
PR Priority Can be 20(highest) or -20(lowest)
NI The nice value of a task
VIRT Virtual memory used (kb)
RES Physical memory used (kb)
SHR Shared memory used (kb)
%CPU % of CPU time
%MEM Physical memory used
TIME+ Total CPU time
Command Command Name

These were the tools we can use to view the processes. Please let me know if I missed something.

In the next post, we will discuss various ways to control processes. See you in the funny papers.

Top comments (2)

Collapse
 
pentacular profile image
pentacular

The key insight here should be that a process is a kind of virtual machine.

Also note that some hardware allows some number of multiple processes to run in parallel rather than being always interleaved.

Collapse
 
yashsugandh profile image
yash sugandh

Thanks for the input @pentacular