Linux Process Management

Whenever you give a command in any operating system, the process starts. In Linux, when the process starts a five-digit number is created which is called process id or PID. This is a system-generated unique Id given for the process.

With the help of the PID, we can track the process and two processes won't have the same process IDs.

Starting a Process

A process executes in two ways they are,

  • Foreground Processes
  • Background Processes

Foreground Processes

Every process by default runs in Foreground which means the output is printed on the screen.

The best example for the foreground process is the ls command which prints the output on the screen by listing the files and directories.

When a program is running in the foreground, we can not start another process without completing the previous process. It is because of this reason foreground process is considered a time-consuming process.

Background Processes

When a process starts running and it is not visible on the screen it is called a background process. You can simultaneously run the 'n' number of commands in the background process.

To enable the background process you have to provide the ampersand symbol (&) at the end of the command.

Example :

ls &

Whenever the process is completed and then you hit enter you will get a prompt like the below snippet, which means the background process is completed successfully.

[1] + Done ls & 
$

Types of process

  • Child process : A process that is created by some other process during run-time is called a child process. The child process is usually created to execute some binary form within an existing process.
  • Daemon process : These are special processes that run in the background. These are system-related processes that have no associated terminals.
  • Orphan process : When the parent process gets killed, the child process becomes the orphan process, and the child process is monitored by the init process. Though the init process adopts the orphan process, this is still called the orphan process as their parent process no longer exists.
  • Zombie process : A zombie process is one that should have terminated but it is still active in the process table. This is usually caused when a parent process has not yet realized that it has completed or wants to create another process with the same process name but with using a different process id.
  • Interactive process : This type of process interacts constantly with the users and therefore spends a lot of time waiting for keyboard and mouse inputs.
  • Batch or automatic process : These do not need user interaction and hence they often run in the background.

Tracking the processes

The ps command is used to track or to list all the running processes.

Example :

$ ps

Output :

PID TTY TIME CMD
19 pts/1 00:00:00 ls
24 pts/1 00:00:00 ps

Options :

  • -a: Shows information about all users
  • -x: Shows information about processes without terminals
  • -u: Shows additional information like -f option
  • -e: Displays extended information

If you require more information about the process you can use the ps command along with the -f option.

$ ps –f

Output :

UID PID PPID C STIME  TTY TIME  CMD
52471 19 1 0 07:20 pts/1 00:00:00f ls
52471 25 19 0 08:04 pts/1 00:00:00 ps -f

To get more information about a particular process, you can give the ps command along with the PID.

$ ps 19

Output :

PID TTY TIME CMD
19 pts/1 00:00:00 ls

The information provided by the ps command for the process are :

  • UID: User ID that this process belongs to (the person running it)
  • PID: Process ID
  • PPID: Parent process ID (the ID of the process that started it)
  • C: CPU utilization of process
  • STIME: Process start time
  • TTY: Terminal type associated with the process
  • TIME: Time taken by the CPU for the process
  • CMD: The command that started this process

Stopping a process

When a process runs in the foreground, you can interrupt the process by hitting Ctrl+c. When a process runs in the background the kill command is used along with the process id to interrupt or to stop the running process.

Example:

$ kill 19

Output: The process which has the process id 19 is terminated.

Terminated

Sometimes a process cannot be terminated by using the kill command. To terminate such kind of process uses the below command.

$ kill -9 pid

-9 forces the termination of the process by blocking the signals.
Example :

$ kill -9 19

Output :

Terminated

Other process commands

The below command is used to get the list of jobs that are either running or stopped.

jobs

The bg command is used to resume the suspended jobs and runs them in the background.

Syntax :

bg [ %job ]

Example :

bg %19

The fg command is used to start the job which has already stopped in the foreground.

Syntax :

fg [ job_id ]

Example :

fg 19

The top command is used to show all the running processes within the working environment of Linux.


Syntax :

top

Output :
linux-systemadmin16

Press 'q' on the keyboard to move out of the process display.

Terminologies of top command:

Field Description
PID The process ID of each task
User The username of the task owner
PR Priority Can be -20(highest) or 19(lowest)
NI The nice value of a task
VIRT Virtual memory used (kb)
RES Physical memory used (kb)
SHR Shared memory used (kb)
S

Status of the process.

The status is of five types:

  • 'D' = uninterruptible sleep
  • 'R' = running
  • 'S' = sleeping
  • 'T' = traced or stopped
  • 'Z' = zombie
%CPU % of CPU time
%MEM Physical memory used
TIME+ Total CPU time
Command Command name

The nice command starts a new process (job) and assigns it a priority (nice) value for the process. The nice value ranges from -20 to 19, where -20 is of the highest priority and 19 is of the lowest priority


Syntax :

nice [-nice value]

The renice command is used to change the priority of the running process.


Syntax :

renice [-nice value] [process id]

The df command shows the disk information of the Linux file systems.
Syntax :

df

Output :

Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/loop0      1845236  15246876   2554440  86% /
none                   4          0         4   0% /sys/fs/task
udev              452369         4    493808   1% /dev
tmpfs             195263      1364     99308   2% /run
none                2123        0      5120   0% /run/lock
none              421980      1764    501588   1% /run/shm
none              562831       20    102380   1% /run/user
/dev/sda3      158297648 164417964  10348112  95% /host

Thefree command shows the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel


Syntax :

free

Output :

 total       used       free     shared    buffers     cached
Mem:       1006708     935872      70836          0     148244     346656
-/+ buffers/cache:     440972     565736
Swap:       262140     130084     132056
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions